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
357 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
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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 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()
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Most if not all databases provide tools to filter data; even simple mail-merge programs might offer basic filtering capabilities. This is so important that, although Access has many built-in features to help the user in this task, developers often n…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
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…

806 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