Hi,
Am gradually teaching myself VBA. Below is some of the code I've written. I'm looking for any suggestions around optimising it.
What it accomplishes is:
* I have two tables, tblOrders & tblOrders_local
* the contents of tblOrders are refreshed each day. The records for this table are extracted from an Oracle one via ODBC (and inserted via a SQL INSERT statement in Access)
* tblOrders_local contains the local order (information not listed in the Oracle database) that I want to store. Unfortunately I'm not allowed to add this information back into the Oracle database.
* the Sub checks if an order number in tblOrders exists in tblOrders_local. If a matching record isn't found in tblOrders_local, the Sub inserts one.
The Sub at the moment seems to work. I'm guessing the code can be cleaned up a lot though.
Am gradually teaching myself VBA. Below is some of the code I've written. I'm looking for any suggestions around optimising it.
Code:
Private Sub cmd_Update_tblOrders_local_Click()
Dim intButtonPressed As Integer
' Check that the user does want to proceed with this action
intButtonPressed = MsgBox("This will add all new orders to the default project. Please press okay to proceed", vbOKCancel, "Assign new orders")
If intButtonPressed = 2 Then GoTo Exit_cmd_Update_tblOrders_local_Click
Dim con As ADODB.Connection
Dim recSet As ADODB.Recordset
Dim tempSet As ADODB.Recordset
Dim localSet As ADODB.Recordset
Dim strSQL As String
Dim tempSQL As String
Dim ordNum As String
strSQL = "SELECT ORDER_NUMBER FROM tblOrders"
Set con = CurrentProject.Connection
Set recSet = New ADODB.Recordset
Set localSet = New ADODB.Recordset
Set tempSet = New ADODB.Recordset
recSet.Open strSQL, con
localSet.Open "tblOrders_local", con, adOpenDynamic, adLockOptimistic
Do Until recSet.EOF
ordNum = recSet.Fields("ORDER_NUMBER")
tempSQL = "SELECT * FROM tblOrders_local WHERE ORDER_NUMBER = " & ordNum
tempSet.Open tempSQL, con
Debug.Print ordNum & " " & tempSet.RecordCount
If tempSet.EOF Then
varfields = Array("ORDER_NUMBER", "PROJECT_NUMER")
varvalues = Array(ordNum, 2)
localSet.AddNew varfields, varvalues
End If
tempSet.Close
recSet.MoveNext
Loop
' close the record set & connection objects
recSet.Close
localSet.Close
con.Close
Set recSet = Nothing
Set con = Nothing
MsgBox ("Its done!")
Exit_cmd_Update_tblOrders_local_Click:
Exit Sub
Error_cmd_Update_tblOrders_local_Click:
MsgBox Err.DESCRIPTION
Resume Exit_cmd_Update_tblOrders_local_Click
End Sub
What it accomplishes is:
* I have two tables, tblOrders & tblOrders_local
* the contents of tblOrders are refreshed each day. The records for this table are extracted from an Oracle one via ODBC (and inserted via a SQL INSERT statement in Access)
* tblOrders_local contains the local order (information not listed in the Oracle database) that I want to store. Unfortunately I'm not allowed to add this information back into the Oracle database.
* the Sub checks if an order number in tblOrders exists in tblOrders_local. If a matching record isn't found in tblOrders_local, the Sub inserts one.
The Sub at the moment seems to work. I'm guessing the code can be cleaned up a lot though.
Last edited: