Using variable in SQL string (1 Viewer)

LjushaMisha

Registered User.
Local time
Tomorrow, 00:23
Joined
Mar 10, 2017
Messages
55
Hello. I have a PEACE of code looking like that:

Dim ElKup1 As String
Dim sq As String



ElKup1 = (Forms!frmNotInList!Combo6.Column(1)) & " " & Forms!frmNotInList!Combo0.Column(1)

sq = "Update tblElements Set ElementName = ElKup1 WHERE ElementName=null;"

DoCmd.RunSQL sq

When I run the sub I (F5 or OnClick Event) get pop window asking me to input parameter's ElKup1 value.
What I'm doing wrong? Can Somebody help me?
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:23
Joined
May 7, 2009
Messages
19,247
since ElKup1 is string you must enclosed them with double aphostraphe in the expression.
also your construction of variable sq, is missing ampersand (&).
to compare variable to null, use Is Null or the function IsNull:

sq = "Update tblElements Set ElementName = " & Chr(34) & ElKup1 & Chr(34) & " Where ElementName Is Null;"

or

sq = "Update tblElements Set ElementName = " & Chr(34) & ElKup1 & Chr(34) & " Where IsNull(ElementName);"

DoCmd.RunSql sq
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 06:23
Joined
May 7, 2009
Messages
19,247
By the way what dies Misha means?
 

LjushaMisha

Registered User.
Local time
Tomorrow, 00:23
Joined
Mar 10, 2017
Messages
55
Hi Arnelgp.
GREAT THANKS
WORKED PERFECTLY FIRST TIME.

Just question: Why you used char(34) instead simply "

Best regards
 

Users who are viewing this thread

Top Bottom