lol !

Comment transmettre correctement une variable string (Dim ville) correctement à ma requête SQL ?
Mon message d'erreur est que les enregristrements ne correspondent pas ) mes options de requêtes .

La problématique de fond est la suivante :

En fonctions du nom de la ville de mon document une variable string avec un code correspondant au nom de la ville est chargée .Ensuite dans une table access et dans une colonnes toutes les villes sont mélangées.
Puis j'essaie de lancer un tri sur la ville qui m'interresse .Voici comment je procède :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
Private Sub CommandButton1_Click()
 
 '*************************
 
        Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "0510"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=0 
 
    Dim ville As String
 
    If Selection.Text = "0510" Then  
 
    Selection.TypeText Text:="Saint Laurent"
 
    ville = "SL51"
 
    End If
 
 
 
        Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "0110"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.MoveRight Unit:=wdCharacter, Count:=0 
 
 
    If Selection.Text = "0110" Then  
    Selection.TypeText Text:="Bapaume"
    ville = "B11"
    End If
 
 
'*******
 
 
Dim DB As Database
Dim RS As Recordset
Dim qdf As DAO.QueryDef
Dim rcs As DAO.Recordset
 
Set DB = OpenDatabase("C:\Documents and Settings\a\Bureau\gesaf.mdb")
Set RS = DB.OpenRecordset("CTR et CV")
 
 
 
RS.Index = "NumCTR"
RS.MoveLast 'pour se mettre sur le dernier enregistrement
MsgBox RS("NumCTR")
 
 
 
With Selection  
    .Collapse Direction:=wdCollapseEnd
    .Range.InsertDatabase _
        Style:=64, _
        LinkToSource:=False, Connection:="TABLE CTR et CV", _
        SQLStatement:="SELECT [NumCTR] FROM [CTR et CV] WHERE [CTR et CV]![NumCTR] LIKE '" & Replace(ville, "'", "''") & "' ", _
        DataSource:="C:\Documents and Settings\a\Bureau\gesaf.mdb"
End With
 
 
    RS.Close
    DB.Close
 
 
'Désallocation mémoire
Set DB = Nothing
Set RS = Nothing
 
End Sub
ça bugue ici

SQLStatement:="SELECT [NumCTR] FROM [CTR et CV] WHERE [CTR et CV]![NumCTR] LIKE '" & Replace(ville, "'", "''") & "' ",