What you want is for your SQL statement to ultimately look like this:
UPDATE candidatures
SET offre_ID = 42
,candidat_Nom = 'Doe'
,candidat_Prenom = 'John'
,manager_Nom = 'Nuts'
,manager_Prenom = 'Doug'
,reponse_Manager = NULL
,reponse_Agent = NULL
,region_Candidat = 'WEST'
,um_Candidat = 72
,dum_Candidat = 72
,nni_Candidat = 24
,emploi_Candidat = 'something'
,accompagnement_Dispense = 'whatever'
,accompagnement_Precision_ID = 10
,accompagnement2_Precision_ID = 11
WHERE candidature_ID = 2345;
You want strings to be enclosed in single quotes, numerical values to not be enclosed in such single quotes, and NULL
values specified literally.
You could keep your string-concatenation approach, and make the query contain NULL
values by concatenating "NULL"
string literals into your query:
If repA = -1 Then
repAVar = "NULL"
Else
repAVar = repA
End If
If repM = -1 Then
repMVar = "NULL"
Else
repMVar = repM
End If
Obviously this means dealing with when/whether to include the single quotes, and when not to.
And that might work... until it doesn't:
UPDATE candidatures
SET offre_ID = 42
,candidat_Nom = 'O'Connor'
,candidat_Prenom = 'David'
...
I know! I'll simply double-up any single quote in my string values! And that might work:
UPDATE candidatures
SET offre_ID = 42
,candidat_Nom = 'O''Connor'
,candidat_Prenom = 'David'
...
But go down that hill and you're in for a ride... you'll keep patching up and "sanitizing" the user's input until things work again, then they break down, and you patch it again...
There's a sane way to do this.
Use parameters, and let the server deal with the parameters.
So instead of concatenating the parameter values into the command string, you send this to the server:
UPDATE candidatures
SET offre_ID = ?
,candidat_Nom = ?
,candidat_Prenom = ?
,manager_Nom = ?
,manager_Prenom = ?
,reponse_Manager = ?
,reponse_Agent = ?
,region_Candidat = ?
,um_Candidat = ?
,dum_Candidat = ?
,nni_Candidat = ?
,emploi_Candidat = ?
,accompagnement_Dispense = ?
,accompagnement_Precision_ID = ?
,accompagnement2_Precision_ID = ?
WHERE candidature_ID = ?;
..along with parameters.
You don't work with a Recordset
. You work with a Command
instead:
Dim connect As ADODB.Connection
Set connect = New ADODB.Connection
connect.ConnectionString = connString
connect.Open
With New ADODB.Command
.ActiveConnection = connect
.CommandType = adCmdText
.CommandText = sqlQuery
'append parameters in the same order they show up in the query
.Parameters.Append .CreateParameter(Type:=adInteger, Direction:=adParamInput, Value:=myValue)
'...
.Execute
End With
connect.Close
To give the parameter a NULL
value, simply use the Null
keyword:
myValue = Null
This requires your nullable values to have a Variant
data type, because an Integer
or Boolean
can't be Null
in VBA.