Results 1 to 12 of 12
  1. #1
    rtt is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    8

    Cancel Insert Statement if value is not within a set range - record inserted anyway


    I have a form that opens to collect a value entered by the user in the field labeled, "txtSoilPh". When clicking on "Add" command button, the Before Update event checks to determine if the value is within the correct range. If it is not, a message box opens advising user of the required range. When I close the message box, the insert statement continues to execute and updates the database anyway. I am still learning VB for Access so any help would be appreciated. Below is the code that I have so far for the form.

    Code:
    
    
    '------------------------------------------------------------
    ' cmdAdd_Click
    '
    '------------------------------------------------------------
    Private Sub cmdAdd_Click()
    On Error GoTo cmdAdd_Click_Err
    
    
    'Turn Off Warnings
    DoCmd.SetWarnings False
    
    
     'Append the Test Results Table with the Value in the Soil Ph Text Box
        DoCmd.RunSQL "INSERT INTO [TestResults]" & _
              " VALUES (Forms![frmAddWP]![txtYr-Sample] , Forms![frmAddWP]![txtSampleID], 'WP', Forms![frmAddWP]![txtSoilPh])"
    
    
    'Turn Message Box asking if you want to update the record back on
        DoCmd.SetWarnings True
    
    
     'Close the Message Box
        DoCmd.Close , ""
        
    cmdAdd_Click_Exit:
        Exit Sub
    
    
    cmdAdd_Click_Err:
        MsgBox Error$
        Resume cmdAdd_Click_Exit
    
    
    End Sub
    
    
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    'Check for WP range between 4 and 8 and Give Error If in range, then Hide the Message Box
    
    
        If Forms![frmAddWP]![txtSoilPh] < 4 Or Forms![frmAddWP]![txtSoilPh] > 8 Then
            MsgBox "WP Range Must be Between 4 and 8"
            Me.Undo
            txtSoilPh.SetFocus
          
     End If
    End Sub
    
    
    Private Sub Form_Close()
    Forms!frmRptCrosstab.Requery
    End Sub
    
    
    Private Sub Form_Current()
    txtSoilPh.SetFocus
    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,521
    I might put the append in an Else clause of the before update code, so you know it only runs with a valid value.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    ...... or move the Before Update code to be the primary IF statement in your On Click event......

  4. #4
    rtt is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    8
    Thank you both for the help. I have moved the code to the OnClick event and it appears to work as expected. Now, I have one more thing that I am wanting it to do. If I enter a value that is outside the range and I click on the message box with "OK", I want the form to stay open and the cursor to focus in the txtSoilPh. Can you help with that? Examples would be appreciated.

    Thank you again.

    Code:
    Private Sub cmdAdd_Click()
    On Error GoTo cmdAdd_Click_Err
    
    
    'Turn Off Warnings
    DoCmd.SetWarnings False
    
    
    'Check for WP range between 4 and 8 and Give Error If in range, then Hide the Message Box
    
    
        If Forms![frmAddWP]![txtSoilPh] < 4 Or Forms![frmAddWP]![txtSoilPh] > 8 Then
            MsgBox "WP Range Must be Between 4 and 8"
            Cancel = True
            
        Else
        'Append the Test Results Table with the Value in the Soil Ph Text Box
        DoCmd.RunSQL "INSERT INTO [TestResults]" & _
              " VALUES (Forms![frmAddWP]![txtYr-Sample] , Forms![frmAddWP]![txtSampleID], 'WP', Forms![frmAddWP]![txtSoilPh])"
          
     End If
    
    
    
    
    'Turn Message Box asking if you want to update the record back on
        DoCmd.SetWarnings True
    
    
     'Close the Message Box
        DoCmd.Close , ""
        
    cmdAdd_Click_Exit:
        Exit Sub
    
    
    cmdAdd_Click_Err:
        MsgBox Error$
        Resume cmdAdd_Click_Exit
    
    
    End Sub

  5. #5
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I would have stayed with the before update event as a more reliable place to do validation, but whatever floats your boat. Move the close into the Else clause and add a set focus line in the appropriate place:

    Me.TextboxName.SetFocus
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  6. #6
    NTC is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2009
    Posts
    2,392
    ....and this code doesn't look correct:

    'Close the Message Box
    DoCmd.Close , ""

    that command would be closing the main form......

  7. #7
    rtt is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    8
    Thank you again. I did notice that code at the bottom that was closing my form and have corrected that. I have it working now. I kept it in the OnClick event because it seemed to work much faster there than in the before update. I will take a look at that in more detail later on. I appreciate the help. Have a great day.

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    No problem, and welcome to the site by the way!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  9. #9
    rtt is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    8
    I thought that I had this worked out but I still have a small glitch and I would also like any suggestions on the best way to code this. I have a form that contains a value for pH. If the user needs to change the value, a doubleclick in the field opens a form with the focus on the pH value. The user can change the value and click Update. If the value is within the range the update goes through. If the value is not within the range, a message box shows advising that the number is out of range. Clicking OK takes the user back to the form with the focus on the pH value. In the example below Clicking update opens a message and when I click OK it goes back to the image below.

    Click image for larger version. 

Name:	FormExampleUpdate.jpg 
Views:	6 
Size:	20.8 KB 
ID:	17174

    All of this works fine, but I found that when I click, "cancel" the form closes but the database updates with the incorrect value. Also, pbaldy recommended that I use the beforeupdate method.

    The Code below shows The cmdCancel event, then the cmdUpdate event. The majority of my code is currently in this event. The BeforeUpdate event has no code in it now. I tried putting the check code in the beforeupdate event but it would freeze up and not process. I am still learning vba so any help with examples would be greatly appreciated.


    Code:
    Private Sub cmdCancel_Click()
    On Error GoTo cmdCancel_Click_Err
    
         DoCmd.Close , ""
    
    cmdCancel_Click_Exit:
        Exit Sub
    
    cmdCancel_Click_Err:
        MsgBox Error$
        Resume cmdCancel_Click_Exit
    
    End Sub
    
    
    Private Sub cmdUpdate_Click()
    On Error GoTo cmdUpdate_Click_Err
    
    
    'Turn Off Warnings
    DoCmd.SetWarnings False
    
     'Check for WP range between 4 and 8 and Give Error If in range, then Hide the Message Box
    
        If Forms![frmUpdateWP]![txtSoilPh] < 4 Or Forms![frmUpdateWP]![txtSoilPh] > 8 Then
            MsgBox "WP Range Must be Between 4 and 8"
            Forms![frmUpdateWP]![txtSoilPh].Undo
            Cancel = True
            Me.txtSoilPh.SetFocus
        Else
        
         'Update the Test Results Table with the Value in the Soil Ph Text Box
                DoCmd.RunSQL "UPDATE [testResults]" & _
            "SET [Reported Conc (Calib)] = Forms![frmUpdateWP]![txtSoilPh]" & _
            "WHERE [TestYear] = 'Forms![frmUpdateWP]![txtYr-Sample]' And [Sample ID] = 'Forms![frmUpdateWP]![txtSampleID]'"
        
          'Close the Message Box
                DoCmd.Close , ""
        End If
          
        'Turn Message Box asking if you want to update the record back on
        DoCmd.SetWarnings True
     
     cmdUpdate_Click_Exit:
        Exit Sub
    
    cmdUpdate_Click_Err:
        MsgBox Error$
        Resume cmdUpdate_Click_Exit
    End Sub
    
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
    
    End Sub
    Attached Files Attached Files

  10. #10
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Thus my statement in post 5.

    There's no validation happening now when you click on your cancel button. You can add similar code to it too, but the before update event catches both. Not sure why you'd have a performance issue with it there. That event is very commonly used for data validation.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  11. #11
    rtt is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2014
    Posts
    8
    Quote Originally Posted by pbaldy View Post
    Thus my statement in post 5.

    There's no validation happening now when you click on your cancel button. You can add similar code to it too, but the before update event catches both. Not sure why you'd have a performance issue with it there. That event is very commonly used for data validation.

    OK, thanks. I have tried this below. It does respond just as quickly and now when I click the OK button on the message box that tells me that I'm out of range, the form closes. The data does not change. So, I'm very close now, I think. Again, I will say that I'm learning this and so feel free to mark up the code or offer an example.

    Code:
    Private Sub cmdUpdate_Click()
    On Error GoTo cmdUpdate_Click_Err
    
    
    'Turn Off Warnings
    DoCmd.SetWarnings False
    
    
     
    
    
         'Update the Test Results Table with the Value in the Soil Ph Text Box
                DoCmd.RunSQL "UPDATE [testResults]" & _
            "SET [Reported Conc (Calib)] = Forms![frmUpdateWP]![txtSoilPh]" & _
            "WHERE [TestYear] = 'Forms![frmUpdateWP]![txtYr-Sample]' And [Sample ID] = 'Forms![frmUpdateWP]![txtSampleID]'"
        
          
      
    'Close the Message Box
                DoCmd.Close , ""
       
        
         
          
        'Turn Message Box asking if you want to update the record back on
        DoCmd.SetWarnings True
          
       
       
    
    
    cmdUpdate_Click_Exit:
        Exit Sub
    
    
    cmdUpdate_Click_Err:
        MsgBox Error$
        Resume cmdUpdate_Click_Exit
    End Sub
    
    
    
    
    
    
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     
    'Check for WP range between 4 and 8 and Give Error If in range, then Hide the Message Box
    
    
        If Forms![frmUpdateWP]![txtSoilPh] < 4 Or Forms![frmUpdateWP]![txtSoilPh] > 8 Then
            MsgBox "WP Range Must be Between 4 and 8"
            Forms![frmUpdateWP]![txtSoilPh].Undo
            Cancel = True
            Me.txtSoilPh.SetFocus
            
        End If
       
    
    
    
    
    End Sub

  12. #12
    John_G is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Looking at the code in post #4 above:

    The code is attached to a command button, not a form event. In this case, the "Cancel" after the error check means nothing - there is no event to cancel, so the code keeps running.

    Replace the Cancel = True with Exit Sub after the Msgbox error message, because you do not want to proceed if the data is not valid.

    HTH

    John

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

Similar Threads

  1. Replies: 5
    Last Post: 05-27-2014, 02:44 PM
  2. Cancel and Delete Record
    By Loc in forum Programming
    Replies: 3
    Last Post: 07-11-2013, 02:50 AM
  3. Replies: 4
    Last Post: 12-02-2011, 06:52 PM
  4. Cancel new record
    By oakoen in forum Forms
    Replies: 11
    Last Post: 12-18-2010, 09:26 AM
  5. Replies: 7
    Last Post: 04-27-2010, 10:29 AM

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