happyhungarian
Active Member
- Joined
- Jul 19, 2011
- Messages
- 252
- Office Version
- 365
- Platform
- Windows
Hi all,
I've been using the following code to send data from my excel workbook to an access database. The thing I wanted to change about it (and hopefully you all can help) is that currently it sends data from the currently active excel sheet. I would like to add a line of code in there where I can tell it what sheet sheet to pull from.
Sub ADOFromExcelToAccess()
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Documents and Settings\saundersj\Desktop\test.mdb;"
Set rs = New ADODB.Recordset
rs.Open "Aircraft_Delivery", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 8 ' the start row in the worksheet
Do While Len(Range("t" & r).Formula) > 0
With rs
.AddNew ' create a new record
.Fields("Delivery Date") = Range("t" & r).Value
.Fields("Award/Revision") = Range("u" & r).Value
.Fields("Opportunity") = Range("v" & r).Value
.Fields("Deliveries") = Range("w" & r).Value
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub
I've been using the following code to send data from my excel workbook to an access database. The thing I wanted to change about it (and hopefully you all can help) is that currently it sends data from the currently active excel sheet. I would like to add a line of code in there where I can tell it what sheet sheet to pull from.
Sub ADOFromExcelToAccess()
Dim cn As ADODB.Connection, rs As ADODB.Recordset, r As Long
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Documents and Settings\saundersj\Desktop\test.mdb;"
Set rs = New ADODB.Recordset
rs.Open "Aircraft_Delivery", cn, adOpenKeyset, adLockOptimistic, adCmdTable
' all records in a table
r = 8 ' the start row in the worksheet
Do While Len(Range("t" & r).Formula) > 0
With rs
.AddNew ' create a new record
.Fields("Delivery Date") = Range("t" & r).Value
.Fields("Award/Revision") = Range("u" & r).Value
.Fields("Opportunity") = Range("v" & r).Value
.Fields("Deliveries") = Range("w" & r).Value
.Update ' stores the new record
End With
r = r + 1 ' next row
Loop
rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing
End Sub