Précédent   Forum des professionnels en informatique > Logiciels > Microsoft Office > Access > IHM
IHM Ce forum est dédié aux questions relatives à la création de formulaires et d'états, avec ou sans code VBA, et macros.
Partagez cette discussion sur d'autres réseaux sociaux : Viadeo Twitter Google Facebook Digg Delicious MySpace Yahoo
Réponse Proposer ce sujet en actualité
 
Outils de la discussion
Publicité
'
Vieux 06/09/2011, 12h36   #1
Invité régulier
 
Femme
Inscription : juin 2011
Messages : 17
Détails du profil
Informations personnelles :
Sexe : Femme

Informations forums :
Inscription : juin 2011
Messages : 17
Points : 6
Points : 6
Par défaut Formulaire de recherche

Bonjour,

J'ai besoin de créer le formulaire de recherche, j'ai trouvé un exemple sur le http://access.mvps.org/access/forms/frm0045.htm
Il est bien sympa, sauf que je voudrais pouvoir faire la recherche sur tous les champ de la table. Dans le combobox j'ai rajouté option "*" et le code correspondant, mais ce ne marche pas bien sur. Est-ce que qn a des idées comment mettre cela en oeuvre?

Merci d'avance,
Voici le code:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
Option Compare Database
Option Explicit
 
'--------------------------------------------------------------------------------------------------------------
'  Copyright (C)1998-99 Dev Ashish and Terry Kreft, All Rights Reserved
'  The Access Web (http://home.att.net/~dashish)
'  Comments and bug reports can be emailed to us
'  Dev Ashish (dash10@hotmail.com) ; Terry Kreft (terry.kreft@mps.co.uk)
'--------------------------------------------------------------------------------------------------------------
 
Private Type OPENFILENAME
  lStructSize As Long
  hwnd As Long
  hInstance As Long
  lpstrFilter As String
  lpstrCustomFilter As String
  nMaxCustFilter As Long
  nFilterIndex As Long
  lpstrFile As String
  nMaxFile As Long
  lpstrFileTitle As String
  nMaxFileTitle As Long
  lpstrInitialDir As String
  lpstrTitle As String
  Flags As Long
  nFileOffset As Integer
  nFileExtension As Integer
  lpstrDefExt As String
  lCustData As Long
  lpfnHook As Long
  lpTemplateName As String
End Type
 
Private Declare Function GetOpenFileName _
    Lib "comdlg32.dll" Alias "GetOpenFileNameA" _
    (pOpenfilename As OPENFILENAME) As Long
 
Private Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
 
Private Const OFN_FILEMUSTEXIST = &H1000
Private Const OFN_HIDEREADONLY = &H4
Private Const OFN_OVERWRITEPROMPT = &H2
Private Const OFN_PATHMUSTEXIST = &H800
Private Const OFN_SAVE = 0
Private Const OFN_OPEN = 1
 
 
 
Private Type CTLInf
  Name As String
  Enabled As Boolean
End Type
Private Declare Function apiSortStringArray Lib "msaccess.exe" _
   Alias "#81" _
   (astrStringArray() As String) _
   As Long
 
Private arrCtls() As CTLInf
Private mvarOriginalFields  As Variant
Private Const mconQ = """"
 
Private Sub chkEditSQL_Click()
   With Me
      .txtSQL.Enabled = (.chkEditSQL = True)
      .txtSQL.Locked = Not ((.chkEditSQL = True))
      .txtSQL.BackColor = IIf(.chkEditSQL = True, vbWhite, _
                                          -2147483633)
   End With
End Sub
 
Private Sub cmdExport_Click()
On Error GoTo ErrHandler
Dim arrCtl As Control
Dim intUbound As Integer
Dim intLbound As Integer
Dim intCount As Integer
  Select Case cmdExport.Tag
  Case "Choose"
    intCount = -1
    For Each arrCtl In Me.Controls
      Select Case arrCtl.ControlType
      Case acTextBox, acComboBox, acCheckBox, acListBox, acCommandButton
        If arrCtl.Name <> "cmdExport" And arrCtl.Name <> "lstResult" Then
          intCount = intCount + 1
          ReDim Preserve arrCtls(0 To intCount)
          With arrCtls(intCount)
            .Name = arrCtl.Name
            .Enabled = arrCtl.Enabled
          End With
          arrCtl.Enabled = False
        End If
      End Select
    Next
 
    With lstResult
      .ColumnCount = 4
      .ColumnWidths = "0,0,0"
      .RowSourceType = "Value List"
      .RowSource = "-1,-1,-1,Export Type," _
                 & "0,0,.xls,Excel 3," _
                 & "0,6,.xls,Excel 4," _
                 & "0,5,.xls,Excel 5," _
                 & "0,5,.xls,Excel 7," _
                 & "0,8,.xls,Excel 97," _
                 & "0,2,.wk1,Lotus WK1," _
                 & "0,3,.wk3,Lotus WK3," _
                 & "0,7,.wk4,Lotus WK4," _
                 & "0,4,.wj2,Lotus WJ2 (Japanese)," _
                 & "1,2,.txt,Delimited Text," _
                 & "1,8,.html,HTML"
                 '& "1,3,.txt,Fixed Length Text,"
      .Selected(1) = True
    End With
    Label16.Caption = "Select ..."
    cmdExport.Tag = "Export"
  Case "Export"
    If MsgBox("Are you sure you want to export this query", vbYesNo + vbQuestion) <> vbNo Then
      Call ExportRoutine
    End If
    intLbound = LBound(arrCtls)
    intUbound = UBound(arrCtls)
    For intCount = intLbound To intUbound
      With arrCtls(intCount)
        Me(.Name).Enabled = .Enabled
      End With
    Next
    Label16.Caption = "Search Results"
    cmdExport.Tag = "Choose"
    lstResult.ColumnWidths = ""
    If Me.chkAutoBuildSQL = True Then Call sBuildSQL
  End Select
ExitHere:
   Exit Sub
ErrHandler:
  If Err = 2448 Then Resume Next
   Resume ExitHere
End Sub
 
Private Sub Form_Load()
  cmdExport.Tag = "Choose"
End Sub
 
Private Sub txtSQL_AfterUpdate()
   'build the SQL with what we have
   Call sBuildSQL
End Sub
 
Private Sub cmdBuildSQL_Click()
   'build the SQL with what we have
   Call sBuildSQL
End Sub
 
Private Sub cmdClear_Click()
'Clear out and disable appropriate controls on the form
Dim ctl As Control
   On Error Resume Next
   For Each ctl In Me.Controls
      Select Case ctl.ControlType
         Case acTextBox:
            ctl = Null
            ctl.Enabled = False
            ctl.BackColor = -2147483633
         Case acCommandButton:
            'only disable the CopySQL or CreateQDF command buttons
            If ctl.Name = "cmdCopySQL" Or ctl.Name = "cmdCreateQDF" Then
               ctl.Enabled = False
            End If
         Case acOptionGroup, acListBox:
            If Not Screen.ActiveControl.ControlType = acListBox Then _
               ctl = Null
         Case acCheckBox:
            If ctl.Name = "chkEditSQL" Then
               ctl = Null
               ctl.Enabled = False
            End If
         Case Else:
            ctl = Null
            ctl.Enabled = False
      End Select
      If ctl.Name <> "cmdExport" Then ctl.Tag = vbNullString
   Next
   With Me.lstResult
      .Enabled = False
      .ColumnCount = 1
      .ColumnHeads = False
      .RowSource = vbNullString
   End With
   mvarOriginalFields = Null
   Me.txtSQL.Enabled = True
   Me.cmdClear.Enabled = True
   'Me.lstTables = Null
End Sub
 
Private Sub cmdCopySQL_Click()
   'Copy the SQL to the clipboard
   On Error Resume Next
   With Me
      .txtSQL.SetFocus
      '.txtSQL.SelText = .txtSQL.SelLength
      DoCmd.RunCommand acCmdCopy
      Screen.PreviousControl.SetFocus
   End With
End Sub
 
Private Sub cmdCreateQDF_Click()
On Error GoTo ErrHandler
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim strName As String
   'first get a unique name for the querydef object
   strName = Application.Run("acwzmain.wlib_stUniquedocname", "Query1", acQuery)
   strName = InputBox("Please specify a query name", "Save As", strName)
   If Not strName = vbNullString Then
      'only create the querydef if user really wants to.
      Set db = CurrentDb
      Set qdf = db.CreateQueryDef(strName, Me.txtSQL)
      qdf.Close
   Else
      'ok, so they don't want to
      MsgBox "The save operation was cancelled." & vbCrLf & _
         "Please try again.", vbExclamation + vbOKOnly, "Cancelled"
   End If
ExitHere:
   On Error Resume Next
   qdf.Close
   Set qdf = Nothing
   db.QueryDefs.Refresh
   Set db = Nothing
   Exit Sub
ErrHandler:
   Resume ExitHere
End Sub
 
Private Sub cmdUndo0_Click()
   Call sDisableControls(0)
End Sub
 
Private Sub cmdUndo1_Click()
   Call sDisableControls(1)
End Sub
 
Private Sub cmdUndo2_Click()
   Call sDisableControls(2)
End Sub
 
Private Sub cmdUndo3_Click()
   Call sDisableControls(3)
End Sub
 
Private Sub cmdUndo4_Click()
   Call sDisableControls(4)
End Sub
 
Private Sub Command87_Click()
   Me.lstTables.Requery
   Call cmdClear_Click
End Sub
 
Private Sub lstTables_AfterUpdate()
 'Try and enable the next control only if the the Clear
 'button has been clicked (ctl.Tag = vbNullString)
 'Otherwise just requery the field's info
 '
   Call cmdClear_Click
   If Me.lstTables.Tag = vbNullString Then Call fEnableNextInTab
   Me.cbxFld0.Requery
End Sub
 
Private Sub cmdExit_Click()
   DoCmd.Close acForm, Me.Name
End Sub
 
Private Sub cmdHelp_Click()
'Display the Help message box.
'
Dim strOut As String
  strOut = "The listbox contains names of all tables and Select queries." & vbCrLf _
    & "Please note that in this version, search cannot be performed against " & vbCrLf _
    & "parametric Select queries " & vbCrLf _
    & "(A warning message will be displayed upon selection.)" & vbCrLf _
    & "To create a new search:" & vbCrLf _
    & Space(5) & "-Select a Table/Query in the listbox." & vbCrLf _
    & Space(5) & "-Select a field name in the first combo box" & vbCrLf _
    & Space(5) & "-and specify a criteria in the textbox." & vbCrLf _
    & Space(7) & " o The criteria may include wildcards '?' or '*'" & vbCrLf _
    & Space(9) & " e.g. *husky* ; Alex*?" _
    & Space(7) & vbCrLf & "To search for Null, use" & vbCrLf _
    & Space(9) & " Is Null" & vbCrLf _
    & Space(7) & " o Numeric values examples: " & vbCrLf _
    & Space(9) & " >9" & vbCrLf _
    & Space(9) & " = 10" & vbCrLf _
    & Space(7) & " o For dates, don't use the '#' delimiter, eg." & vbCrLf _
    & Space(9) & " > 1/1/1999"
 
  strOut = strOut & vbCrLf _
    & "To remove a criteria, click on the Undo icon." _
    & vbCrLf & "To start over, click on 'Clear'." _
    & vbCrLf & "To create a new query, click on 'Create Query' " _
      & vbCrLf & Space(3) & " when the button is enabled (if the SQL is valid)." _
    & vbCrLf & "To copy the SQL statement to the Clipboard, " _
      & "click on 'Copy SQL'."
 
    strOut = strOut & vbCrLf & vbCrLf _
                  & "© 1998-1999, Terry Kreft and Dev Ashish." & vbCrLf _
                  & "The Access Web (http://www.mvps.org/access)"
    MsgBox strOut, vbInformation + vbOKOnly, "Search tips" _
                     & ": Version " & fGetDocObjectProperty(Me.Name, "Forms", "Version")
End Sub
 
Private Sub sDisableControls(intIndex As Integer)
'Undo/disable the field combo, criteria textbox,
' and the Or/And option
'
   On Error Resume Next
   With Me
      .Controls("cbxFld" & intIndex) = Null
      .Controls("opgClauseType" & intIndex) = Null
      .Controls("txtVal" & intIndex) = Null
   End With
   If Not intIndex = 0 Then
      'if the user wants to clear out the first combo,
      'don't disable, just clear out the controls
      With Me
         .Controls("cbxFld" & intIndex).Enabled = False
         .Controls("txtVal" & intIndex).Enabled = False
      End With
   End If
   'Build the SQL automatically only if the user specified so
   If Me.chkAutoBuildSQL = True Then Call sBuildSQL
End Sub
 
Private Sub sFillCombo(intTargetIndex As Integer)
'Fills the Rowsource for a combo
'
On Error GoTo ErrHandler
Dim i As Long
Dim j As Integer
Dim strOut As String
Dim ctlTarget As Control
 
   'Which one to fill?
   Set ctlTarget = Me("cbxFld" & intTargetIndex)
   For i = LBound(mvarOriginalFields) To UBound(mvarOriginalFields)
      strOut = strOut & mvarOriginalFields(i) & ";"
   Next
   strOut = " * ;" & strOut   
With ctlTarget
      .RowSourceType = "Value List"
      .RowSource = strOut
   End With
ExitHere:
   Set ctlTarget = Nothing
   Exit Sub
ErrHandler:
   Resume ExitHere
End Sub
 
Sub sBuildSQL()
'  Take what's currently selected on the form
'  and create a dynamic SQL statement for the
'  lstResults listbox.
'
On Error GoTo ErrHandler
Dim strSQL As String
Dim strWhere As String
Dim strJoinType As String
Dim i As Integer
Dim j As Integer
Dim l As Integer
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim tdf As TableDef
Dim qdf As DAO.QueryDef
Dim rsQdf As DAO.Recordset
Dim fld As DAO.Field
Const conMAXCONTROLS = 5
 
   Set db = DBEngine(0)(0)
   strSQL = "Select * "
   'Right now we have five combo/textbox sets
   'so set up the master loop to go through these controls
   For i = 0 To conMAXCONTROLS - 1
      strJoinType = vbNullString
      'there might be some unused sets, so don't bother
      'going through the disabled controls
      If Me("cbxFld" & i).Enabled Then
         'The Or/And set starts with the second combo/textbox set
         'so if there's only one criteria specified, don't need to
         'concatenate additional stuff.
         If i > 0 Then
            If Me("opgClauseType" & i) = 1 Then
               strJoinType = " OR "
            Else
               strJoinType = " AND "
            End If
         End If
         'Get the a reference to the field in the table/Query as
         'we'll need it for BuildCriteria later on
         If Me.lstTables.Column(1) = "Table" Then
            Set tdf = db.TableDefs(Me.lstTables.Column(0))
            Set fld = tdf.Fields(Me("cbxFld" & i))
         Else
            Set rsQdf = db.OpenRecordset( _
               "Select * from [" & Me.lstTables.Column(0) & "] Where 1=2", dbOpenSnapshot)
            Set fld = rsQdf.Fields(Me("cbxFld" & i))
         End If
 
         'Only build a criteria if something's typed in the textbox
         'otherwise assume all records
 
         If (Me("txtVal" & i)) = "*" Then
 
 
 
                    For l = 0 To conMAXCONTROLS - 1
                    strWhere = strWhere & strJoinType & Application.BuildCriteria( _
                                                           "[" & Me("cbxFld" & i) & "]", _
                                                        fld.Type, Me("txtVal" & i) & "")
                    Next l
 
         Else         
                    If Not IsNull(Me("txtVal" & i)) Then
                       strWhere = strWhere & strJoinType & Application.BuildCriteria( _
                                                           "[" & Me("cbxFld" & i) & "]", _
                                                        fld.Type, Me("txtVal" & i) & "")
                    Else
                       strWhere = strWhere & strJoinType & "[" & Me("cbxFld" & i) & "]  like '*'"
                    End If
 
         End If         
      End If
   Next
   'The final all important SQL statement
   strSQL = strSQL & " from [" & Me.lstTables & "] Where " & strWhere
 
   'If the user has modified the SQL directly, take what they've typed in
   If Nz(Me.chkEditSQL, False) = False Then
      '"save" it in the textbox
      Me.txtSQL = strSQL
   End If
 
   With Me.lstResult
      Set rs = db.OpenRecordset(Me.txtSQL)
      'assign the SQL to the lstResult only if
      ' (a) it's valid (Set rs will generate an error otherwise)
      ' (b) if the recordset actually returned any records.
      If rs.RecordCount > 0 Then
         Me.cmdCopySQL.Enabled = True
         Me.cmdCreateQDF.Enabled = True
         Me.cmdExport.Enabled = True
         .RowSourceType = "Table/Query"
         .RowSource = Me.txtSQL
         .Enabled = True
         'display * fields
         .ColumnCount = CInt(Me.lstTables.Tag)
         .ColumnHeads = True
         Me.chkEditSQL.Enabled = True
      Else
        'Thanks for trying, better luck next time!!
         Me.cmdCopySQL.Enabled = False
         Me.cmdCreateQDF.Enabled = False
         Me.cmdExport.Enabled = False
         .ColumnCount = 1
         .RowSourceType = "Value List"
         .RowSource = "No records found."
      End If
   End With
ExitHere:
   Set rsQdf = Nothing
   Set rs = Nothing
   Set tdf = Nothing
   Set db = Nothing
   Exit Sub
ErrHandler:
   Select Case Err.Number
      'we're trying to open a parameter query
      Case 3061:
         MsgBox "The " & mconQ & Me.lstTables & mconQ & " query you've selected " _
            & " is a Parameter Query." & vbCrLf & Err.Description, vbExclamation + vbOKOnly, _
            "Missing parameters"
      Case Else:
         'Either invalid SQL or some other error
   End Select
   Me.cmdCopySQL.Enabled = False
   Me.cmdCreateQDF.Enabled = False
   With Me.lstResult
      .RowSourceType = "Value List"
      .RowSource = "Invalid SQL statement."
      .ColumnHeads = False
      .ColumnCount = 1
      .Enabled = False
   End With
   Resume ExitHere
End Sub
 
Function fListFill(ctl As Control, varID As Variant, lngRow As Long, _
                    lngCol As Long, intCode As Integer) As Variant
'The callback function for the first combo
'  sFillCombo takes care of the rest of 'em.
On Error GoTo ErrHandler
Static sastrObjSource() As String
Static sastrFields() As String
Static slngCount As Long
Static sdb As DAO.Database
Dim i As Long
Dim j As Long
Dim tdf As TableDef
Dim rsQdf As DAO.Recordset
Dim fld As DAO.Field
Dim varRet As Variant
Dim strObjectType As String
Dim varItem As Variant
 
   Select Case intCode
      Case acLBInitialize
         If sdb Is Nothing Then Set sdb = CurrentDb
         With Me
            ReDim sastrObjSource(0)
            'Are we looking for a table or a query
            sastrObjSource(0) = .lstTables.Column(0)
            strObjectType = .lstTables.Column(1)
            j = -1
            If strObjectType = "Table" Then
               Set tdf = sdb.TableDefs(sastrObjSource(0))
               Me.lstTables.Tag = tdf.Fields.Count
               'Get a list of all the fields
               For Each fld In tdf.Fields
                  j = j + 1
                  ReDim Preserve sastrFields(j)
                  sastrFields(j) = fld.Name
               Next
               j = UBound(sastrFields)
            Else
               'Since the fieldnames can be changed, safest way is to
               'open a recordset and go through it's Fields collection
               Set rsQdf = sdb.OpenRecordset( _
                     "Select * from [" & sastrObjSource(0) & "] Where 1=2", _
                     dbOpenSnapshot)
               Me.lstTables.Tag = rsQdf.Fields.Count
               For Each fld In rsQdf.Fields
                  j = j + 1
                  ReDim Preserve sastrFields(j)
                  sastrFields(j) = fld.Name
               Next
               j = UBound(sastrFields)
            End If
            'sort the string
            Call apiSortStringArray(sastrFields)
            slngCount = UBound(sastrFields) + 1
            'create a module level variant array for other combos
            mvarOriginalFields = sastrFields
         End With
         varRet = True
 
        Case acLBOpen
            varRet = Timer
 
        Case acLBGetRowCount
            varRet = slngCount
 
        Case acLBGetValue
            varRet = sastrFields(lngRow)
 
        Case acLBEnd
            Set rsQdf = Nothing
            Set tdf = Nothing
            Set sdb = Nothing
            Erase sastrFields
            Erase sastrObjSource
    End Select
    fListFill = varRet
ExitHere:
   Exit Function
ErrHandler:
   Resume ExitHere
End Function
 
Function fEnableNextInTab()
'Enable and Setfocus to the next control
'in the form's TabIndex.
Dim ctlNew As Control, intTab As Integer
Dim ctlOld As Control, intNewTab As Integer
 
   On Error Resume Next
   'Since we're calling this function from AfterUpdate,
   'what's the current control's position in TabIndex
   Set ctlOld = Screen.ActiveControl
   'we want the next one
   intNewTab = ctlOld.TabIndex + 1
 
   For Each ctlNew In Me.Controls
      intTab = ctlNew.TabIndex
      If Not Err And (intTab = intNewTab) Then
         'if no error occurred and the tab index is same as
         'what we're looking for, then enable it
         With ctlNew
            'Store the control's name for later use
            'but exclude the listbox since the tag there
            'contains the number of fields in the object select
            If Not ctlOld.ControlType = acListBox Then _
               ctlOld.Tag = .Name
            Select Case .ControlType
               Case acListBox:
               Case acComboBox:
                  'If the control found is a combo, fill it's rowsource
                  Call sFillCombo(Right(.Name, 1))
               Case Else:
 
            End Select
            .Enabled = True
            .Locked = False
            .BackColor = vbWhite
            .SetFocus
            Exit For
         End With
      End If
   Next
   Set ctlOld = Nothing
   Set ctlNew = Nothing
   'Build the SQL automatically only if the user specified so
   If Me.chkAutoBuildSQL = True Then Call sBuildSQL
End Function
 
Private Function fGetDocObjectProperty(strObjectName As String, _
                                    strObjectType As String, _
                                    strPropertyName As String) _
                                    As Variant
'?fGetDocObjectProperty("Module33","Modules","DateLastUpdated")
'
On Error GoTo ErrHandler
  Dim db As DAO.Database
  Dim doc As Document
  Dim ctr As Container
 
  Set db = CurrentDb
  Set ctr = db.Containers(strObjectType)
  Set doc = ctr.Documents(strObjectName)
 
  fGetDocObjectProperty = doc.Properties(strPropertyName)
ExitHere:
  Set doc = Nothing
  Set ctr = Nothing
  Set db = Nothing
  Exit Function
ErrHandler:
  fGetDocObjectProperty = Null
  Resume ExitHere
End Function
 
Private Function fSetDocObjectProperty(strObjectName As String, _
                            strObjectType As String, _
                            strPropertyName As String, _
                            varPropertyValue As Variant, _
                            Optional varPropertyType As Variant = dbText) _
                            As Boolean
'?fSetDocObjectProperty("Module33","Modules","DateLastUpdated",Now)
'
On Error GoTo ErrHandler
  Dim db As DAO.Database
  Dim doc As Document
  Dim ctr As Container
  Dim prop As Property
 
  Set db = CurrentDb
  Set ctr = db.Containers(strObjectType)
  Set doc = ctr.Documents(strObjectName)
 
  doc.Properties(strPropertyName).Value = varPropertyValue
  fSetDocObjectProperty = True
ExitHere:
  Set prop = Nothing
  Set doc = Nothing
  Set ctr = Nothing
  Set db = Nothing
  Exit Function
ErrHandler:
  Select Case Err.Number
    Case 3270:
      Set prop = doc.CreateProperty(strPropertyName, _
                            varPropertyType, varPropertyValue)
      doc.Properties.Append prop
      Resume Next
    Case Else:
      fSetDocObjectProperty = False
      Resume ExitHere
  End Select
  Resume ExitHere
End Function
 
Private Function ExportRoutine()
  Dim db As DAO.Database
  Dim qdf As DAO.QueryDef
  Dim lorst As DAO.Recordset
  Dim strName As String
  Dim strFile As String
  Const strSpecName = "~~TempSpec~~"
  On Error GoTo ExportRoutine_err
  With Me.lstResult
    strFile = DialogFile(OFN_SAVE, "Save file", "", .Column(3) & " (" & .Column(2) & ")|" & .Column(2), CurDir, .Column(2))
  End With
  If Len(strFile) > 0 Then
    'first get a unique name for the querydef object
    strName = Application.Run("acwzmain.wlib_stUniquedocname", "Query1", acQuery)
    Set db = CurrentDb
    Set qdf = db.CreateQueryDef(strName, Me.txtSQL)
    qdf.Close
    With lstResult
      Select Case .Column(0)
      Case 0 'Transferspreadsheet
        DoCmd.TransferSpreadsheet acExport, .Column(1), strName, strFile, True
      Case 1 'Transfertext
        If .Column(1) = acExportFixed Then
'Considerations
  'Do MsysImexColumns and MsysImexSpecs exist
  'Need to create if not
  'Can use Max Length on each field in query to get lengths for MsysImexSpecs
 
'          Set lorst = db.OpenRecordset(strName)
'Do loads of other stuff in here ...
'          DoCmd.TransferText .Column(1), , strName, strFile, True
        Else
          DoCmd.TransferText .Column(1), , strName, strFile, True
        End If
      End Select
    End With
  End If
ExportRoutine_end:
  On Error Resume Next
  DoCmd.DeleteObject acQuery, strName
  qdf.Close
  Set qdf = Nothing
  db.QueryDefs.Refresh
  Set db = Nothing
Exit Function
ExportRoutine_err:
  Resume ExportRoutine_end
End Function
Public Function DialogFile(wMode As Integer, szDialogTitle As String, szFileName As String, szFilter As String, szDefDir As String, szDefExt As String) As String
  Dim x As Long, OFN As OPENFILENAME, szFile As String, szFileTitle As String
  With OFN
    .lStructSize = Len(OFN)
    .hwnd = hWndAccessApp
    .lpstrTitle = szDialogTitle
    .lpstrFile = szFileName & String$(250 - Len(szFileName), 0)
    .nMaxFile = 255
    .lpstrFileTitle = String$(255, 0)
    .nMaxFileTitle = 255
    .lpstrFilter = NullSepString(szFilter)
    .nFilterIndex = 2
    .lpstrInitialDir = szDefDir
    .lpstrDefExt = szDefExt
    If wMode = 1 Then
      OFN.Flags = OFN_HIDEREADONLY Or OFN_PATHMUSTEXIST Or OFN_FILEMUSTEXIST
      x = GetOpenFileName(OFN)
    Else
      OFN.Flags = OFN_HIDEREADONLY Or OFN_OVERWRITEPROMPT Or OFN_PATHMUSTEXIST
      x = GetSaveFileName(OFN)
    End If
    If x <> 0 Then
      If InStr(.lpstrFile, Chr$(0)) > 0 Then
        szFile = Left$(.lpstrFile, InStr(.lpstrFile, Chr$(0)) - 1)
      End If
      DialogFile = szFile
    Else
      DialogFile = ""
    End If
  End With
End Function
 
'Pass a "|" separated string and returns a Null separated string
Private Function NullSepString(ByVal CommaString As String) As String
  Dim intInstr As Integer
  Const vbBar = "|"
  Do
    intInstr = InStr(CommaString, vbBar)
    If intInstr > 0 Then Mid$(CommaString, intInstr, 1) = vbNullChar
  Loop While intInstr > 0
  NullSepString = CommaString
End Function
sabinaS est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/09/2011, 13h02   #2
Rédacteur/Modérateur
 
Avatar de GAYOT
 
Homme Jean-Damien GAYOT
Inscription : novembre 2004
Messages : 2 075
Détails du profil
Informations personnelles :
Nom : Homme Jean-Damien GAYOT
Âge : 56
Localisation : France, Meuse (Lorraine)

Informations professionnelles :
Secteur : Distribution

Informations forums :
Inscription : novembre 2004
Messages : 2 075
Points : 4 385
Points : 4 385
Envoyer un message via Skype™ à GAYOT
Bonjour
Et as tu été voir là?
http://access.developpez.com/cours/?...#formrecherche
__________________
Plus j'avance et plus j'ai l'impression de ne rien savoir. Et comme j'essaie d'aller loin..!!.

Tutoriels sur:http://jdgayot.developpez.com

Pas de sujets techniques par Mp. Sinon
GAYOT est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 06/09/2011, 14h09   #3
Invité régulier
 
Femme
Inscription : juin 2011
Messages : 17
Détails du profil
Informations personnelles :
Sexe : Femme

Informations forums :
Inscription : juin 2011
Messages : 17
Points : 6
Points : 6
J'ai regardé ton lien, mais je n'ai toujours pas compris comment je peux effectuer la recherche sur plusieurs champs avec le même mot clé
sabinaS est déconnecté   Envoyer un message privé Réponse avec citation 00
Vieux 07/09/2011, 16h06   #4
Invité régulier
 
Femme
Inscription : juin 2011
Messages : 17
Détails du profil
Informations personnelles :
Sexe : Femme

Informations forums :
Inscription : juin 2011
Messages : 17
Points : 6
Points : 6
J'ai trouvé la solution en creant une nouvelle formulaire qui permet de faire la recherche avec plusieurs mots clés sur toute la table. Il y a un petit bug avec les cases à cocher, mais sinon ca fonctionne:
Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
Option Compare Database
Public strSearchResult As String
Private Sub cbo_Table_AfterUpdate()
 Me.txt_Critere.Value = ""
 Me.txt_Critere2.Value = ""
 Me.txt_Critere3.Value = ""
 Me.txt_Critere4.Value = ""
 Me.txt_Critere5.Value = ""
 Me.txt_Critere6.Value = ""
End Sub
 
Private Sub cmd_Find_Click()
 
Dim strTable As String, strField As String, strCriteria As String, strSql As String
    Dim Criter As Variant
 
 
 
  Dim tbl As TableDef
  Dim db As DAO.Database
  Set db = DBEngine(0)(0)
 
If Me.cbo_Table.Column(1) = "Table" Then
   Set tbl = db.TableDefs(Me.cbo_Table.Column(0))
    End If
 
    strTable = Me.cbo_Table         ' recupère le nom de la table
 
For Each fld In tbl.Fields
      strCriteria = strCriteria & strTable & "." & fld.Name & " Like '*" & Me.txt_Critere & "*' OR "
Next
strCriteria = "((" & Left(strCriteria, Len(strCriteria) - 3) & "))"
 
 
If Not IsNull(Me.txt_Critere2) Then
For Each fld In tbl.Fields
      strCriteria2 = strCriteria2 & strTable & "." & fld.Name & " Like '*" & Me.txt_Critere2 & "*' OR "
Next
If Me.opgClauseType2 = 1 Then
               strJoinType = " OR "
            Else
               strJoinType = " AND "
            End If
strCriteria = strCriteria & strJoinType & "((" & Left(strCriteria2, Len(strCriteria2) - 3) & "))"
End If
 
 
If Not IsNull(Me.txt_Critere3) Then
For Each fld In tbl.Fields
      strCriteria3 = strCriteria3 & strTable & "." & fld.Name & " Like '*" & Me.txt_Critere3 & "*' OR "
Next
If Me.opgClauseType3 = 1 Then
               strJoinType = " OR "
            Else
               strJoinType = " AND "
            End If
 
strCriteria = strCriteria & strJoinType & "((" & Left(strCriteria3, Len(strCriteria3) - 3) & "))"
End If
 
If Not IsNull(Me.txt_Critere4) Then
For Each fld In tbl.Fields
      strCriteria4 = strCriteria4 & strTable & "." & fld.Name & " Like '*" & Me.txt_Critere4 & "*' OR "
Next
If Me.opgClauseType4 = 1 Then
               strJoinType = " OR "
            Else
               strJoinType = " AND "
            End If
 
strCriteria = strCriteria & strJoinType & "((" & Left(strCriteria4, Len(strCriteria4) - 3) & "))"
End If
 
If Not IsNull(Me.txt_Critere5) Then
For Each fld In tbl.Fields
      strCriteria5 = strCriteria5 & strTable & "." & fld.Name & " Like '*" & Me.txt_Critere5 & "*' OR "
Next
If Me.opgClauseType5 = 1 Then
               strJoinType = " OR "
            Else
               strJoinType = " AND "
            End If
 
strCriteria = strCriteria & strJoinType & "((" & Left(strCriteria5, Len(strCriteria5) - 3) & "))"
End If
 
If Not IsNull(Me.txt_Critere6) Then
For Each fld In tbl.Fields
      strCriteria6 = strCriteria6 & strTable & "." & fld.Name & " Like '*" & Me.txt_Critere6 & "*' OR "
Next
If Me.opgClauseType6 = 1 Then
               strJoinType = " OR "
            Else
               strJoinType = " AND "
            End If
 
strCriteria = strCriteria & strJoinType & "((" & Left(strCriteria6, Len(strCriteria6) - 3) & "))"
End If
 
    ' construit la requête sql
    strSql = "SELECT DISTINCTROW " & strTable & ".* "
    strSql = strSql & " FROM " & strTable
    strSql = strSql & " WHERE  " & strCriteria & ";"
 
 
    Me.lst_Resultat.RowSource = strSql  ' affecte sql a lst_Resultat
    Me.lst_Resultat.Requery             ' recalcule la liste
 
strSearchResult = strSql
 
End Sub
 
Private Sub Command29_Click()
DoCmd.Close acQuery, ("SearchResults"), acSaveNo
CurrentDb.QueryDefs.Delete ("SearchResults")
Set qdf = CurrentDb.CreateQueryDef("SearchResults", strSearchResult)
DoCmd.OpenQuery ("SearchResults")
End Sub
sabinaS est déconnecté   Envoyer un message privé Réponse avec citation 00
Réponse Proposer ce sujet en actualité Cette discussion est résolue.
Outils de la discussion



Fuseau horaire GMT +2. Il est actuellement 21h56.


 
 
 
 
Partenaires

Hébergement Web