Updating Access table from Excel

thall1974

New Member
Joined
Jan 28, 2011
Messages
2
I have an excel workbook that is a DB query from Access. The users send this "report" out to their vendors for updates. I want them to be able to import the data back into access and run the update queries from the Excel workbook. I "THINK" what I need is VBA code that will copy the open workbook, reach out to access and paste append the data into a staging table. Then run the update query in Access to complete the updates. Does anyone have a better solution? I have also never had excel talked to access this way. Any help would be greatly appreciated!! :)
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I had a similar problem but I only have one user and one sheet. Here is the code:

/*

Sub AlterAllRecords()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim fld As ADODB.Field
Dim MyConn
Dim lngRow As Long
Dim lngID, LR, Upd
Dim j As Long
Dim sSQL As String

LR = Range("A" & Rows.Count).End(xlUp).Row
Upd = LR - 6

lngRow = 6
Do While lngRow <= LR


lngID = Cells(lngRow, 1).Value

sSQL = "SELECT * FROM Base WHERE (((Base.[PO])=" & "'" & lngID & "'" & "));"



Set cnn = New ADODB.Connection
'MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB
MyConn = "Provider = Microsoft.ACE.OLEDB.12.0;" & _
"Data Source = S:\Public Folder\DanielleSN\Steel Needs 1209.accdb"
With cnn
' .Provider = "Microsoft.Jet.OLEDB.4.0"
.Provider = "Microsoft.ACE.OLEDB.12.0"
.Open MyConn

End With

Set rst = New ADODB.Recordset
rst.CursorLocation = adUseServer
rst.Open sSQL, ActiveConnection:=cnn, _
CursorType:=adOpenKeyset, LockType:=adLockOptimistic

'Load all records from Excel to Access.

' rst(Cells(1, j).Value) = Cells(lngRow, j).Value
'Next j
With rst
.Fields("PO") = Cells(lngRow, 1).Value
.Fields("Vendor") = Cells(lngRow, 2).Value
.Fields("ShiptoName") = Cells(lngRow, 3).Value
.Fields("ShiptoADDR") = Cells(lngRow, 4).Value
.Fields("ShiptoCITY") = Cells(lngRow, 5).Value
.Fields("SHIPVIA") = Cells(lngRow, 6).Value
.Fields("FOB") = Cells(lngRow, 7).Value
.Fields("WEIGHT") = Cells(lngRow, 8).Value
.Fields("GRADE") = Cells(lngRow, 9).Value
.Fields("GAUGE") = Cells(lngRow, 10).Value
.Fields("WIDTH") = Cells(lngRow, 11).Value
.Fields("LENGTH") = Cells(lngRow, 12).Value
.Fields("COST-MATL") = Cells(lngRow, 13).Value
.Fields("SlitTo-Part") = Cells(lngRow, 14).Value
.Fields("P-ODate") = Cells(lngRow, 15).Value
.Fields("CUSTOMER") = Cells(lngRow, 16).Value
.Fields("TagDesc") = Cells(lngRow, 17).Value
.Fields("Rockwell") = Cells(lngRow, 18).Value
.Fields("Vendor Acknowledgement") = Cells(lngRow, 19).Value
.Fields("Po Status and/or Update") = Cells(lngRow, 20).Value
.Fields("Estimated ready date") = Cells(lngRow, 21).Value
.Fields("Released by vendor?") = Cells(lngRow, 22).Value
.Fields("POStatus") = Cells(lngRow, 23).Value


rst.Update
End With

' Close the connection
rst.Close
cnn.Close
Set rst = Nothing
Set cnn = Nothing

lngRow = lngRow + 1

Loop
MsgBox "You just updated " & Upd & " records"

End Sub
*/

What I do is count the number of rows (LR - 6). I have header records in the first 6 rows.

Then I set up the sql statement that will locate the single record that I want to update.

Now you open the database and recordset

Then I update the records with each cell. I basically establish a relationship between a cell and a field in Access. Once it is all built then it updates.

I keep looping until I have reached the last row and I close. I added a you just updated X number of records but I am finding it is more annoying than helpful.

You need to close the connection and reopen it every time. I am usually updating about 100 - 200 records and it takes about 4 seconds. I was trying to find out if it needs to be updated and then found it was easier to just do them all.

HTH
 
Upvote 0
Thank you for your help! I think I understand what your code is doing. However I can't get past the first two lines before it errors out. I am in Excel 2003 do I need a special reference Library?
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top