close

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 quot;DRIVER={Microsoft Access Driver (*.mdb)}; quot; amp; _
quot;DBQ=C:\Documents and Settings\My Documents\Work Databases\BC Prod Stds amp;
Calcs.mdbquot;
' open a recordset
Set rs = New ADODB.Recordset
rs.Open quot;tblSurchargequot;, cn, adOpenKeyset, adLockOptimistic, adCmdTable
With rs
strSQL = quot;select * from tblSurcharge where SurchargeID = '19'quot;
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(quot;Dquot; amp; 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 SubAdditional Info:

It seems to only work if the first quot;SurchargeIDquot; is used. For example, my
first quot;SurchargeIDquot; 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?

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 software 的頭像
    software

    software

    software 發表在 痞客邦 留言(0) 人氣()