Re: Update multiple records in access from Excel also close access when done
Hello.
Do you specifically need Access to be open to do this? If not you can just open a connection and update the table using an update statement. You need to add a reference to Microsoft ActiveX Data Objects 2.8 Library for this solution (if you haven't already added it). This solution does not require the access database to open at all (only a connection to it).
Public stCon As String
Public stDB As String
Public stPass As String
Public strSQL As String
Public cnt As ADODB.Connection
Public rst As ADODB.Recordset
Sub OpenConn()
Set cnt = New ADODB.Connection
Set rst = New ADODB.Recordset
stDB = "C:\Users\n576546\Desktop\Tech Review Reporting.accdb"
stPass = "saloba3415"
'Create the connectionstring - After http://www.connectionstrings.com/access-2007
stCon = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & stDB & ";" _
& "Jet OLEDB:Database Password=stPass;"
With cnt
.Open stCon
.CursorLocation = adUseClient
End With
End Sub
Sub CloseConn()
If CBool(rst.State And adStateOpen) = True Then rst.Close
Set rst = Nothing
If CBool(cnt.State And adStateOpen) = True Then cnt.Close
Set cnt = Nothing
End Sub
Sub UpdateDatabase()
Dim intRows As Integer
OpenConn
Do While Cells(intRows, 1) > 0
strSQL = "update [Daily Reconciliation] set " _
& "[Bank Account Name] = '" & Cells(intRows, 2) & "', " _
& "[Date Final Revision Completed] = " & Cells(intRows, 4).Value _
& "[Tech Reviewer] = " & Cells(intRows, 5).Value _
& "[Technical Review Date] = " & Cells(intRows, 6).Value _
& "[Final Technical Revision Completed] = " & Cells(intRows, 7).Value _
& "[Returned Date] = " & Cells(intRows, 8).Value _
& "[Technical Completed Correctly] = " & Cells(intRows, 9).Value _
& "[Comments] = " & Cells(intRows, 10).Value _
& "[Tech Review Score] = " & Cells(intRows, 11).Value _
& "[Header] = " & Cells(intRows, 12).Value _
& "[Bank Stmt Bal] = " & Cells(intRows, 12).Value _
& "[Signature] = " & Cells(intRows, 14).Value _
& "[Outstanding Items match detail] = " & Cells(intRows, 15).Value _
& "[In Transit Item] = " & Cells(intRows, 16).Value _
& "[Ending Balance zero] = " & Cells(intRows, 17).Value _
& "[Calculator Tape] = " & Cells(intRows, 18).Value _
& "[FontsSizeColumnWidths] = " & Cells(intRows, 19).Value _
& "[Dates/Aging/% Items] = " & Cells(intRows, 20).Value _
& "[Department for Referral] = " & Cells(intRows, 21).Value _
& "[ReadablePrintout] = " & Cells(intRows, 22).Value _
& "[Referral backup accurate] = " & Cells(intRows, 23).Value _
& "[Offsets] = " & Cells(intRows, 24).Value _
& "[Items loaded to matrix] = " & Cells(intRows, 25).Value _
& "[Items > 6 days referred] = " & Cells(intRows, 26).Value
strSQL = strSQL & "[Items > 10 days referred] = " & Cells(intRows, 27).Value _
& "[Items > 15 days referred] = " & Cells(intRows, 28).Value _
& "[Items > 30 days referred] = " & Cells(intRows, 29).Value _
& "Where [Account Number] = " & Cells(intRows, 1).Value & " AND [Date of Reconciliation] = '" & Format(Cells(intRows, 3).Value, "MM/dd/yyyy") & "'"
'Date format must use uppercase MM. Lowercase mm will use minutes, not month
rst.Open strSQL, cnt, adOpenStatic, adLockOptimistic
intRows = intRows + 1
Loop
CloseConn
End Sub
Display More
Hope this helps.