Error: Number of query values and destination fields are not the s

  • Thread starter Secret Squirrel
  • Start date
S

Secret Squirrel

I'm trying to upload data from excel to access but I'm getting this error
message:

Number or query values and destination fields are not the same.

Here is the code I am trying to run. Can anyone help me?

Sub UploadData()

Dim MyCn As ADODB.Connection
Dim SQLStr As String
Dim i As Long
Dim r As Long
Dim delRows As Range

Set MyCn = New ADODB.Connection

MyCn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & _
"DBQ=C:\Documents and Settings\owner\My Documents\BC\Quality Action
Database.mdb;SystemDB=C:\Documents and Settings\owner\My
Documents\BC\sys.mdw;" & _
"Uid=admin;" & _
"Pwd=password;"
' Replace actual Access file path here
' Note: If database has userID and password, need to specify them here also
' by appending "; UID=MyUserID; PWD=MyPassword"
' - if trouble accessing the file do a net search for help on Connection
Strings

i = Cells(Rows.Count, 1).End(xlUp).Row

For r = 10 To i '<<Change Start Row
SQLStr = "INSERT INTO [tblOctExt] " _
& "Values ('" & Range("A" & r).Value & "', '" _
& Range("B" & r).Value & "', '" _
& Range("C" & r).Value & "', '" & Range("D" & r).Value _
& "', '" & Range("E" & r).Value & "', '" _
& Range("F" & r).Value & "', '" & Range("G" & r).Value _
& "', '" & Range("H" & r).Value & "', '" _
& Range("I" & r).Value & "', '" & Range("J" & r).Value _
& "', '" & Range("K" & r).Value & "', '" _
& Range("L" & r).Value & "', '" & Range("M" & r).Value _
& "', '" & Range("N" & r).Value & "', '" & Range("O" & r).Value &
"')"

' NOTE: The above assumes all fields are TEXT data type, that is why the "'"s;
' might have trouble with other data types unless you match the format
expected
' by the database

MyCn.Execute SQLStr

If delRows Is Nothing Then
Set delRows = Range("A" & r)
Else
Set delRows = Union(delRows, Range("A" & r))
End If

Next r

delRows.EntireRow.Delete

MsgBox "Data has been uploaded to tblOctExt"
MyCn.Close
Set MyCn = 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