You have more values (8) than fields (6). You have Test through TestE, but your values go from Range("A5") to Range("H5"). Your field list also ends in a comma.
I change the code but now the reads Invalid SQL statement; expected 'DELETE', 'INSERT', 'Procedure', 'SELECT', or 'UPDATE'
at this line of code: cnn.Execute (sql)
Code change is following:
ub CopyToAccessTable()
Dim sql As String, accFilePath As String
Dim cnn As ADODB.Connection
accFilePath = ("\\DTCHYB-ILSP001\C_MTG_Groups\Mandatory\Analysts - Working Files\Carol\Test\Expert\TestAccess.accdb")
MsgBox sql
Check it for screwed up quotes.
It must contain quotes in pairs around each string value.
This is why I called your string a horror.
There's no good way to check for errors.
Build up the string element by element and you can debug the building of the string.
Gustav Brock
I change the code but now the reads Invalid SQL statement; expected 'DELETE', 'INSERT', 'Procedure', 'SELECT', or 'UPDATE'
Of course. You garbled the SQL beyond any valid syntax.
/gustav
Nick67
You left out most of my code!
Let's fix it
Sub CopyToAccessTable()Dim sql As String,Dim accFilePath As StringDim cnn As ADODB.ConnectionDim MyValA as StringDim MyValB as StringDim MyValC as StringDim MyValD as StringDim MyValE as StringDim MyValF as StringDim MyValG as StringDim MyValH as String accFilePath = ("\\DTCHYB-ILSP001\C_MTG_Groups\Mandatory\Analysts - Working Files\Carol\Test\Expert\TestAccess.accdb") MyValA = Range("A5") MyValB = Range("B5") MyValC = Range("C5") MyValD = Range("D5") MyValE= Range("E5") MyValF = Range("F5") MyValG = Range("G5") MyValH = Range("H5") sql = "INSERT INTO ExcelToAccess ([Test], [TestA], [TestB], [TestC], [TestD], [TestE], [TestF], [TestG]" sql = "Values " & MyValA & "," & MyValB & "," & MyValC & "," & MyValD & "," & MyValE & "," & MyValF & "," & MyValG & "," & MyValH Set cnn = New ADODB.Connection cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & accFilePath & ";Persist Security Info=False;" If Not (cnn Is Nothing) Then 'Execute Sql cnn.Execute (sql) 'Close cnn.Close End If Set cnn = Nothing End Sub
If Not (cnn Is Nothing) Then
'Execute Sql
Debug.Print sql
cnn.Execute (sql)
'Close
cnn.Close
End If
and study the output. Then correct the code.
/gustav
ca1358
ASKER
Eorr now
Syntax errpt in INSERT INTO statement
Gustav Brock
The Debug command doesn't correct anything, it just prints in the immediate windows (Ctrl+G) the sql that you will be about to execute.
Study the output, and then correct the code until the sql contains correct syntax.