nils7
10-09-2015, 03:12 AM
Hi all,
I have a central database that is being updated with values from an excel spreadsheet. I am using the ADO connection method to write each non-empty cell from an Excel sheet into the database. The writing into the database works perfectly fine as long as it is a new record.
What i would like to do now is to write a scipt that can update an existing record in the database table. My idea was to:
(1) read unique identifier for a row in excel
(2) retrieve the record that relates to this identifier in Access (using select * table, connection WHERE AcessID = unique identifier)
(3) change all other fields in the record using the information in the excel row
(4) loop to the next row in excel and repeat
The VBA code sits in a module within the Excel spreadshete and is initiated from Excel.
I am stuck at point (2). The recodset that I select is empty.
I woul be very grateful for any help.
Cheers,
Nils
Option Explicit
Sub update_existing_record()
'updates data from the active worksheet in a table in an Access database
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim r As Long
Dim t1, t2 As String
Dim VClookup, ssql As String
'timestamp 1
t1 = Now()
'this is the excel sheet that has the data
Worksheets("Export").Activate
'connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
"Data Source=\\servera\databasepath\FCSTTestV1.accdb;"
' all records in a table
r = 2 ' the start row in the worksheet
'loop that cycles through list starting in row 2 until end of list
Do While Len(Range("A" & r).Formula) > 0
'assigning value to be looked up to lookup variable
VClookup = Range("A" & r).Value
'lookup
ssql = "select * From fdfolio where [VC] ='VClookup'"
Debug.Print ssql
Set rs = New ADODB.Recordset
rs.Open ssql, cn, adOpenDynamic, adLockOptimistic, adCmdTable
With rs
If Not .BOF And Not .EOF Then
.MoveLast
.MoveFirst
If .Supports(adUpdate) Then
.Fields("AC") = Range("B" & r).Value
.Fields("E") = Range("C" & r).Value
.Fields("CC") = Range("D" & r).Value
.Fields("Ac1") = Range("E" & r).Value
.Fields("Ac2") = Range("F" & r).Value
.Fields("Ac3") = Range("G" & r).Value
.Fields("Ac4") = Range("H" & r).Value
.Fields("P1") = Range("I" & r).Value
.Fields("P2") = Range("J" & r).Value
.Fields("P3") = Range("K" & r).Value
.Fields("P4") = Range("L" & r).Value
.Fields("P5") = Range("M" & r).Value
.Fields("P6") = Range("N" & r).Value
.Fields("P7") = Range("O" & r).Value
.Fields("P8") = Range("P" & r).Value
.Fields("P9") = Range("Q" & r).Value
.Fields("P10") = Range("R" & r).Value
.Fields("P11") = Range("S" & r).Value
.Fields("P12") = Range("T" & r).Value
.Fields("FCST") = Range("U" & r).Value
.Fields("B") = Range("V" & r).Value
.Fields("Updated") = Now()
.Update
.Save
End If
End If
.Close
End With
r = r + 1
Loop
'emptying recordset and closing connection
Set rs = Nothing
cn.Close
Set cn = Nothing
'timestamp 2
t2 = Now()
'output time taken to process records
MsgBox t1 & " to " & t2
End Sub
Some more information about my data setup:
My excel data is in a sheet called "Export". Each column relates to one ofthe fields in the access database table called "fdfolio".
The unique identifier is in row A in the Excel sheet and this is the field called "VC" in the database. It is the first field in the table fdfolio
I have a central database that is being updated with values from an excel spreadsheet. I am using the ADO connection method to write each non-empty cell from an Excel sheet into the database. The writing into the database works perfectly fine as long as it is a new record.
What i would like to do now is to write a scipt that can update an existing record in the database table. My idea was to:
(1) read unique identifier for a row in excel
(2) retrieve the record that relates to this identifier in Access (using select * table, connection WHERE AcessID = unique identifier)
(3) change all other fields in the record using the information in the excel row
(4) loop to the next row in excel and repeat
The VBA code sits in a module within the Excel spreadshete and is initiated from Excel.
I am stuck at point (2). The recodset that I select is empty.
I woul be very grateful for any help.
Cheers,
Nils
Option Explicit
Sub update_existing_record()
'updates data from the active worksheet in a table in an Access database
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim r As Long
Dim t1, t2 As String
Dim VClookup, ssql As String
'timestamp 1
t1 = Now()
'this is the excel sheet that has the data
Worksheets("Export").Activate
'connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.ACE.OLEDB.12.0; " & _
"Data Source=\\servera\databasepath\FCSTTestV1.accdb;"
' all records in a table
r = 2 ' the start row in the worksheet
'loop that cycles through list starting in row 2 until end of list
Do While Len(Range("A" & r).Formula) > 0
'assigning value to be looked up to lookup variable
VClookup = Range("A" & r).Value
'lookup
ssql = "select * From fdfolio where [VC] ='VClookup'"
Debug.Print ssql
Set rs = New ADODB.Recordset
rs.Open ssql, cn, adOpenDynamic, adLockOptimistic, adCmdTable
With rs
If Not .BOF And Not .EOF Then
.MoveLast
.MoveFirst
If .Supports(adUpdate) Then
.Fields("AC") = Range("B" & r).Value
.Fields("E") = Range("C" & r).Value
.Fields("CC") = Range("D" & r).Value
.Fields("Ac1") = Range("E" & r).Value
.Fields("Ac2") = Range("F" & r).Value
.Fields("Ac3") = Range("G" & r).Value
.Fields("Ac4") = Range("H" & r).Value
.Fields("P1") = Range("I" & r).Value
.Fields("P2") = Range("J" & r).Value
.Fields("P3") = Range("K" & r).Value
.Fields("P4") = Range("L" & r).Value
.Fields("P5") = Range("M" & r).Value
.Fields("P6") = Range("N" & r).Value
.Fields("P7") = Range("O" & r).Value
.Fields("P8") = Range("P" & r).Value
.Fields("P9") = Range("Q" & r).Value
.Fields("P10") = Range("R" & r).Value
.Fields("P11") = Range("S" & r).Value
.Fields("P12") = Range("T" & r).Value
.Fields("FCST") = Range("U" & r).Value
.Fields("B") = Range("V" & r).Value
.Fields("Updated") = Now()
.Update
.Save
End If
End If
.Close
End With
r = r + 1
Loop
'emptying recordset and closing connection
Set rs = Nothing
cn.Close
Set cn = Nothing
'timestamp 2
t2 = Now()
'output time taken to process records
MsgBox t1 & " to " & t2
End Sub
Some more information about my data setup:
My excel data is in a sheet called "Export". Each column relates to one ofthe fields in the access database table called "fdfolio".
The unique identifier is in row A in the Excel sheet and this is the field called "VC" in the database. It is the first field in the table fdfolio