INSERT INTO Help needed please

Mike Leigh

Registered User.
Local time
Today, 00:56
Joined
Feb 5, 2001
Messages
11
Hi,

This must be really simple to do. I am trying to run an insert into from vba while taking the values from a form.

DoCmd.RunSQL "INSERT INTO company_categories (company_name_id,category_name_id) SELECT '2','3'", -1

The above code works however the text 2 and 3 I have to place manually in here because I do not remember how to place them in a variable. Please help

e.g.

Dim test2 as string

The code now reads for me
Dim test2 as string
test2 = 4
DoCmd.RunSQL "INSERT INTO company_categories (company_name_id,category_name_id) SELECT test2,'3'", -1

This gives me a message box asking what value test2 should be and I have allready set test2 to be 4 in the above.

Thanks in advance
 
Dim test2 as string
dim MySQL as string
test2 = 4

MySQL = "INSERT INTO company_categories (company_name_id,category_name_id) SELECT '" & test2 & "','" & 3 & "'"

DoCmd.RunSQL MySQL,-1

You have to concatenate the variable into the string.. I also seperated your criteria string from the RunSQL.. You should always have it seperate, that way it's easier to troubleshoot if you have a problem. Hope this helps.

Doug
 
thanks for the help...

no doubt I will post some more soon :-)

[This message has been edited by Mike Leigh (edited 07-22-2001).]

[This message has been edited by Mike Leigh (edited 07-22-2001).]
 

Users who are viewing this thread

Back
Top Bottom