Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.misc
Secret Squirrel
 
Posts: n/a
Default ADO Transfer from Excel to Access

Ok I've figured out some code to transfer data to Access but I can't figure
out how to make this loop for different records. As you can see in my code it
is transferring data for record 19. How do I add another statement to be able
to do this for another record?
Sub ADOFromExcelToAccesss()
' exports data from the active worksheet to a table in an Access database
' this procedure must be edited before use
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "DRIVER={Microsoft Access Driver (*.mdb)}; " & _
"DBQ=C:\Documents and Settings\My Documents\Work Databases\BC Prod Stds &
Calcs.mdb"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "tblSurcharge", cn, adOpenKeyset, adLockOptimistic, adCmdTable
With rs
strSQL = "select * from tblSurcharge where SurchargeID = '19'"
On Error Resume Next
..Open strSQL, cn, adOpenKeyset, adLockOptimistic, adCmdText
On Error GoTo 0
If .State = adStateOpen Then ' successfully opened the recordset
If .EOF Then ' no records returned

Else ' one (or more records returned)
' edit existing record
..Fields(1) = Range("D" & 25).Value
..Update ' stores the new record
End If
..Close ' close the recordset
End If
End With
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub


Additional Info:

It seems to only work if the first "SurchargeID" is used. For example, my
first "SurchargeID" is '19' so it updates fine but if I change that '19' to
'20' it won't work. How do I have it look through the records to find the
right ID?

Reply
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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Financial modelling from Excel to Access Huyeote Excel Discussion (Misc queries) 3 March 9th 11 03:59 PM
Transfer data from Excel to Access Secret Squirrel Excel Discussion (Misc queries) 1 March 27th 06 12:07 AM
Excel Access and Oracle Chris K Excel Discussion (Misc queries) 1 February 17th 06 06:16 PM
TRYING TO SET UP EXCEL SPREADSHEET ON MY COMPUTER MEGTOM New Users to Excel 5 October 27th 05 03:06 AM
Excel Spreadsheet from Access. List of names changes as names are Gordy w/Hi Expectations Excel Discussion (Misc queries) 1 October 21st 05 03:30 AM


All times are GMT +1. The time now is 02:46 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"