Results 1 to 6 of 6
  1. #1
    Khalil Handal is offline Competent Performer
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2016
    Posts
    237

    Required fields in a table, Message to add value

    Hi to all,


    I started a database table tblPersonnel with BirthDate field. This field is required.

    I created a form frmPersonnel, the error message shown when birth date field is null was: You must enter a value in the 'tblPersonnel.BirthDate' field

    How to do it on the form level for the txtBirthDate? How can we deal with the case of four required fields?


    Khalil
    Last edited by Khalil Handal; 05-05-2022 at 12:53 AM. Reason: mistakes

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Remove the validation from the table.
    Validate in the form instead. Most people use the tag property of the control to indicate required controls for validation.
    Try using google, plenty of examples out there.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    Join Date
    May 2018
    Location
    Living in Scotland UK
    Posts
    1,563
    Hi Khalil

    This is the best method as suggested by Welshgasman:-

    Validate Controls using Tag


    In the Before Update of the Form

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
     
    10    On Error GoTo Form_BeforeUpdate_Error
     
            Dim ctl As Control
            Dim CName As String
           
    20      For Each ctl In Me.Controls
    30        Select Case ctl.ControlType
              Case acTextBox, acComboBox, acListBox, acOptionGroup, acCheckBox
    40          If ctl.Tag = "Reqd" Then
    50            If Nz(ctl, "") = "" Then
    60              CName = ctl.Controls(0).Caption
    70              MsgBox "Following field is required: " & vbCrLf & vbCrLf & CName
    80              Cancel = True
    90              ctl.SetFocus
    100             Exit Sub
    110           End If
    120         End If
    130       End Select
    140     Next ctl
    150     On Error GoTo 0
    160     Exit Sub
     
    Form_BeforeUpdate_Error:
    170       MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure Form_BeforeUpdate, line " & Erl & "."
    End Sub
    You can PM me if you need further help.
    Good Reading https://docs.microsoft.com/en-gb/off...on-description

  4. #4
    Micron is online now Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,789
    This is the best method as suggested by Welshgasman
    I'd say pretty good, but IMO, better to loop over all controls and append with line feeds to a string variable and present them all at once rather than perhaps multiple messages. The Select Case block is better than a long IF statement though. I'm going to steal that one!

    What should be noted re: that code is that the labels must be attached to the controls, else it will raise an error.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  5. #5
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,651
    I agree with Micron. I set the borders red and launch 1 messagebox for all. Additionally its a function so it returns true if missing fields and false if its good. Then you can just use Cancel = ValidateForm(Me, "V8").

    Code:
    Public Function ValidateForm(frm As Form, TagCharacter As String) As Boolean
    'validated controls must have a label. Ok to use a hidden label if needed.
    'Returns True if there is missing fields
    
    
        Dim ctl As Control
        Dim flg As Boolean
        Dim strOut As String
    
    
        flg = False
    
    
        For Each ctl In frm.Controls
    
    
            If ctl.Tag = TagCharacter Then
                If Nz(ctl.value, "") = "" Then
                    flg = True
                    ctl.BorderColor = vbRed
                    strOut = strOut & Space(10) & "* " & ctl.Controls.Item(0).Caption & vbNewLine
                Else
                    ctl.BorderColor = vbBlack
                End If
    
    
            End If
        Next
    
    
        If flg = True Then
            MsgBox "The following field(s) must be completed:" & vbNewLine & strOut
        End If
    
    
        ValidateForm = flg
    
    
    End Function
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  6. #6
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Although you need to set it, I use the datasheet caption property so a label doesn't need to be present;
    This also gathers the missing values into a single message box, and relies on a tag property being set. It alters the background colour to yellow.
    It also resets the colours on the controls so they don't get left behind if they fail validation the second time.
    Code:
    Function ValidateRecs() As Boolean
        
        Dim sControls As String
        Dim ctl As Control
        Dim ctlColour As Long
        
         
        For Each ctl In Me.Controls
            If ctl.Tag = "Req" Then     'Reset the colours
                If ctl.BackColor = vbYellow Then
                    ctlColour = GetHexColor("#C6D9F1") ' light blue
                    ctl.BackColor = ctlColour
                End If
                If ctl.Enabled Then
                    'Debug.Print ctl.Name, ctl.value, ctl.Properties("DataSheetCaption")
                    If IsNull(ctl.value) Then
                        sControls = sControls & ctl.Properties("DataSheetCaption") & vbCrLf
                        ctl.BackColor = vbYellow
                    End If
                End If
            End If
        Next ctl
        
        ValidateRecs = True
        bOkClose = True
        If Len(sControls & "") > 0 Then
            Box "The following fields require data before the completion date can be entered;" & vbCrLf & vbCrLf & sControls, vbInformation + vbOKOnly, "Missing Data!", , , 0, , 0, 0
            ValidateRecs = False
            bOkClose = False
        End If
        
    End Function
    Be aware I use a custom message box hence the slightly different layout for the message box line.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 06-16-2015, 03:52 AM
  2. Table validation for empty fields
    By slufoot80 in forum Database Design
    Replies: 2
    Last Post: 11-11-2013, 05:33 PM
  3. Replies: 11
    Last Post: 01-28-2013, 12:11 PM
  4. Required Fields in a Form
    By Alaska1 in forum Access
    Replies: 3
    Last Post: 12-23-2010, 01:41 PM
  5. Replies: 2
    Last Post: 10-23-2010, 09:38 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums