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
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