473,416 Members | 1,550 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,416 software developers and data experts.

Validate Required Fields on a Form

Hi All,
I have a form and need to make sure that 5 of those fields have data before the record is saved.
On the form the fields are in the following Tab order:
DATE NOTIFIED, DATE OF INCIDENT, FNAME, LNAME, AGENCY

I have some code on the BeforeUpdate event of the Form which is shown below:

When i test for a value in those columns by hitting the save button(with no data entered), the first field that it detects needs data is Date of Incident. I want it to validate each field based on it's tab position, so it should say DATE OF NOTIFICATION before DATE OF INCIDENT. Maybe I am missing something in my code, or maybe there is a better way to validate these fields. Any Help is appreciated. Thanks in advance.


Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_BeforeUpdate(Cancel As Integer)
  2. Dim Msg As String, Style As Integer, Title As String
  3.    Dim DL As String, ctl As Control
  4.  
  5.    DL = vbNewLine & vbNewLine
  6.  
  7.    For Each ctl In Me.Controls
  8.  
  9.      If ctl.Tag = "?" Then
  10.        If Trim(ctl.Value & "") = "" Then
  11.  
  12.         Msg = "'" & ctl.Name & "' is Required!" & DL & _
  13.                "Please enter a value . . ."
  14.          Style = vbInformation + vbOKOnly
  15.          Title = "Required Data Missing! . . ."
  16.          MsgBox Msg, Style, Title
  17.          ctl.SetFocus
  18.          Cancel = True
  19.          Exit For
  20.          End If
  21.       End If
  22.    Next
  23.  
  24. End Sub
Feb 28 '13 #1

✓ answered by Seth Schrock

Please read Before Posting (VBA or SQL) Code

I recently created a function for one of my databases to make sure that the fields that I needed to populated were in fact populated. It doesn't give the control name in the error message, but if you are following a naming convention, you probably wouldn't want the control name anyway. The control's label's caption could work, but I just make the label red and then tell the user to enter data in the highlighted fields.
Expand|Select|Wrap|Line Numbers
  1. Private Function VerifyFields() As Boolean
  2. On Error GoTo Error_Handler
  3.  
  4. VerifyFields = True
  5.  
  6. If Me.txtBorrower & "" = "" Then
  7.     Me.txtBorrower_Label.ForeColor = vbRed
  8.     VerifyFields = False
  9. Else
  10.     Me.txtBorrower_Label.ForeColor = 8355711
  11. End If
  12.  
  13. If IsNull(Me.cboLoanType) Then
  14.     Me.cboLoanType_Label.ForeColor = vbRed
  15.     VerifyFields = False
  16. Else
  17.     Me.cboLoanType_Label.ForeColor = 8355711
  18. End If
  19.  
  20. If Me.cboCounty.Visible = True Then
  21.     If IsNull(Me.cboCounty) Then
  22.         Me.cboCounty_Label.ForeColor = vbRed
  23.         VerifyFields = False
  24.     Else
  25.         Me.cboCounty_Label.ForeColor = 8355711
  26.     End If
  27. End If
  28.  
  29.  
  30. Exit_Procedure:
  31.  
  32.     Exit Function
  33.  
  34.  
  35. Error_Handler:
  36.     Call ErrorMessage(Err.Number, Err.Description, "Form_frmRequestProcessor: VerifyFields")
  37.     Resume Exit_Procedure
  38.     Resume
  39.  
  40.  
  41. End Function
You can ignore the error trapping at the end. I called this from a button's OnClick event like you are planning on doing.
Expand|Select|Wrap|Line Numbers
  1. If VerifyFields Then
  2.     'Save your Record here
  3. Else
  4.     MsgBox "Please fill in the highlighted fields."
  5. End If
With this code, the highlighted field would remain highlighted until the save button was clicked again, but you could call the VerifyFields function in each of the required controls' AfterUpdate events and it would keep the highlights accurate.

3 9478
Seth Schrock
2,965 Expert 2GB
Please read Before Posting (VBA or SQL) Code

I recently created a function for one of my databases to make sure that the fields that I needed to populated were in fact populated. It doesn't give the control name in the error message, but if you are following a naming convention, you probably wouldn't want the control name anyway. The control's label's caption could work, but I just make the label red and then tell the user to enter data in the highlighted fields.
Expand|Select|Wrap|Line Numbers
  1. Private Function VerifyFields() As Boolean
  2. On Error GoTo Error_Handler
  3.  
  4. VerifyFields = True
  5.  
  6. If Me.txtBorrower & "" = "" Then
  7.     Me.txtBorrower_Label.ForeColor = vbRed
  8.     VerifyFields = False
  9. Else
  10.     Me.txtBorrower_Label.ForeColor = 8355711
  11. End If
  12.  
  13. If IsNull(Me.cboLoanType) Then
  14.     Me.cboLoanType_Label.ForeColor = vbRed
  15.     VerifyFields = False
  16. Else
  17.     Me.cboLoanType_Label.ForeColor = 8355711
  18. End If
  19.  
  20. If Me.cboCounty.Visible = True Then
  21.     If IsNull(Me.cboCounty) Then
  22.         Me.cboCounty_Label.ForeColor = vbRed
  23.         VerifyFields = False
  24.     Else
  25.         Me.cboCounty_Label.ForeColor = 8355711
  26.     End If
  27. End If
  28.  
  29.  
  30. Exit_Procedure:
  31.  
  32.     Exit Function
  33.  
  34.  
  35. Error_Handler:
  36.     Call ErrorMessage(Err.Number, Err.Description, "Form_frmRequestProcessor: VerifyFields")
  37.     Resume Exit_Procedure
  38.     Resume
  39.  
  40.  
  41. End Function
You can ignore the error trapping at the end. I called this from a button's OnClick event like you are planning on doing.
Expand|Select|Wrap|Line Numbers
  1. If VerifyFields Then
  2.     'Save your Record here
  3. Else
  4.     MsgBox "Please fill in the highlighted fields."
  5. End If
With this code, the highlighted field would remain highlighted until the save button was clicked again, but you could call the VerifyFields function in each of the required controls' AfterUpdate events and it would keep the highlights accurate.
Feb 28 '13 #2
zmbd
5,501 Expert Mod 4TB
AB may have a non-vba solution:
Validation Rules You can use a validation text to relate the information requirements to the user. The user then will have the option to [Esc] out of the record or enter the data.
As to do this at the table or the form level, that depends; however, I tend to do this at the table level and trap for errors if needed.
Feb 28 '13 #3
thanks Seth and zmbd for the quick replies. I think now i have a better understanding of what path to take.
Mar 1 '13 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

14
by: Oleg | last post by:
Hello there: I've been trying to create two different sets of required fields in one form and to use a radiobutton as sort of a switcher between these sets. In my HTML form there are two...
2
by: Cliff R. | last post by:
Hello, I have a form that has a few required fields and also an "agree to terms" checkbox that must be required. I have used Javascripts for both functions individually, but I need a little help...
2
by: bufbec1 | last post by:
I am pretty good with Access, but do not understand VBA. I have researched this topic and see only VBA answers, so I hope someone can help with my specific question. I have 2 fields for an...
1
by: Tien | last post by:
Hi, By default, all controls must have a RequiredField control in order to use more complex validation control e.g. RangeValidator, so how can I use validation controls with non-required fields?...
3
by: kaosyeti | last post by:
i have a form that has required fields and i have a delete button, created by the wizard, that has this code for onclick: Private Sub cmddeleterecord_Click() On Error GoTo...
1
by: Dave | last post by:
Hi, I'm using .NET 1.1 and wondering if I can use RequiredFieldValidators with required fields inside user controls. The page is a type of tabbed panel layout with the divs show/hidden using...
1
by: swingingming | last post by:
Hi, I made a form based on one table that has several required fields. If in the form, I leave some of them blank and close the form using 'X', I get 2 warning messages, 1st is "... field is...
1
by: sdavis1970 | last post by:
I am working on an Access 2002 database where one of the tables has five required fields making up the key. There is a form that is linked to this table which is used for adding new records. ...
5
by: Cubicle Intern | last post by:
Hi, I have a form with multiple fields that confirmed before the form is submitted (ex. email field needs to be completed before the form can be submitted). Once the required fields are...
1
by: marcher | last post by:
please .. i have this form <FORM action="add.php" method="post"> <TABLE width="100%" border=0> <TBODY> <TR> <TD align=center><FONT color=#ff0000>* </FONT>title:<BR><INPUT size=40 ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.