• vba code control defaults (access 97)

    Author
    Topic
    #368365

    hi all
    example: in the code window, i select a control from the left drop down. let’s just say that it is a text box. access defaults to “before update” as the event to start with. is there any way to change what it defaults to for any particular type of control? i don’t know about you but i have used “before update” perhaps once in 2 years of programming. i am constantly using “after update” and would love if it could default to that instead. if nothing else, perhaps default to nothing at all – make you select the event yourself. any help would be appreciated

    thanks in advance

    Viewing 1 reply thread
    Author
    Replies
    • #577055

      If there is already a routine for that control, it will default to that routine. Otherwise, you get them in the order MS has decided to give them to you. AFAIK there is no way to change that. Just delete the shell it drops in there for BeforeUpdate and select something else instead.

      FWIW, I use BeforeUpdate whenever I really need to validate a control before letting the user move on. In VB the equivalent event is called Validate.

      • #577158

        thanks charlotte,
        most of the time, i just need an event to happen after a control updates. if i need to validate, i check afterwards or check when the “OK” button is pressed. if you could give me some examples of how and where you would use before update it would be appreciated. i would hate to be not using something that could help me out

        thanks

        • #577181

          If you want to be sure that the value entered in the control is a number between 1 and 10, for example:

          Private Sub TextBox1_BeforeUpdate(Cancel As Integer)
          If Me.TextBox1 10 Then
          MsgBox “You must enter a number between 1 and 10.”
          Cancel = True
          End If
          End Sub

          This will let the user know they goofed and will keep them in the field so they can fix it. The only way out is for them to hit ESC to undo the value.

          The BeforeUpdate event of a Form is even more useful because you can test controls to see if they skipped entering a value that has to be there.

          Private Sub Form_BeforeUpdate(Cancel As Integer)
          If IsNull(Me.TextBox1) Then
          Msgbox “You must enter a value between 1 and 10.”
          Cancel = True
          Me.TextBox1.Setfocus
          End If
          End Sub

          • #577184

            hmm, i guess i never realized that. it is useful to check the value before it gets updated in the table as opposed to after its updated and going back to change it if its wrong. thanks for your help.

    • #577077

      Like Charlotte, I also use BeforeUpdate quite a bit; and I’m sure she is just as surprised as I am that you aren’t using it more. Sometimes the Order Of Events is not perfectly clear (like the Form_BeforeInsert event, for example), and you might be overlooking a valuable tool.

    Viewing 1 reply thread
    Reply To: vba code control defaults (access 97)

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

    Your information: