Hi all,
i am trying to loop an array that i had populated with recordset.getrows() method and do an insert into a new table if certain records met a criteria, the code is working but i realised in the access table that the insert statement isnt inserting new records from the bottom, sometimes i would continue inserting records in the middle of the table, sometimes the bottom, this has messed up the sequence for the new records.
Please help! thanks!
Code:
Private Sub cmd_SortByArray_Click() ' Sort By Using Array Method
Dim db As Database
Dim rs As DAO.Recordset
' a variant is a dynamic data type
Dim data As Variant
Set db = CurrentDb
Set rs = db.OpenRecordset("tempHoldingTable", dbOpenDynaset)
rs.MoveLast
rs.MoveFirst
'get the all the rows in record count
data = rs.GetRows(rs.RecordCount)
'Sno
Dim OriginStr1 As String
'Wire Spec
Dim OriginStr2 As String
'Origin
Dim OriginStr3 As String
'Destination
Dim OriginStr4 As String
'counter for original file
Dim OrigIntCounter As Integer
'Sno
Dim DestStr1 As String
'Wire Spec
Dim DestStr2 As String
'Origin
Dim DestStr3 As String
'Destination
Dim DestStr4 As String
'counter for original file
Dim DestIntCounter As Integer
Dim testBoolean As Boolean
For OrigIntCounter = 0 To rs.RecordCount - 1
OriginStr1 = data(0, OrigIntCounter)
OriginStr2 = data(1, OrigIntCounter)
OriginStr3 = data(2, OrigIntCounter)
OriginStr4 = data(3, OrigIntCounter)
'Disable warning
DoCmd.SetWarnings False
** I AM INSERTING THE RECORD THAT I AM COMPARING**
'insert true results
DoCmd.RunSQL "INSERT INTO [ReformattedTable] SELECT tempHoldingTable.* FROM [tempHoldingTable] WHERE Sno = " & OriginStr1 & ";"
'enable warning again
DoCmd.SetWarnings True
For DestIntCounter = 0 To rs.RecordCount - 1
DestStr1 = data(0, DestIntCounter)
DestStr2 = data(1, DestIntCounter)
DestStr3 = data(2, DestIntCounter)
DestStr4 = data(3, DestIntCounter)
** IF THERE IS IS A RECORD THAT MATCHES FROM THE REMAINDING RECS***
If OriginStr4 = DestStr3 Then
testBoolean = True
'Disable warning
DoCmd.SetWarnings False
****INSERT IT AFTER THE ORGIN RECORDS ***
'insert true results
DoCmd.RunSQL "INSERT INTO ReformattedTable SELECT tempHoldingTable.* FROM tempHoldingTable WHERE Sno = " & DestStr1 & ";"
'enable warning again
DoCmd.SetWarnings True
End If
Next DestIntCounter
'MsgBox CStr(OriginStr1) + " " + CStr(OriginStr2) + " " + CStr(OriginStr3) + " " + CStr(OriginStr4) + " ", vbOKCancel
Next OrigIntCounter
MsgBox ("Done!")
Call cmd_refreshResultsTable_Click
End Sub
i am trying to loop an array that i had populated with recordset.getrows() method and do an insert into a new table if certain records met a criteria, the code is working but i realised in the access table that the insert statement isnt inserting new records from the bottom, sometimes i would continue inserting records in the middle of the table, sometimes the bottom, this has messed up the sequence for the new records.
Please help! thanks!
Code:
Private Sub cmd_SortByArray_Click() ' Sort By Using Array Method
Dim db As Database
Dim rs As DAO.Recordset
' a variant is a dynamic data type
Dim data As Variant
Set db = CurrentDb
Set rs = db.OpenRecordset("tempHoldingTable", dbOpenDynaset)
rs.MoveLast
rs.MoveFirst
'get the all the rows in record count
data = rs.GetRows(rs.RecordCount)
'Sno
Dim OriginStr1 As String
'Wire Spec
Dim OriginStr2 As String
'Origin
Dim OriginStr3 As String
'Destination
Dim OriginStr4 As String
'counter for original file
Dim OrigIntCounter As Integer
'Sno
Dim DestStr1 As String
'Wire Spec
Dim DestStr2 As String
'Origin
Dim DestStr3 As String
'Destination
Dim DestStr4 As String
'counter for original file
Dim DestIntCounter As Integer
Dim testBoolean As Boolean
For OrigIntCounter = 0 To rs.RecordCount - 1
OriginStr1 = data(0, OrigIntCounter)
OriginStr2 = data(1, OrigIntCounter)
OriginStr3 = data(2, OrigIntCounter)
OriginStr4 = data(3, OrigIntCounter)
'Disable warning
DoCmd.SetWarnings False
** I AM INSERTING THE RECORD THAT I AM COMPARING**
'insert true results
DoCmd.RunSQL "INSERT INTO [ReformattedTable] SELECT tempHoldingTable.* FROM [tempHoldingTable] WHERE Sno = " & OriginStr1 & ";"
'enable warning again
DoCmd.SetWarnings True
For DestIntCounter = 0 To rs.RecordCount - 1
DestStr1 = data(0, DestIntCounter)
DestStr2 = data(1, DestIntCounter)
DestStr3 = data(2, DestIntCounter)
DestStr4 = data(3, DestIntCounter)
** IF THERE IS IS A RECORD THAT MATCHES FROM THE REMAINDING RECS***
If OriginStr4 = DestStr3 Then
testBoolean = True
'Disable warning
DoCmd.SetWarnings False
****INSERT IT AFTER THE ORGIN RECORDS ***
'insert true results
DoCmd.RunSQL "INSERT INTO ReformattedTable SELECT tempHoldingTable.* FROM tempHoldingTable WHERE Sno = " & DestStr1 & ";"
'enable warning again
DoCmd.SetWarnings True
End If
Next DestIntCounter
'MsgBox CStr(OriginStr1) + " " + CStr(OriginStr2) + " " + CStr(OriginStr3) + " " + CStr(OriginStr4) + " ", vbOKCancel
Next OrigIntCounter
MsgBox ("Done!")
Call cmd_refreshResultsTable_Click
End Sub