How to enforce text fields as required in a form before saving or exiting or updating in MS Access 2010

Posted on 2013-02-05
Last Modified: 2013-02-05

I am building a DB in Access 2010. I am still new to DB programming.

On my forms I have various buttons such as a Save Record button, a Add New Record button and 2 navigation arrow buttons. When the client using the DB inserts data into the text fields and attempts to save or update the record, he must enter text into certain fields or cancel that process without saving any changes.

If the client enters data in an existing record or a new record without saving, I want to display an error message of some sort so that the client must enter data into the required fields.

To summarize:
I want to make certain fields required before saving the form.

Thank you in advance !
Question by:Daniel Van Der Werken
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
LVL 85

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 50 total points
ID: 38855458
You can use the Form's Before_Update event for this:

Private Sub Form_BeforeUpdate(Cancel As Integer)
  If Nz(Me.SomeTextBox,"") = "" Then
    Msgbox "You must fill in SomeTextbox"
    Cancel = True
  End If
End Sub

Note you can also do this at the Control level (i.e. the Textbox also has a Before Update event that can be Canceled). Whether you do that depends on your own personal needs and how you want the app to react to the user.
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38855523
use the tag property of the controls, use this code in the before update event of the form

Dim sCtlName As String, NullCtl As String, i As Integer
sCtlName = ""
Dim ctl As Control
For Each ctl In Me.Controls

    If ctl.ControlType = acComboBox Or ctl.ControlType = acTextBox Then
            If ctl.Tag = "mandatory" then
            If IsNull(Me(ctl.Name)) Or Len(Me(ctl.Name)) = 0 Then
                sCtlName = sCtlName & ctl.Name & ";"
            End If
            end if
    End If

Next ctl
If Len(sCtlName) > 0 Then
    NullCtl = Mid(sCtlName, 1, InStr(sCtlName, ";") - 1)

    msgbox "Please complete all fields!"
    Exit Sub
End If
LVL 20

Author Comment

by:Daniel Van Der Werken
ID: 38855608
Hi, thanks for the quick reply.

I entered the code you suggested. I placed it in Before Update field for the form and I do receive the message from the code when I try to exit the form (i.e. returning to design view).

What I was trying to do was display the message when the user presses the save button.

And regarding:
Note you can also do this at the Control level (i.e. the Textbox also has a Before Update event that can be Canceled). Whether you do that depends on your own personal needs and how you want the app to react to the user.

Could you tell me a little more about this. This might be something I may want to try as well.

Thank you.
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

LVL 20

Author Comment

by:Daniel Van Der Werken
ID: 38855643

I tried using the code you gave but it didn't work. I am not receiving any type of alert message or any response.
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38855676
did you place the word "Mandatory" in the Tag property of your controls ?

sorry, forgot to mention this in my first post.
LVL 20

Author Comment

by:Daniel Van Der Werken
ID: 38855830

Okay, I made the change you suggested.

I am encountering the same problem I was when I tried LSMConsulting's advice.
When I leave a text field blank (such as the Company Name field) and then try to click the Save Record button, it does save. When I try to close the form the alert message "Please complete all fields!" does display but not until after I try to close the form. And then the form closes without forcing the user to fill in the text field.

Do you have any further suggestions or advice ?
LVL 120

Accepted Solution

Rey Obrero (Capricorn1) earned 450 total points
ID: 38855851
< Save Record button>
so you are using a button to save the records,

move the codes in the click event of the button, place the codes right after the line

private sub button_click()
LVL 20

Author Comment

by:Daniel Van Der Werken
ID: 38856001
Amazing ! Worked like a charm !!
Thank you very much.

I had to add the code you suggested into the OnClick event for the Save Record button, then add "Mandatory" to the Tag controls in Properties for each field that would require some entry by the client/user.

Saved my day. Thanks.

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question