Link to home
Start Free TrialLog in
Avatar of billcute
billcute

asked on

Tag property question

I have fucntions that use the "Tag Property" to check for mandatory fields.
This is how it works.
(1). User fills in data in his form and then clicks the "AddNew" button.
     The code in the button addnew first check if all the mandatory fields
       have been completely filled out by the user.
(2). If the user is not missing any field, the code simply commits saving of the
      new record to the table.
(3). If there are some missing fields, a flag message shows up on the screen
      informing the user the missing fields and then exit allowing user to fill
      in data in the required fields.
 
Form Design:
(a). Each designated control that is considered mandatory field has its tag property set to
       either [ne], [1ne], [2ne], [3ne] based on the criteria each control "ne's" represent
(b). Each control that is tagged with the [ne's] also utilize it's label name for displaying
      the correspondent label "CAPTION" of the of the controls (i.e the missing fields).
 
This control works fine until recently  I discovered a snag or let say a bug in the code.
Testing:
(1). Whe user opens a blank form in the "new record" mode and clicks the "btnAddNew" -
      it immediately display all missing fields - and the controls also show colors that makes it easy for user  
     to identify which control is designated as  "mandatory fields"
....... this appears to be working right..,,,
 
(2). Unfortunately this is not so as further tests shows inconsistency in the display of the missing fields.
For example:
Aassuming that the user fills in the first two form fields out of the six or eight mandatory fields and decides to click the btnAddNew....to my dismay, without completely filling the missing fields the records are saved in the table and no message flag is displayed showing the missing fields before saving the record.

The following are the two major code associated with the mandatory field function.
' *********************************
Public Function ChkCtl(ByRef mCtl As Control) As Boolean
  ChkCtl = True
    If mCtl.Properties("TabIndex") <= Screen.ActiveControl.Properties("TabIndex") Then  ' additional
        Select Case ExtractTag(mCtl.Tag, 2)
            Case "ne"
                If Len(Nz(mCtl.Value)) = 0 Then ChkCtl = False
            Case "1ne"
                If Len(Screen.ActiveForm.txtPermitNo) > 0 And Len(Nz(mCtl.Value)) = 0 Then ChkCtl = False
            Case "2ne"
                If Len(Screen.ActiveForm.cboSConnType) > 0 And Len(Nz(mCtl.Value)) = 0 Then ChkCtl = False
            Case ">0"
                If Not Nz(mCtl.Value) > 0 Then ChkCtl = False
            Case "tf"
                If Nz(mCtl.Value, 99) <> 0 And Nz(mCtl.Value, 99) <> -1 Then ChkCtl = False
        End Select
  End If
  Exit Function
End Function

Function MandatoryFields() As Boolean      
  Dim myCtl As Control
  Dim myCtlList, myCtlName As String
    MandatoryFields = True
  ' First the basic controls
  For Each myCtl In Me.Controls
    If Len(myCtl.Tag) > 1 Then
       If ChkCtl(myCtl) = False Then
          myCtlList = myCtlList & Me.Controls("l_" & myCtl.Name).Caption & ", "
            On Error Resume Next
          myCtl.BackColor = 16776960
            On Error GoTo 0
       Else
            On Error Resume Next
            myCtl.BackColor = 13434879
            On Error Resume Next
       End If
    End If
  Next

  ' The dependant controls are covered by specific tagvalues:
  ' first the Me.txtPermitNo) >> 1ne
  ' second the Me.cboSConnType) >> 2ne
    If Len(myCtlList) > 0 Then
      MsgBox "Please fill the following field(s) before continuing:" & vbCrLf & myCtlList
        MandatoryFields = False
    End If
    Exit Function
End Function
ASKER CERTIFIED SOLUTION
Avatar of Angelp1ay
Angelp1ay
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of billcute
billcute

ASKER

Angelp1ay:
Do you mean Form's Before Update?
Errrm I was thinking catch the button click. I haven't done much myself with bound forms, but as long as the code is run before the record is added then that's what counts!! :o)
Here is my current code behind the btnAddNew button that triggers the [ne], [1ne], [2ne] etc... that does that..

Private Sub btnAddNew_Click()
On Error GoTo Err_btnAddNew_Click
  '  ***************************
  'the following Messagebox is just for testing tag property settings in controls
'           MsgBox ShowTags

    If Not MandatoryFields Then    ' <---Check for Mandatory Fields
      Exit Sub
    End If
       
'    Call SaveCurrentControlValues            ' For Autofill Routine - this works fine
' ****************************************************************
    If Me.AllowAdditions = False Then Me.AllowAdditions = True
    Me.Filter = ""
    Me.FilterOn = False
   
    DoCmd.GoToRecord , "frmMain", acNewRec
  '*******************
  Me.cboBlockNo.SetFocus
Exit_btnAddNew_Click:
     Exit Sub
Err_btnAddNew_Click:
     MsgBox err.Description
   Resume Exit_btnAddNew_Click
End Sub
I tested your suggestion code behind btnAddNew button and there was a:
 compile error:
"Variable not defined
...on....
Cancel = True
Angelp1ay:
I play around with your code  and got it to work.

Regards
Bill
Awesome! So it does what you want?
Any chance you could post how you fixed it?
Angelplay,
If you have time, please check out the link below. Any assistance will be appreciated.
https://www.experts-exchange.com/questions/22986676/Show-form-fields-with-data.html

Regards
Bill
Angelplay:
I will appreciate it if you could use the technique here to change (control background and fore color) at my similar question at the link below:

   Dim ctl AS Control        
        For Each ctl In Me.Controls
            If Nz(Instr(ctl.Tag, "NE"),0) <> 0 Then
                If Len(Nz(ctl.Value,"") < 1 Then
                    Cancel = True                    
                    ' Something to make the control obvious (syntax a bit iffy)
                    ctl.BackgroundColor = Red
                End If
        Next ctl
    End Sub

Here is the link:
https://www.experts-exchange.com/questions/23844081/Changing-Control-Colors.html