?
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
?
361 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
What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

 
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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Suggested Courses

765 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