Solved

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
8
353 Views
Last Modified: 2013-02-05
Hello-

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 !
0
Comment
Question by:Daniel Van Der Werken
  • 4
  • 3
8 Comments
 
LVL 84

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 50 total points
Comment Utility
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.
0
 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
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 & ";"
                Else
               
            End If
            end if
    End If



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

    msgbox "Please complete all fields!"
    Exit Sub
    Else
   
End If
0
 
LVL 19

Author Comment

by:Daniel Van Der Werken
Comment Utility
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.
0
 
LVL 19

Author Comment

by:Daniel Van Der Werken
Comment Utility
Capricorn1-

I tried using the code you gave but it didn't work. I am not receiving any type of alert message or any response.
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 119

Expert Comment

by:Rey Obrero
Comment Utility
did you place the word "Mandatory" in the Tag property of your controls ?

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

Author Comment

by:Daniel Van Der Werken
Comment Utility
Capricorn1-

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 ?
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 450 total points
Comment Utility
< 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()
0
 
LVL 19

Author Comment

by:Daniel Van Der Werken
Comment Utility
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.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now