Someone here helped me with this code before but I am still having a small problem with it. If I only have one record for the PartName or nothing at all it errors out with something about EOF or BOF. I am not sure what to change to keep it from doing this.
Thanks for the help.
Code:
On Error GoTo Err_Handler
Dim s As String
Dim db As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim rsFind As New ADODB.Recordset
Dim Icount As Double
Dim Rcount As Double
Dim EngineID As Double
Dim NewPartName As String
'Open db connection
Set db = CurrentProject.Connection
'open recordet to tblCPartOnOrder
s = "Select * from tblCPartOnOrder"
Set rsFind = New ADODB.Recordset
rsFind.Open s, db, adOpenDynamic, adLockOptimistic
'open recordet to tblCPartOnOrderSort
s = "tblCPartOnOrderSort"
Set rs = New ADODB.Recordset
rs.Open s, db, adOpenDynamic, adLockOptimistic
Rcount = 1
Do While Not rsFind.EOF
If Rcount = 1 Then
EngineID = rsFind.Fields("EngineID")
NewPartName = rsFind.Fields("PartName")
rsFind.MoveNext
Rcount = Rcount + 1
End If
If rsFind.Fields("EngineID") = EngineID Then
'same record
NewPartName = NewPartName & ", " & rsFind.Fields("PartName")
rsFind.MoveNext
Rcount = Rcount + 1
Else
'new record
'Add Current record
With rs
.AddNew
.Fields("EngineID") = EngineID
.Fields("ComboPartName") = NewPartName
.Update
Icount = Icount + 1
End With
EngineID = rsFind.Fields("EngineID")
NewPartName = rsFind.Fields("PartName")
rsFind.MoveNext
Rcount = Rcount + 1
End If
Loop
With rs
.AddNew
.Fields("EngineID") = EngineID
.Fields("ComboPartName") = NewPartName
.Update
Icount = Icount + 1
End With
MsgBox (Rcount - 1) & " records were combined into " & Icount & " new records."
Set rs = Nothing
Set rsFind = Nothing
DoCmd.Requery
Exit_Err_handler:
Exit Sub
Err_Handler:
Select Case Err.Number
'This checks for Error ______
Case Else
MsgBox Err.Description
MsgBox Err.Number
Resume Exit_Err_handler
End Select
Exit_Sub
Thanks for the help.
Last edited by a moderator: