LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.worksheet.functions
Secret Squirrel
 
Posts: n/a
Default Error: Number of query values and destination fields are not the s

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
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:16 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"