Bonjour.
Dans VBA, le délimiteur de texte est OBLIGATOIREMENT le guillemet (").
Ici ton code corrigé.
Set ligne = base.OpenRecordset("SELECT * FROM Clients WHERE N°_client=" & liste_clients.Value, dbOpenDynaset)
Dans SQL tu peux utiliser au choix le guillemet (") ou l'apostrophe (').
Perso, je préfère le guillemet qui est moins fréquent que l'apostrophe en français.
Attention, si tu as besoin d'un guillemet dans une chaîne en VBA, il faut le doubler.
Voici un exemple :
1 2 3 4
| dim critereTexte as string
dim variableTexte as string
critereTexte="[ChampTexte]=""" & VariableTexte & """" |
en passant pour rendre ton code plus lisible, indente les "blocs".
Ça ressemble à cela :
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
| Private Sub creer_client_Click()
Dim base As Database: Dim ligne As Recordset
Dim nb_clients As Byte: Dim requete As String
If (civilite.Value <> "" And nom_client.Value <> "" And prenom_client.Value <> "") Then
Set base = Application.CurrentDb
Set ligne = base.OpenRecordset("SELECT COUNT(N°_client) AS nb_client FROM Clients WHERE nom_client=" & nom_client.Value & " AND prenom_client=" & prenom_client & "", dbOpenDynaset)
ligne.MoveFirst
nb_clients = ligne.Fields('nb_client').Value
If (Int(nb_clients > 0)) Then
MsgBox ('Le client existe déjà, il ne peut donc être créé une deuxième fois')
Else
requete = "INSERT INTO Clients (civilite_client,nom_client,prenom_client) VALUES (""" & civilite.Value & """, """ & nom_client.Value & """, """ & prenom_client.Value &""")"
base.Execute requete
MsgBox 'Le client a été créé avec succès'
DoCmd.Requery
liste_clients = liste_clients.ItemData(liste_clients.ListCount - 1)
n_client.Value = liste_clients.Value
ligne.Close
base.Close
Set ligne = Nothing
Set base = Nothing
End If
Else
MsgBox 'Pour créer un nouveau client, toutes les informations des champs doivent être renseignées'
End If
End Sub |
A+
Partager