Results 1 to 4 of 4

Thread: Error Handling

  1. #1
    4 Star Lounger
    Join Date
    May 2002
    Location
    Roanoke, Virginia, USA
    Posts
    493
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error Handling

    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

  2. #2
    Lounger
    Join Date
    Feb 2011
    Posts
    43
    Thanks
    0
    Thanked 6 Times in 6 Posts
    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.

  3. #3
    4 Star Lounger
    Join Date
    May 2002
    Location
    Roanoke, Virginia, USA
    Posts
    493
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    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.
    Last edited by RetiredGeek; 2012-08-10 at 17:10.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •