Results 1 to 14 of 14
  1. #1
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117

    Error Handling messing up Input Box

    Hello,



    I have a form which allows users to edit details of specific records.
    Subsequently, in the On Open event an input box requests the user to enter the record number they require.
    Users were complaining of error code 3075, which was resulting from users (stupidly) pressing enter without bothering to input anything into the input box.
    Subsequently, I set up error handling to display a msgbox reminding the user to enter their required record number, and then reload the input box so that the user could try again.

    The Problem-
    Somehow when I did this I made it so that pressing the cancel button on the input box also triggers the error message, and this results in an irritating loop of clicking cancel>error msgbox>trying to click cancel again>error msgbox again>etc.

    I have provided my code below, any and all help is appreciated I am still fairly new to VBA.

    Code:
    Private Sub Form_Open(Cancel As Integer)
        On Error GoTo Err_Ness
    Dim ControlNumber As String
    
    
    1: ControlNumber = InputBox("Please enter your lowest Control Number", _
             "Input Required", "001")
    
    
    DoCmd.OpenForm "frm_pmTensileTest", acNormal, , "ControlNumber = " & ControlNumber
    Exit_Ness:
        Exit Sub
    Err_Ness:
        Select Case Err.Number
            Case 3075
                MsgBox "Please enter a valid control number."
                Resume 1
               
        End Select
    
    
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I'd wrap the OpenForm in a test. Since your value appears to be numeric:

    Code:
    If IsNumeric(ControlNumber) Then
      DoCmd.OpenForm "frm_pmTensileTest", acNormal, , "ControlNumber = " & ControlNumber
    End If
    You can add an Else clause to give the user a message if you want.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117
    So use an if statement in place of my error handling protocol? Sorry I'm still new to VBA.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I subscribe to the school of thought that you prevent errors when you can, the error handler is for unexpected errors. This one is easily anticipated and handled with program logic. You can put a GoTo in the Else clause as well, though that would cause your loop again.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    drexasaurus's Avatar
    drexasaurus is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jul 2011
    Location
    Santa Ana, CA
    Posts
    60
    In the error handling for no selection I would add something like this:

    Dim resp As VbMsgBoxResult
    resp = MsgBox("Do you want to try input again?", vbYesNo)
    If resp = vbNo Then
    Exit_Ness
    Else
    Resume 1
    Else
    Last edited by drexasaurus; 12-08-2014 at 11:23 AM. Reason: Pressed publish on accident, too soon.

  6. #6
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117
    Okay pbaldy, I am onboard with this approach.

    I have amended the code as it appears below.
    I still need it to loop back to the original input box after displaying the error message.
    I just used 'Resume 1' to accomplish this in the error handling before, how would I do that now. Thanks!

    Code:
    Private Sub Form_Open(Cancel As Integer)
    Dim ControlNumber As String
    
    
    'This requests the user enter the desired control number
    1: ControlNumber = InputBox("Please enter your lowest Control Number", _
             "Input Required", "001")
    
    
    If IsNumeric(ControlNumber) Then
        DoCmd.OpenForm "frm_pmTensileTest", acNormal, , "ControlNumber = " & ControlNumber
            Else
                MsgBox "Please enter a valid control number.", vbOKOnly, "Data Required"
                
    End If
    End Sub

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    As I mentioned, you can use GoTo. That will still get you in that loop, so you may want to add a yes/no message box as in post 5. I typically wouldn't bother, I'd just make them click the button or whatever again.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117
    Quote Originally Posted by pbaldy View Post
    As I mentioned, you can use GoTo. That will still get you in that loop, so you may want to add a yes/no message box as in post 5. I typically wouldn't bother, I'd just make them click the button or whatever again.
    If this was an onclick event I wouldn't bother either, but it is an onOpen event and after the error msgbox displays it is simply opening the form to a random (near as I can tell) record, which isn't going to work.
    Also the GoTo method, as you mentioned, continues the looping problem when clicking the cancel button.
    Last edited by jtm013; 12-08-2014 at 12:28 PM. Reason: I apparently, do not know how to spell.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    This code is in the open event of the same form (frm_pmTensileTest)? If so, I don't think it will work this way. The OpenForm wouldn't do anything, since the form is already opening. I'd probably set the filter property, and perhaps Cancel = True if they don't enter a valid number, which should stop the form from opening.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117
    So I realized I was being dense and went ahead and moved this code to the on click event of the switchboard button for this form.

    But is there really no way to make the cancel button simply close out the input box?
    I guess I don't understand why the cancel button isn't...well...cancelling.

    So now it looks like this:
    Code:
    Private Sub Command2_Click()
    Dim ControlNumber As String
    
    
    'This requests the user enter the desired control number
    1: ControlNumber = InputBox("Please enter your lowest Control Number", _
             "Input Required", "001")
    
    
    'This actually opens the selected record
    DoCmd.OpenForm "frm_pmTensileTest", , , "ControlNumber = " & ControlNumber
    DoCmd.Close acForm, "frm_welcomescreen"
         
    End Sub

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Try an If Then Else structure. Example from my db:
    Code:
        Do
            strTestTime = InputBox("Enter a time value for test number 800 - Special Testing.")
        Loop Until IsNumeric(strTestTime) Or Len(strTestTime) = 0
        If Len(strTestTime) > 0 Then
            CurrentDb.Execute "UPDATE Tests SET TimeCharge=" & CDbl(strTestTime) & " WHERE LabNum='" & strLabNum & "' AND TestNum='800'"
        Else
            'User pressed cancel
            MsgBox "Logout cancelled.", vbOKOnly, "Cancel"
        End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I'd still use that IsNumeric() test, which cancelling the input box will fail.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  13. #13
    jtm013 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Aug 2014
    Posts
    117
    My bad that last bit of code I pasted was not correct. I was still using the IsNumeric() test.
    Quote Originally Posted by pbaldy View Post
    I'd still use that IsNumeric() test, which cancelling the input box will fail.
    Anyway, June7 inspired me to simply make my msgbox 'cancel friendly' so now I think it will be okay for the folks who will be using it daily (famous last words).

    I'm going to go ahead and marked this solved, since I think this will be sufficient.

    Final code ended up looking like this:

    Code:
    Private Sub Command2_Click()
    Dim ControlNumber As String
    
    
    'This requests the user enter the desired control number
    1: ControlNumber = InputBox("Please enter your lowest Control Number", _
             "Input Required", "001")
    
    
    'This actually opens the selected record
    If IsNumeric(ControlNumber) Then
        DoCmd.OpenForm "frm_pmTensileTest", , , "ControlNumber = " & ControlNumber
        DoCmd.Close acForm, "frm_welcomescreen"
            Else
                'User pressed cancel
                MsgBox "No Control Number, form open cancelled.", vbOKOnly, "Missing Data"
    End If
    
    End Sub
    Thanks for all the assistance!

  14. #14
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    Glad you got it sorted. That's basically what I suggested in post 2.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Error handling of table update error?
    By panoss in forum Forms
    Replies: 5
    Last Post: 10-31-2014, 02:06 PM
  2. error handling
    By slimjen in forum Forms
    Replies: 6
    Last Post: 03-13-2013, 11:49 AM
  3. Error Handling Question - On Error Goto
    By redbull in forum Programming
    Replies: 7
    Last Post: 12-06-2012, 07:54 AM
  4. Replies: 3
    Last Post: 09-05-2012, 10:23 AM
  5. Error 2501 displays with Error handling
    By agent- in forum Programming
    Replies: 13
    Last Post: 08-05-2011, 02:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums