Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
Medium Priority
?
363 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
[X]
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
8 Comments
 
LVL 85

Assisted Solution

by:Scott McDaniel (Microsoft Access MVP - EE MVE )
Scott McDaniel (Microsoft Access MVP - EE MVE ) earned 200 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.
0
 
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 & ";"
                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 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.
0
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
LVL 20

Author Comment

by:Daniel Van Der Werken
ID: 38855643
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
 
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.
0
 
LVL 20

Author Comment

by:Daniel Van Der Werken
ID: 38855830
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 120

Accepted Solution

by:
Rey Obrero (Capricorn1) earned 1800 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()
0
 
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.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
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 …
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…

604 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