Exporting contents of range to Access...HELP!

W

WhytheQ

I've got the following code. What I'm trying to is export the contents
of a range into an Access table called Scorers. The range C22:E22
contains 'fred','2','2' which correspond to the fields name/age/score.
An error occurs when creating the SQL string. Do I need to use a loop
and individually move each bit of data?

Any help greatly appreciated
Jason



Sub AddToAccess()

Dim cnAccess As ADODB.Connection
Dim sConnect As String
Dim sSQL As String


'create connection string
sConnect = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & ThisWorkbook.Path & "\Access
Experiment.mdb"

'create SQL string
sSQL = "INSERT INTO Scorers (name,age,score) VALUES (" &
Application.Transpose(Range("C22:E22")) & ";" 'Range("C22:E22") & ";"
'('skelly','2','2');"

'make connection and get get the SQL query recordset
Set cnAccess = New ADODB.Connection
cnAccess.ConnectionString = sConnect
cnAccess.Open
cnAccess.Execute sSQL, , adCmdText + adExecuteNoRecords

'close the connection
cnAccess.Close
Set cnAccess = Nothing

End Sub
 

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