The code below is to compare two tables and see if there is any differences. one of the table act as a historian and the other as an update table.
The historian will record any changes plus any new records that hasnt been stored in the database. I use two two ado recordset with one connection since they are located in the same database. For some weird reason i cant close the rst recordset. Everythings seems to be working fine but i dont want anything leaving open and if you have a better way to do this you feedback will be most helpful. It works but is it best practice?
Private Sub Command0_Click()
Dim mycon As ADODB.Connection
Set mycon = CurrentProject.Connection
Dim rs As New ADODB.Recordset
Dim rst As New ADODB.Recordset
Dim closedate As Date
Dim Deliverydate As Date
Dim TES As String
Dim Opp As String
Dim Opp_Desc As String
Dim win_prob As Integer
Dim Team As String
Dim Forecast As String
Dim PropID As String
Dim Orig As String
Dim Dest As String
Dim Trev As Currency
Dim exist As Boolean
rs.ActiveConnection = mycon
rst.ActiveConnection = mycon
rst.Open "Forecast", , adOpenDynamic, adOpenStatic
rs.Open "[temp forecast]", , adOpenDynamic, adOpenStatic
rs.MoveFirst
rst.MoveFirst
While Not rs.EOF
exist = False
While Not rst.EOF
If rs.Fields("Oppty ID").Value = rst.Fields("Oppty ID").Value Then
closedate = Nz(rst.Fields("close date").Value)
Deliverydate = Nz(rst.Fields("Delivery date").Value)
TES = Nz(rst.Fields("Oppty ID").Value)
Opp = Nz(rst.Fields("Opportunity").Value)
Opp_Desc = Nz(rst.Fields("Opportunity Description").Value)
win_prob = Nz(rst.Fields("Win Probability").Value)
Team = Nz(rst.Fields("Primary Team Member").Value)
Forecast = Nz(rst.Fields("Forecast Type").Value)
PropID = Nz(rst.Fields("Quote Number").Value)
Orig = Nz(rst.Fields("Origination Region").Value)
Dest = Nz(rst.Fields("Project Destination Region").Value)
Trev = Nz(rst.Fields("Total Revenue").Value)
exist = True
End If
rst.MoveNext
Wend
rst.MoveFirst
If exist = False Or rs.Fields("close date").Value <> closedate Or rs.Fields("Delivery date").Value <> Deliverydate Or rs.Fields("Win Probability").Value <> win_prob Or rs.Fields("Forecast type").Value <> Forecast Or rs.Fields("Total Revenue").Value <> Trev Then
rst.AddNew
rst.Fields("close date").Value = rs.Fields("close date").Value
rst.Fields("Delivery date").Value = rs.Fields("Delivery date").Value
rst.Fields("Oppty ID").Value = rs.Fields("Oppty ID").Value
rst.Fields("Opportunity").Value = rs.Fields("Opportunity").Value
rst.Fields("Opportunity Description").Value = rs.Fields("Opportunity Description").Value
rst.Fields("Win Probability").Value = rs.Fields("Win Probability").Value
rst.Fields("Primary Team Member").Value = rs.Fields("Primary Team Member").Value
rst.Fields("Forecast Type").Value = rs.Fields("Forecast Type").Value
rst.Fields("Quote Number").Value = rs.Fields("Quote Number").Value
rst.Fields("Origination Region").Value = rs.Fields("Origination Region").Value
rst.Fields("Project Destination Region").Value = rs.Fields("Project Destination Region").Value
rst.Fields("Total Revenue").Value = rs.Fields("Total Revenue").Value
rst.Update
End If
rs.MoveNext
Wend
'rst.Close <---- Not working
rs.Close
Set rst = Nothing
Set rs = Nothing
Set mycon = Nothing
End Sub
The historian will record any changes plus any new records that hasnt been stored in the database. I use two two ado recordset with one connection since they are located in the same database. For some weird reason i cant close the rst recordset. Everythings seems to be working fine but i dont want anything leaving open and if you have a better way to do this you feedback will be most helpful. It works but is it best practice?
Private Sub Command0_Click()
Dim mycon As ADODB.Connection
Set mycon = CurrentProject.Connection
Dim rs As New ADODB.Recordset
Dim rst As New ADODB.Recordset
Dim closedate As Date
Dim Deliverydate As Date
Dim TES As String
Dim Opp As String
Dim Opp_Desc As String
Dim win_prob As Integer
Dim Team As String
Dim Forecast As String
Dim PropID As String
Dim Orig As String
Dim Dest As String
Dim Trev As Currency
Dim exist As Boolean
rs.ActiveConnection = mycon
rst.ActiveConnection = mycon
rst.Open "Forecast", , adOpenDynamic, adOpenStatic
rs.Open "[temp forecast]", , adOpenDynamic, adOpenStatic
rs.MoveFirst
rst.MoveFirst
While Not rs.EOF
exist = False
While Not rst.EOF
If rs.Fields("Oppty ID").Value = rst.Fields("Oppty ID").Value Then
closedate = Nz(rst.Fields("close date").Value)
Deliverydate = Nz(rst.Fields("Delivery date").Value)
TES = Nz(rst.Fields("Oppty ID").Value)
Opp = Nz(rst.Fields("Opportunity").Value)
Opp_Desc = Nz(rst.Fields("Opportunity Description").Value)
win_prob = Nz(rst.Fields("Win Probability").Value)
Team = Nz(rst.Fields("Primary Team Member").Value)
Forecast = Nz(rst.Fields("Forecast Type").Value)
PropID = Nz(rst.Fields("Quote Number").Value)
Orig = Nz(rst.Fields("Origination Region").Value)
Dest = Nz(rst.Fields("Project Destination Region").Value)
Trev = Nz(rst.Fields("Total Revenue").Value)
exist = True
End If
rst.MoveNext
Wend
rst.MoveFirst
If exist = False Or rs.Fields("close date").Value <> closedate Or rs.Fields("Delivery date").Value <> Deliverydate Or rs.Fields("Win Probability").Value <> win_prob Or rs.Fields("Forecast type").Value <> Forecast Or rs.Fields("Total Revenue").Value <> Trev Then
rst.AddNew
rst.Fields("close date").Value = rs.Fields("close date").Value
rst.Fields("Delivery date").Value = rs.Fields("Delivery date").Value
rst.Fields("Oppty ID").Value = rs.Fields("Oppty ID").Value
rst.Fields("Opportunity").Value = rs.Fields("Opportunity").Value
rst.Fields("Opportunity Description").Value = rs.Fields("Opportunity Description").Value
rst.Fields("Win Probability").Value = rs.Fields("Win Probability").Value
rst.Fields("Primary Team Member").Value = rs.Fields("Primary Team Member").Value
rst.Fields("Forecast Type").Value = rs.Fields("Forecast Type").Value
rst.Fields("Quote Number").Value = rs.Fields("Quote Number").Value
rst.Fields("Origination Region").Value = rs.Fields("Origination Region").Value
rst.Fields("Project Destination Region").Value = rs.Fields("Project Destination Region").Value
rst.Fields("Total Revenue").Value = rs.Fields("Total Revenue").Value
rst.Update
End If
rs.MoveNext
Wend
'rst.Close <---- Not working
rs.Close
Set rst = Nothing
Set rs = Nothing
Set mycon = Nothing
End Sub