Access form need critical stop VBA coding

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • percman
    New Member
    • Sep 2013
    • 8

    Access form need critical stop VBA coding

    I am extremely new to VBA and am trying to something that one would think would be easy. All I need is a way to create a stop in a form when a wrong entry is input. The form consists of hours per day, and three different areas where these hours are distributed. I have this code:

    *

    Code:
    Private Sub Text1470_AfterUpdate()
    
    Me.Text1474.Value = (Text1470 / HOURSMondayTotal)
    
    If Val([Text1467]) + Val([Text1469]) + Val([Text1470]) > Val(HOURSMondayTotal) Then
    
    MsgBox "Hours for each well combined cannot exceed total hours for day, please re-enter"
    
    End If
    
    If Val([Text1467]) + Val([Text1469]) + Val([Text1470]) < Val(HOURSMondayTotal) Then
    
    MsgBox "Hours for each well combined must at least add up to the Total Hours Worked for the day, please re-enter"
    
    End If
    
    End Sub
    *

    ... that says after the last project's hours are entered it is compared to the total hours worked. Although the message box works, I need it to go back to the last entry and force the user to re-enter BEFORE moving on. I've tried GoTo, Undo, and looking up On Error statements - NOTHING works! Any ideas?


    *
    Last edited by Rabbit; Sep 9 '13, 09:04 PM. Reason: Please use code tags when posting code or formatted data.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    Then you need to use the BeforeUpdate event and not the AfterUpdate event. Then you can set Cancel to true to stop the field from updating. If you then combine that with setting the field to always need a value, it will stop them from moving to another record.

    Comment

    • percman
      New Member
      • Sep 2013
      • 8

      #3
      But if it's comparing the last value entered in addition to the others, wouldn't have to be after update? How could it be a before update ifthe value has not gone in? Guess I'm still a little confused...

      Here's another similar situation. I have a button at the bottom of the form that sends all of the information into a table. The information sent the table is useless without a name. The button is defined and all of the fields are going into the table without a problem, but I need to have a control that says it cannot be submitted without a name. The button is set up with with an "On Click procedure, but I can't figure out how to write an If/Then statement that would prevent this.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        If you use the After Update, the value is already saved. If you use the Before Update, you can prevent the value from being saved. If you want to prevent a value from being saved, that's the place to do it. Which is what it sounds like you want to do since they are inputting an incorrect entry, it shouldn't save.

        We limit threads to one question on this forum. Please create a new thread for your second question.

        Comment

        Working...