Insert into SQl table

S

Striker

I am trying to insert into a SQL table the results of some variables. Let's
say I have variables called; PHONE,LNAME,FNAME and I want to insert them
into a table called CONTACTS with Field names (FIRST, LAST, TELEPHONE)

What syntax can I use to insert the value from these variables into the
table? Also if I do not provide a SQL UID/Password, will it attempt to use
the network information form the person logged in? Some of the code I have
come up with is below. Just seem to be missing the insert query part of it.



~~~~~~~~Code to open connection~~~~~~~~~~~~~~~~~~
'Dim MyConnection As ADODB.Connection
'Dim MySQL As String
'Set MyConnection = New ADODB.Connection
'MyConnection.Open "driver=SQL
Server;server=sql01;DATABASE=Sta01l;UID=;PWD=;"

INSERT QUERY STATEMENT HERE

'MyConnection.Execute MySQL
'MyConnection.Close
'Set MyConnection = Nothing

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
A

Andrew Taylor

The syntax for inserting the record looks like this
(optional line breaks added for readability):

INSERT INTO CONTACTS
(FIRST, LAST, TELEPHONE)
VALUES ('John','Smith','0123456789')

(NB single quotes)
So you would have to say:

MySQL = " INSERT INTO CONTACTS " _
& " (FIRST, LAST, TELEPHONE) " _
& "VALUES ('" & FNAME & "','" _
& LNAME & "','" & PHONE & "')


To be safe you should handle the possibility of
apostrophes in the names (e.g. O'Reilly) by
using Replace(LNAME, "'","''") etc .

I'm not sure about the password thing off hand. Try it and see!


hth
Andrew
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top