Using ADO with INSERT query

T

todtown

I'm trying to do this in VBA:

Dim cn As New ADODB.Connection
Dim sql As String

cn.Open "Driver={SQL
Server};Server=servername;Database=dbname;Uid=userid;Pwd=password"

sql = "INSERT INTO table_name " & _
"(field_1,field_2,field_3,field_4) " & _
"Values ('value1','value2','value3','value4')"
cn.Execute sql

I get an automation error with the Execute method. However, if I run
the query from Query Analyzer it works. So, I'm pretty sure the
problem is either I'm not setting up the sql string properly to use
with cn.Execute, or I'm using the wrong ADO objects/methods.

How do I do this correctly?

tod
 

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