Error Handling

ddrew

seasoned user
Local time
Today, 16:18
Joined
Jan 26, 2003
Messages
911
I have a form that I use to set the owner of the DB (so it only allows the user to create one record), with a combobox and a number of textboxes. The textboxes are populated by choosing a record from the combobox. I have found on occasion that I get the following error message:

Update or CancelUpdate without AddNew or Edit. It has an OK and a Help button but the Help gives no help! Pressing OK will remove the message but wont allow me to do anything eles with the form as the error comes back again.


I have a checkbox that hides the combobox once a selection has been made, the error always occurs when I have selected from the combobox, the same record that is already populated into the textboxes and then selectined the checkbox.

I guess I need some error handling in here but Im unsure what or how!

My code for the combobox is:
Code:
Private Sub cbo_DatabaseOwner_AfterUpdate()
On Error GoTo cbo_DatabaseOwner_AfterUpdate_Err

    DoCmd.SearchForRecord , "", acFirst, "[ContactID] = " & Str(Nz(Screen.ActiveControl, 0))

cbo_DatabaseOwner_AfterUpdate_Exit:
    Exit Sub

cbo_DatabaseOwner_AfterUpdate_Err:
    MsgBox Error$
    Resume cbo_DatabaseOwner_AfterUpdate_Exit

End Sub
 
Could you add code in the after update event of the combo box that evalutes the entry and either rejects it or clears the text box values ?

Does this link assist ?
http://allenbrowne.com/RecordCountError.html

The code that Im using is in the AfterUpdate Event, so you maybe right but Im not sure what to put to handle it.
 
Before this line of code
Code:
DoCmd.SearchForRecord , "", acFirst, "[ContactID] = " & Str(Nz(Screen.ActiveControl, 0))
Add code to evaluate the data entered and compare against any data in text boxes on the form.
To do this you will need to use code something like this:
Code:
If Me.thiscomboname = Me.sometextboxname Then
Me.sometextboxname = ""
End If

May not be so simple but this may cause the value of the text box to be "" if what you just entered in the combobox is the same as what was in the text box.

If you wanted the combox data to not be the same ie the error is what the operator enered then you could use:
Code:
If Me.thiscomboname = Me.sometextboxname Then
MsgBox "you must enter a different value " & thiscomboname & " has already been used"
Me.thiscomboname = ""
End If
 
Find the root of the problem and nip it in the bud. Comment out the On Error GoTo line of code and run and trace the error.
 
Find the root of the problem and nip it in the bud. Comment out the On Error GoTo line of code and run and trace the error.

Afetr searching around for a while, I found this piece of code
Code:
If Me.Dirty Then
Me.Dirty = False
End If
I put it in here
Code:
Private Sub cbo_DatabaseOwner_AfterUpdate()
On Error GoTo cbo_DatabaseOwner_AfterUpdate_Err

If Me.Dirty Then
Me.Dirty = False
End If

  DoCmd.SearchForRecord , "", acFirst, "[ContactID] = " & Str(Nz(Screen.ActiveControl, 0))
cbo_DatabaseOwner_AfterUpdate_Exit:
    Exit Sub

cbo_DatabaseOwner_AfterUpdate_Err:
    MsgBox Error$
    Resume cbo_DatabaseOwner_AfterUpdate_Exit

End Sub
and now it works fine. I have no idea what or why just that it does, maybe you can shed some light on it?
 
It's commiting your changes before you perform a search. But still that's not where the problem is originating from. Like I mentioned, comment out the On Error line, comment out your new code and re-run it to see where the error originates.
 

Users who are viewing this thread

Back
Top Bottom