AndrewKent
Well-known Member
- Joined
- Jul 26, 2006
- Messages
- 889
Hi there,
I have a series of macros that transmit data from an Excel worksheet to fields within an ".mdb" file. Although what I am doing is a working process and I am fixing any errors as they turn up I am looking to keep an automatic log of these (as I have a user base that don't always feed bugs back!!)
An example of one of the macros is this...
However I need to add two error checks to this.
1) to check that firstly the table the code is trying to update exists in the database
2) to check that a record with the unique key exists in that table.
I have other similar routines for creating records and finding records in an ".mdb" file but this one is for updating an existing record. If I can find out how to check these points, I can then decide what my program will do if this happens.
Many thanks in advance,
Andy
I have a series of macros that transmit data from an Excel worksheet to fields within an ".mdb" file. Although what I am doing is a working process and I am fixing any errors as they turn up I am looking to keep an automatic log of these (as I have a user base that don't always feed bugs back!!)
An example of one of the macros is this...
Code:
Sub UpdateRecord()
' =============================================================================================
' This macro will firstly connect to the Access database. It will then update every field in
' each table based on the criteria that has been set. In order to simplify the code structure,
' the coding to update each table has been placed in individual macros.
' =============================================================================================
Dim DBName, DBLocation, FilePath As String
Dim DBConnection As ADODB.Connection
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Set DBConnection = New ADODB.Connection
DBName = Worksheets("Calculation Matrix").Range("CalculationMatrix_DatabaseName").Value
DBLocation = Worksheets("Calculation Matrix").Range("CalculationMatrix_DatabaseLocation").Value
FilePath = DBLocation & DBName
With DBConnection
.Provider = "Microsoft.Jet.OLEDB.4.0"
.Open FilePath
End With
Worksheets("Data Capture").Range("C5").Value = Worksheets("Calculation Matrix").Range("CalculationMatrix_Search").Value
Worksheets("Data Capture").Range("C8").Value = Worksheets("Staff Data").Range("E5").Value
Worksheets("Data Capture").Range("C9").Formula = "=Now()"
Worksheets("Data Capture").Select
Range("C9").Copy
Range("C9").PasteSpecial xlPasteValues
Worksheets("Data Capture").Range("C12").Value = False
Call UpdateIndex(DBConnection)
Call UpdateCustomerData(DBConnection)
Call UpdateCTFData(DBConnection)
Call UpdateGCBData(DBConnection)
Call UpdateCombiData(DBConnection)
Call UpdateTrackerFundData(DBConnection)
Call UpdateStakeholderData(DBConnection)
DBConnection.Close
Set DBConnection = Nothing
Worksheets("Index").Activate
Range("A1").Select
End Sub
Code:
Sub UpdateIndex(DBConnection As ADODB.Connection)
' =============================================================================================
' This macro is responsible for updating one table within the database. It is called by the
' UpdateRecord macro as part of a routine.
' =============================================================================================
Dim DBRecordSet As ADODB.Recordset
Dim Query As String
Query = "SELECT * FROM tblIndex WHERE Record_ID = '" & Worksheets("Calculation Matrix").Range("CalculationMatrix_Search").Value & "'"
Set DBRecordSet = New ADODB.Recordset
DBRecordSet.CursorLocation = adUseServer
DBRecordSet.Open Source:=Query, ActiveConnection:=DBConnection, CursorType:=adOpenForwardOnly, LockType:=adLockOptimistic, Options:=adCmdText
With DBRecordSet
.Fields("Record_ID") = Worksheets("Data Capture").Range("C5").Value
.Fields("Created_By") = Worksheets("Data Capture").Range("C6").Value
.Fields("Created_Date") = Worksheets("Data Capture").Range("C7").Value
.Fields("Modified_By") = Worksheets("Data Capture").Range("C8").Value
.Fields("Modified_Date") = Worksheets("Data Capture").Range("C9").Value
.Fields("Stakeholder_ID") = Worksheets("Data Capture").Range("C10").Value
.Fields("Brand") = Worksheets("Data Capture").Range("C11").Value
.Fields("Active_Status") = Worksheets("Data Capture").Range("C12").Value
.Fields("Record_Status") = Worksheets("Data Capture").Range("C13").Value
.Fields("ReferralTracker_Reference") = Worksheets("Data Capture").Range("C14").Value
.Fields("ModifiedBy_SellerCode") = Worksheets("Data Capture").Range("C15").Value
.Fields("NSO_Reason") = Worksheets("Data Capture").Range("C16").Value
.Fields("Campaign_Code") = Worksheets("Data Capture").Range("C17").Value
.Update
End With
DBRecordSet.Close
Set DBRecordSet = Nothing
End Sub
However I need to add two error checks to this.
1) to check that firstly the table the code is trying to update exists in the database
2) to check that a record with the unique key exists in that table.
I have other similar routines for creating records and finding records in an ".mdb" file but this one is for updating an existing record. If I can find out how to check these points, I can then decide what my program will do if this happens.
Many thanks in advance,
Andy