Adding a variable to a string

Steveanderson

Registered User.
Local time
Yesterday, 23:14
Joined
May 27, 2010
Messages
16
I'm using Access 2003 and I'm trying to configure a combobox to add the combobox value to a table named listeanalyse
What is the proper synthax to add the variable A to the SQL string ?
Here is the code.


Private Sub Combobox1_Click()
Dim A As String, SqlTxt As String
A = Combobox1.Value
SqlTxt = "INSERT INTO ListeAnalyse (Analyse) VALUES ('"&A&"') ;"
DoCmd.RunSQL SqlTxt
End Sub

Thanks
 
Code:
Private Sub Combobox1_Click()
Dim A As String, SqlTxt As String
A = [COLOR=red]Me.[/COLOR]Combobox1
SqlTxt = "INSERT INTO ListeAnalyse (Analyse) VALUES ('"&A&"') ;"
DoCmd.RunSQL SqlTxt
End Sub

Try and use Form refrence .

Alternative
A = Forms!NameOfForm!NameOfControl

JR
 
Thanks but I think that there is an error in line 4 of my code ... VALUES ('"&A&"') could be the problem.
How could I insert the value of the combobox variable in a table ?
 
You have to make sure to include spaces.

SqlTxt = "INSERT INTO ListeAnalyse (Analyse) VALUES ('" & A & "')"

It doesn't seem to like it if you omit those spaces around the ampersands.

(and you don't need the semi-colon at the end - just an FYI)
 
Also you should check to see if combobox1 has a value BEFORE you run the insert statement.

Code:
Private Sub Combobox1_Click()
If Not IsNull Me.Combobox1 Then
     .. Do the Insert
End If
End Sub

JR
 
Thanks guys Boblarson nailed it!
I did a mistake by not adding the spaces
 

Users who are viewing this thread

Back
Top Bottom