Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error Trap Method, how 2

Status
Not open for further replies.

uncleG

Technical User
Jun 10, 2004
63
US
On my form I use a double click procedure on a field [rePo] to open another form that contains the matching value using the code below.

Private Sub rePO_DblClick(Cancel As Integer)
On Error GoTo Err_rePO_DblClick

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmSalesOrder"
stLinkCriteria = "[PO]=" & "'" & Me![rePO] & "'"

DoCmd.OpenForm stDocName, , , stLinkCriteria

Exit_rePO_DblClick:
Exit Sub

Err_rePO_DblClick:
MsgBox Err.Description
Resume Exit_rePO_DblClick

End Sub

If a record exists with the matching reference it is displayed.
If no record is found with the matching reference data a new blank record is created. (not what I want)

Access 2000 does not see this as an error, but I do.

How would I check that the record first exists, if not prevent a new record from being created when the reference is not matched, and inform the user of such?
Thanks, Just trying to understand error handling
UncleG
 
Obviously frmSalesOrder has it's AllowAdditions property set to True, so it isn't considered an error. Even if AllowAdditions was False, I think the form would still open without errors (although you wouldn't be able to do anything with it).

I would check if the record exists before I try to open the form:
Code:
If Nz(DLookup("PO", "tbl_POs", "PO='" & Me![rePO] & "'"),"") <> "" Then
    stDocName = "frmSalesOrder"
    stLinkCriteria = "[PO]=" & "'" & Me![rePO] & "'"
    
    DoCmd.OpenForm stDocName, , , stLinkCriteria
Else
    MsgBox "The PO doesn't exist!"
End If



 
Thanks for your response JoeAtWork

Been at this for a while and modified as suggested or at least I think I have.
The error message comes up even when a record exists.
Have I entered the code in the wrong order?
Both Fields [Po] and [rePo] are Text, could this be my issue?
As you might guess I haven't a clue.
Thanks again, UncleG

CODE
Private Sub rePO_DblClick(Cancel As Integer)
On Error GoTo Err_rePO_DblClick

If Nz(DLookup("PO", "tblSalOrd", "PO" = "'" & Me![rePO] & "'"), "") <> "" Then

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "frmSalesOrder"
stLinkCriteria = "[Po]=" & "'" & Me![rePO] & "'"

DoCmd.OpenForm stDocName, , , stLinkCriteria

Else
MsgBox "Customer PO # is not in the System !"
End If

Exit_rePO_DblClick:
Exit Sub

Err_rePO_DblClick:
MsgBox Err.Description
Resume Exit_rePO_DblClick

End Sub
 
Hi UncleG:

I think you have a problem with the DLookup syntax. I think it should be:

DLookup("PO", "tblSalOrd", "PO = " & Me!rePO)

Hope that helps (and is right!).
T

 
Thank You JoeAtWork for getting me started.

Thank You Tarnish for making me look at the syntax.

I replaced the Dlookup line as follows, (' and " misplaced) and all is well once again.

If Nz(DLookup("PO", "tblSalOrd", "[Po]=" & "'" & Me![rePO] & "'"),"") <> "" Then

Thanks Again UncleG
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top