473,394 Members | 1,750 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,394 software developers and data experts.

Ensure all fields are filled out

Hi,

I have a form in Access.

Before a user clicks Add Record, I would like a check to be performed to ensure the fields do not equal blank.

The forms contains fields which are bound.

Many thanks
Sep 8 '15 #1
1 1946
Seth Schrock
2,965 Expert 2GB
There are a couple of different ways to do this. You can use validation rules on each control to make sure that it isn't Null (see Allen Browne Validation Rules). Or you can use VBA and create a set of If statements in the form's BeforeUpdate event to test teach control to make sure it isn't blank. I actually created a procedure that does this for me with just a few lines of code for each form and lets me pick which controls are required. To use this method, create a new Class Module and name it clsRequired and then paste the following code into it:
Expand|Select|Wrap|Line Numbers
  1. '**************************************************************************************************
  2. 'Author         : Seth Schrock
  3. 'Date           : 10/30/13
  4. 'Purpose        : Set the fore color of the labels connected to controls that are set
  5. '                 to be required to red if they haven't been populated and back to
  6. '                 black when they have been populated.
  7. '
  8. 'Instructions  1: When calling the the CheckControls procedure, frm is the calling form's
  9. '                 name.
  10. '
  11. '              2: TxtCboLstChkOpt is a flag variable that allows you to choose
  12. '                 which types of controls to check if they are required.  The calling code just
  13. '                 need to add up the control types listed as properties of this class.
  14. '                 For example, to check textboxes and checkboxes, you would just pass the value
  15. '                 .txt + .chk    Order doesn't matter.
  16. '
  17. '              3: The tag parameter allows you to specify what the tag property value is
  18. '                 that specifies that control as being required.  By default the value
  19. '                 is "req".
  20. '
  21. '**************************************************************************************************
  22.  
  23.  
  24. '**************************************************************************************************
  25. 'SETUP CONSTANTS
  26. '
  27. 'Error Color:
  28. Const errColor As Long = 255
  29. '
  30. 'Populated Color:
  31. Const okColor As Long = 855309
  32. '
  33. 'Control OK Border Color
  34. Const okBorderColor As Long = 10921638
  35. '
  36. 'Control Type Values
  37. Const iTxt = 16
  38. Const iCbo = 8
  39. Const iLst = 4
  40. Const iChk = 2
  41. Const iOpt = 1
  42. '
  43. '**************************************************************************************************
  44.  
  45.  
  46.  
  47. 'Stored Values
  48. Dim blnCompleted As Boolean
  49. Dim strMesssage As String
  50.  
  51. Private msg As New clsMessage
  52.  
  53.  
  54. Private Sub Class_Initialize()
  55. blnCompleted = True
  56.  
  57. End Sub
  58.  
  59. Public Sub CheckControls(frm As String, TxtCboLstChkOpt As Long, Optional tag As String = "req")
  60. 'On Error GoTo Error_Handler
  61.  
  62. Dim bTxt As Boolean
  63. Dim bCbo As Boolean
  64. Dim bLst As Boolean
  65. Dim bChk As Boolean
  66. Dim bOpt As Boolean
  67. Dim ctl As Control
  68.  
  69.  
  70. 'Check which control types to check
  71. bTxt = (TxtCboLstChkOpt And iTxt)
  72. bCbo = (TxtCboLstChkOpt And iCbo)
  73. bLst = (TxtCboLstChkOpt And iLst)
  74. bChk = (TxtCboLstChkOpt And iChk)
  75. bOpt = (TxtCboLstChkOpt And iOpt)
  76.  
  77.  
  78. 'Mark controls that are required and empty
  79.  
  80. For Each ctl In Forms(frm).Controls
  81.     If ((bTxt And ctl.ControlType = acTextBox) Or (bCbo And ctl.ControlType = acComboBox) Or _
  82.        (bLst And ctl.ControlType = acListBox) Or (bChk And ctl.ControlType = acCheckBox) Or _
  83.        (bOpt And ctl.ControlType = acOptionGroup)) And ctl.Properties("Visible") = True Then
  84.  
  85.         With Forms(frm).Controls(ctl.Name)
  86.  
  87.             If .tag = tag Then
  88.  
  89.                 '.Controls.Item(0) refers to the attached label
  90.                 If .Controls.Count > 0 Then
  91.                     If (ctl.ControlType <> acCheckBox And .Value & "" <> "") Or _
  92.                        (ctl.ControlType = acCheckBox And .Value = True) Then
  93.                         .Controls.Item(0).ForeColor = okColor
  94.                     Else
  95.                         .Controls.Item(0).ForeColor = errColor
  96.                         msg.AddText .Controls.Item(0).Caption
  97.                         blnCompleted = False
  98.                     End If
  99.                 Else
  100.                     If (ctl.ControlType <> acCheckBox And .Value & "" <> "") Or _
  101.                        (ctl.ControlType = acCheckBox And .Value = True) Then
  102.                         .BorderColor = okBorderColor
  103.                     Else
  104.                         .BorderColor = errColor
  105.                         msg.AddText .Name
  106.                         blnCompleted = False
  107.                     End If
  108.                 End If
  109.  
  110.             End If
  111.  
  112.         End With
  113.  
  114.     End If
  115.  
  116. Next ctl
  117.  
  118. strMesssage = msg.Message
  119.  
  120. Exit_Procedure:
  121.     Exit Sub
  122.  
  123. Error_Handler:
  124.     If Err.Number = 2467 Then Resume Next
  125.  
  126. End Sub
  127.  
  128. Public Property Get Completed() As Boolean
  129.     Completed = blnCompleted
  130. End Property
  131.  
  132. Public Function Message() As String
  133. Dim strMsg As String
  134.  
  135. strMsg = "Please fill in the following field"
  136. If msg.ItemCount > 1 Then
  137.     strMsg = strMsg & "s"
  138. End If
  139. strMsg = strMsg & ": " & msg.Message
  140.  
  141. Message = strMsg
  142.  
  143. End Function
  144.  
  145. Public Property Get txt() As Integer
  146.     txt = iTxt
  147. End Property
  148.  
  149. Public Property Get cbo() As Integer
  150.     cbo = iCbo
  151. End Property
  152.  
  153. Public Property Get lst() As Integer
  154.     lst = iLst
  155. End Property
  156.  
  157. Public Property Get chk() As Integer
  158.     chk = iChk
  159. End Property
  160.  
  161. Public Property Get opt() As Integer
  162.     opt = iOpt
  163. End Property
  164.  
Then create another Class Module and name it clsMessage and paste the following code into it:
Expand|Select|Wrap|Line Numbers
  1. '---------------------------------------------------------------------------------------
  2. ' Module    : clsMessage
  3. ' Author    : sschrock
  4. ' Date      : 11/7/2013
  5. ' Purpose   : This class module will be passed a series of text strings and then will
  6. '             return the collection as a single string will proper comma placements
  7. '             as well as any additional words needed to make it grammatically correct.
  8. '
  9. '---------------------------------------------------------------------------------------
  10.  
  11.  
  12. Private intUpper As Integer
  13. Private intLower As Integer
  14. Private arrText() As String
  15.  
  16.  
  17.  
  18. Private Sub Class_Initialize()
  19.  
  20. intUpper = 0
  21. ReDim arrText(0) As String
  22.  
  23. End Sub
  24.  
  25. Public Sub AddText(Text As String)
  26.  
  27. intUpper = intUpper + 1
  28. ReDim Preserve arrText(intUpper) As String
  29. arrText(intUpper) = Text
  30.  
  31. End Sub
  32.  
  33. Public Function Message(Optional addPeriod As Boolean = False) As String
  34. Dim i As Integer
  35. Dim strMsg As String
  36.  
  37. Select Case intUpper
  38.     Case 1
  39.         strMsg = arrText(1)
  40.  
  41.     Case 2
  42.         strMsg = arrText(1) & " and " & arrText(2)
  43.  
  44.     Case Else
  45.         For i = 1 To intUpper
  46.             Select Case i
  47.                 Case 1
  48.                     strMsg = arrText(i)
  49.  
  50.                 Case Is <= (intUpper - 1)
  51.                     strMsg = strMsg & ", " & arrText(i)
  52.  
  53.                 Case Is = intUpper
  54.                     strMsg = strMsg & " and " & arrText(i)
  55.  
  56.             End Select
  57.         Next
  58.  
  59. End Select
  60.  
  61. If addPeriod Then
  62.     strMsg = strMsg & "."
  63. End If
  64.  
  65. Message = strMsg
  66.  
  67. End Function
  68.  
  69. Public Property Get ItemCount() As Integer
  70.     ItemCount = intUpper
  71. End Property
  72.  
Then, for each control that you want to be required in the form, put the text Req in the Tag property (found at the bottom of the Other tab in the property window). To utilize all this code, you would put the following in your Add Record button's OnClick event:
Expand|Select|Wrap|Line Numbers
  1. Dim clsReq As clsRequired
  2. Set clsReq = New clsRequired
  3.  
  4. clsReq.CheckControls Me.Name, clsReq.txt + clsReq.cbo 'this checks just textboxes and comboboxes
  5.  
  6. If Not clsReq.Completed Then
  7.     MsgBox clsReq.Message
  8. Else
  9.     'perform your code here
  10. End If
  11.  
  12. Set clsReq = Nothing
Sep 8 '15 #2

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

Similar topics

3
by: Unregistered | last post by:
Hi, First, excuse me for my bad English, I'm Dutch.... :-) I know there's a lot of information about disabeling form fields, but cannot find the script I need: I'm looking for a script...
2
by: Rick Knight | last post by:
I need to have a couple of form fields filled in according to the value of a third field before the entire form is submitted. I have searced and I can't find any information on how to do this. Is...
1
by: bobby1041 | last post by:
Hi All! I am trying to make several fields in a HTML form validated, but only when a dynamic checkbox is selected. I am not sure how to do this. Here is a snippet of the dynamic checkbox...
5
by: C | last post by:
Hi, I have a web form which is quite long. My first field is required. If my user submits the form the page just sits there and the do not see my validation message at the top of my page. ...
1
by: Zameer Syed | last post by:
I have a webform in which users enter their information and there is a button at the end of form,which when clicked should open a word document with fields filled in dynamically with the...
1
by: griemer | last post by:
I have a database like this id, field1,field2,field3,field4,field5 Database contains 100 rows, some rows have no fields filled, some 1field , some 2 fields etc. How would i count the...
2
by: fstenoughsnoopy | last post by:
I have a customer order database and I need to pull a customers information, ie first name, last name, address, city, state, zip, phone, etc, into the oder table. i don't know how to go about...
16
by: mtuller | last post by:
I am creating a page for nomination and want to let the information pass if any field are filled out, but if none are filled out, a message will appear. I can't get the check to happen on multiple...
5
by: apking | last post by:
hi Friends, how to filled values from mysql database using php in html form.actually when user wants to edit his account.when fields filled with old details from musql database for example :...
4
by: Samuel Murray | last post by:
G'day everyone I'm not a JavaScript programmer, so I wonder if you could point me in the right direction or simply tell me how to do the following. I'd like to submit a patch for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.