T
terry w
hello to all
I have some code that works OK but has a few problems that I'd really like
to fix. My form has 2 comboboxes called cboA and cboB. When the user
selects an AB combination then clicks cmdDelete, the code finds the matching
record then deletes it. But, there are 2 bugs:
1) If the user chooses an AB combination that is NOT in the recordset then
clicks cmdDelete, the code goes into an endless loop. I get around this by
using a counter (see the 6 lines marked ****), but I know that this is bad
code. How should it be handled?
2) Also, the user is unable to delete the FIRST record displayed. I suspect
this has to do with the rs.FindNext, but I'm not sure how to fix it.
Here's a code fragment:
Private Sub cmdDelete_Click()
Dim rs As Object
Set rs = Me.Recordset.Clone
Dim counter As Integer '****
counter = 0 '****
If IsNull(cboA) Or IsNull(cboB) Then
MsgBox ("Fill BOTH comboboxes then try again.")
Exit Sub
End If
Do While Not rs.EOF
rs.FindNext "[A_ID]=" & str(Me![cboA])
Me.Bookmark = rs.Bookmark
If [B_ID] = Int(Me![cboB]) Then
DoCmd.RunCommand acCmdDeleteRecord
Exit Do
End If
counter = counter + 1 '****
If counter > 2000 Then '****
MsgBox ("ENDLESS LOOP CATCHER!!! - FIX " & vbCrLf & "No Match")
Exit Sub '****
End If '****
Loop
.... code goes on ...
I have some code that works OK but has a few problems that I'd really like
to fix. My form has 2 comboboxes called cboA and cboB. When the user
selects an AB combination then clicks cmdDelete, the code finds the matching
record then deletes it. But, there are 2 bugs:
1) If the user chooses an AB combination that is NOT in the recordset then
clicks cmdDelete, the code goes into an endless loop. I get around this by
using a counter (see the 6 lines marked ****), but I know that this is bad
code. How should it be handled?
2) Also, the user is unable to delete the FIRST record displayed. I suspect
this has to do with the rs.FindNext, but I'm not sure how to fix it.
Here's a code fragment:
Private Sub cmdDelete_Click()
Dim rs As Object
Set rs = Me.Recordset.Clone
Dim counter As Integer '****
counter = 0 '****
If IsNull(cboA) Or IsNull(cboB) Then
MsgBox ("Fill BOTH comboboxes then try again.")
Exit Sub
End If
Do While Not rs.EOF
rs.FindNext "[A_ID]=" & str(Me![cboA])
Me.Bookmark = rs.Bookmark
If [B_ID] = Int(Me![cboB]) Then
DoCmd.RunCommand acCmdDeleteRecord
Exit Do
End If
counter = counter + 1 '****
If counter > 2000 Then '****
MsgBox ("ENDLESS LOOP CATCHER!!! - FIX " & vbCrLf & "No Match")
Exit Sub '****
End If '****
Loop
.... code goes on ...