• Error Handling

    Author
    Topic
    #484737

    I have a switchboard that has a Edit Record button. When the Edit Record button is clicked in call frmCheckInEdit. The operator is instructed to enter the receipt number. The On Open event on frmCheckInEdit is as follows:

    Private Sub Form_Open(Cancel As Integer)
    Dim strWhere As String
    strWhere = “ReceiptNumber=[Enter Receipt Number]”
    DoCmd.OpenForm “frmCheckInEdit”, acNormal, , strWhere
    End Sub

    I am new to error handling and need to add an error handling routine that will tell the operator that an invalid receipt number was entered. I think it would be nice to give the operator the option of entering a new receipt number or returning to the Main Menu.

    Tom Goodwin

    Viewing 1 reply thread
    Author
    Replies
    • #1344461

      What defines an “invalid” receipt number? One that does not exist? You could check the existence first (check out DLookup) and if it does not exist, advise the user accordingly.

      • #1344712

        Yes. One that doesn’t exist. This was my solution, which seems to work.

        Private Sub Form_Open(Cancel As Integer)
        Dim strWhere As String
        strWhere = “ReceiptNumber=[Enter Receipt Number]”
        DoCmd.OpenForm “frmCheckInEdit”, acNormal, , strWhere

        If Me.RecordsetClone.RecordCount = 0 Then
        Cancel = True
        MsgBox “You entered an invalid receipt number. Press OK and start over.”
        End If
        Exit Sub
        End Sub

    • #1344720

      Tom,

      Glad you got it sorted out.

      What Cronk was getting out was to check for the existance of the Receipt No before opening the form.

      Code in your Edit Record button would look something like:

      Code:
        Private Sub cmdEditBtn_Click()
      
             Dim lRecCnt as long
             Dim zRecNo as String  'many need to change based on data type of ReceiptNo!
             
             zRecNo = inputbox("Enter Receipt No.","Receipt No To Edit:")
            If zRecNo = "" Then Exit Sub
      
            lRecCnt = DLookup("[Receipt No"], "Table Name Here", "[Receipt No] = " & Chr(34) & zRecNo & Chr(34))
           If lRecCnt > 0 Then
             DoCmd.OpenForm "frmCheckInEdit", acNormal, , "[Receipt No] = " & Chr(34) & zRecNo & Chr(34))
           Else     'Offer option to enter new record'
             if inputbox("Do you want to enter a New record","New Record Prompt","N") = "Y" then
               DoCmd.OpenForm "frmCheckInEdit", acNormal
             End if
           End If
             
        End Sub
      

      Of course this is “Air Code” and will need to be changed to fit your situation, e.g. your Database identifiers.
      :cheers:

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    Viewing 1 reply thread
    Reply To: Error Handling

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: