I have a sub that is supposed to split a table (which is imported from excel) into two tables based on a value in the column "Award Categroy" (sic). This main table "list" is in alphabetical order, and the two new tables should be, too. However, sometimes, this sub does not add the entries to the new tables in order. For example, table "A" will start with last names beginning with H. Here is my code for the sub:
Private Sub split_Click()
On Error GoTo Err_split_Click
Dim dbs As Database
Dim Master As Recordset
Dim Accepted As Recordset
Dim Rejected As Recordset
Set dbs = CurrentDb()
Set Master = dbs.OpenRecordset("list")
Set Accepted = dbs.OpenRecordset("A")
Set Rejected = dbs.OpenRecordset("DandR")
clearA (Accepted.RecordCount) 'clear table "A" if not empty
clearDandR (Rejected.RecordCount)
If Not Master.EOF Then
Master.MoveFirst
Do Until Master.EOF
If (Master("SA#ACTION") = "A") Then
Accepted.AddNew
Accepted("SA#ACYR") = Master("SA#ACYR")
Accepted("LAST") = Master("LAST")
Accepted("FIRST") = Master("FIRST")
Accepted("Student Awards") = Master("Student Awards")
Accepted("Award Amount") = Master("Award Amount")
Accepted("SA#ACTION") = Master("SA#ACTION")
Accepted("Award Categroy") = Master("Award Categroy")
Accepted.Update
Else
Rejected.AddNew
Rejected("SA#ACYR") = Master("SA#ACYR")
Rejected("LAST") = Master("LAST")
Rejected("FIRST") = Master("FIRST")
Rejected("Student Awards") = Master("Student Awards")
Rejected("Award Amount") = Master("Award Amount")
Rejected("SA#ACTION") = Master("SA#ACTION")
Rejected("Award Categroy") = Master("Award Categroy")
Rejected.Update
End If
Master.MoveNext
Loop
End If
MsgBox "Table Split Complete", vbInformation, "Finished"
Master.Close
Accepted.Close
Rejected.Close
Exit_split_Click:
Set dbs = Nothing
Set Master = Nothing
Set Accepted = Nothing
Set Rejected = Nothing
Exit Sub
Err_split_Click:
MsgBox Err.Description
Resume Exit_split_Click
End Sub
I have tried running it in debug, and it seems like the addnew/update methods are the problem. How can i make this always add the next new record below the previously added one (in the two split tables)? Also, is there any way i should clean up my code (i'm a n00bie). Thanks in advance
Private Sub split_Click()
On Error GoTo Err_split_Click
Dim dbs As Database
Dim Master As Recordset
Dim Accepted As Recordset
Dim Rejected As Recordset
Set dbs = CurrentDb()
Set Master = dbs.OpenRecordset("list")
Set Accepted = dbs.OpenRecordset("A")
Set Rejected = dbs.OpenRecordset("DandR")
clearA (Accepted.RecordCount) 'clear table "A" if not empty
clearDandR (Rejected.RecordCount)
If Not Master.EOF Then
Master.MoveFirst
Do Until Master.EOF
If (Master("SA#ACTION") = "A") Then
Accepted.AddNew
Accepted("SA#ACYR") = Master("SA#ACYR")
Accepted("LAST") = Master("LAST")
Accepted("FIRST") = Master("FIRST")
Accepted("Student Awards") = Master("Student Awards")
Accepted("Award Amount") = Master("Award Amount")
Accepted("SA#ACTION") = Master("SA#ACTION")
Accepted("Award Categroy") = Master("Award Categroy")
Accepted.Update
Else
Rejected.AddNew
Rejected("SA#ACYR") = Master("SA#ACYR")
Rejected("LAST") = Master("LAST")
Rejected("FIRST") = Master("FIRST")
Rejected("Student Awards") = Master("Student Awards")
Rejected("Award Amount") = Master("Award Amount")
Rejected("SA#ACTION") = Master("SA#ACTION")
Rejected("Award Categroy") = Master("Award Categroy")
Rejected.Update
End If
Master.MoveNext
Loop
End If
MsgBox "Table Split Complete", vbInformation, "Finished"
Master.Close
Accepted.Close
Rejected.Close
Exit_split_Click:
Set dbs = Nothing
Set Master = Nothing
Set Accepted = Nothing
Set Rejected = Nothing
Exit Sub
Err_split_Click:
MsgBox Err.Description
Resume Exit_split_Click
End Sub
I have tried running it in debug, and it seems like the addnew/update methods are the problem. How can i make this always add the next new record below the previously added one (in the two split tables)? Also, is there any way i should clean up my code (i'm a n00bie). Thanks in advance