Results 1 to 3 of 3
  1. #1
    easyrider is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64

    Force a user to enter data on a form

    Good morning! I've created a form to add a record to a table with unbound text boxes. The add record event is handled by a DoCmd.RunSQL INSERT statement.



    Before running the INSERT command, I would like to make sure that the user has entered something in each text box before moving on to another text box by any means (tab, mouse-click, etc). If they try to move out of a blank text box, throw a warning and then return focus to the blank control.

    I have tried most of the pertinent Events for the control (Click, Before Update, After Update, Exit). With any of those event subs, the MsgBox displays but focus does not go back to the blank control - instead, focus stays on the next control in the tab order.

    Here is the code for the Exit event:

    Private Sub VinNum_Exit(Cancel As Integer)
    If IsNull(VinNum) Then
    MsgBox "VIN # required.", vbExclamation, "Invalid Entry"
    VinNum.SetFocus
    End If
    End Sub


    Thoughts?

  2. #2
    Gicu's Avatar
    Gicu is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2015
    Location
    Kelowna, BC, Canada
    Posts
    4,250
    Use the BeforeUpdate event and Cancel it if the control is empty.
    https://documentation.help/MS-Access...foreUpdate.htm
    Cheers,
    Vlad Cucinschi
    MS Access Developer
    http://forestbyte.com/

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    I would like to make sure that the user has entered something in each text box before moving on to another text box by any means
    I would not do that as it's too constricting to the point that it will be a PITA if you try to control the exact sequence of entries (unless you absolutely need to do that). Instead, use a form level event to trap any missing but required entries. I typically use .Tag property for these fields (set it to reqd) and loop over the form controls looking for that value. If found and the control has no value, then prompt. I'd rather build a list if there are many controls and present one message so that user doesn't get umpteen prompts every time they fire the event because they're fixing one at a time. Your way, you need an event for every control -maybe LostFocus would be better but I don't see why yours doesn't work except to say I'd always use Me.VinNum.SetFocus

    Perhaps you'd need 2 events at the form level: 1 if you save the record with a button click and the other would be the unload event because you can cancel it if data is missing. You don't have to repeat the code for both events (although that would be easier) as there are other ways to consolidate the code. One would be to have the button perform a save & close action rather than just a save. That way, you only need the unload event.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

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

Similar Threads

  1. Replies: 1
    Last Post: 09-01-2017, 10:28 AM
  2. Replies: 15
    Last Post: 04-10-2017, 01:09 PM
  3. Replies: 5
    Last Post: 08-15-2012, 06:01 PM
  4. Replies: 2
    Last Post: 06-01-2012, 08:10 AM
  5. Force user to enable macros
    By tuna in forum Security
    Replies: 1
    Last Post: 05-09-2010, 04:04 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