Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Validate a control using its tag

Status
Not open for further replies.

oxicottin

Programmer
Jun 20, 2008
353
US
Hello, I have no clue where to begin on this... I have a form that has a few text boxes and combo boxes and one option group that needs to have data in it before the form can be either changed or closed. I have been reading and it seems you can use a controls tag to validate. I wanted to have an Ok/Cancel message box come up and ask if I wanted to close the form and undo the data I have entered or cancel and finish filling out the form. How can this be done?

Thanks,
SoggyCashew.....
 
How are ya oxicottin . . .

To be specific, in your secnario you validate data to [blue]make sure a proper record is saved![/blue] ... [purple]Not to see if you can change data or close the form[/purple] [surprise]

Thru the user interface, [blue]a record is saved whenever the focus moves to a different record.[/blue] For standard data entry this occurs when the user enters data in the last field and tabs/enters into the next record.

Now ... [blue]a record is saved only if that record is in edit mode[/blue], which occurs when your entering new data or editing previously saved data.

The technique you see involves the follwoing:
[ol][li]In the [blue]Tag[/blue] property of the controls of interest, enter a question mark [blue]?[/blue] [red](no quotations please!)[/red]. The question mark cause the code that follows to only examine those controls tagged.[/li]
[li]In the forms [blue]Before Update[/blue] event, copy/paste the following:
Code:
[blue]   Dim ctl As Control
   Dim Msg As String, Style As Integer, Title As String, DL As String
   
   DL = vbNewLine & vbNewLine [green]'double CRLF[/green]
   
   For Each ctl In Me.Controls
      If ctl.Tag = "?" Then
         If Trim(ctl & "") = "" Then
            Msg = "'" & ctl.Name & "' is a required field!" & DL & _
                  "You'll have to go back and fix this before the record can be saved, or, " & _
                  "hit escape to cancel the record."
            Style = vbInformation + vbOKOnly
            Title = "Required Data Missing! . . ."
            MsgBox Msg, Style, Title
            
            Me(ctl.Name).SetFocus [green]'set focus to required field.[/green]
            Cancel = True         [green]'cancel saving the record.[/green]
            Exit For              [green]'break the loop to take care of the problem.[/green]
         End If
      End If
   Next[/blue]
[/li][/ol]
A good merit for the code is that it allows the user to enter a record smoothly (skipping fields or not), until an attempt is made to save the record. One by one the user is sent back to complete required fields until all are done. Properly edited records are saved normally.

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
AceMan, thanks for the great example but I have seen almost this exact example and have tried it on my forms beforeUpdate prior to posting and it only worked for a text box and and I have combo boxes and an option group I needed it to work on and the form had still closed. When I click my close button on my form it prompts the message and still closes the form.

Thanks,
Chad

Thanks,
SoggyCashew.....
 
oxicottin . . .

As the code is presented it will work for any control that can return a [blue]Null[/blue] or a [blue]Null String ""[/blue], [purple]which includes all the controls you've mentioned![/purple] Also you didn't mention a close button in your post origination.

Post the code in your button . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
oxicottin,
If the Combo Boxes and Option Group have a default value, like 0, then it won't be seen by the code as Trim(ctl)="" or IsNull. Somthing like:
If Trim(ctl & "") = "" Or ctl=0(Default Value) Then

That may help
 
Here is the code in my close button... Thanks!

Code:
Private Sub cmdClose_Click()

'//Closes form
DoCmd.Close acForm, "AccidentEntry"

'/Shows frmSwitchboard again
Forms![frmSwitchboard].Visible = True

'Clears frmSwitchboard search controls

'Clears option group's
Forms!frmSwitchboard!optClassicficationGroup = Null
Forms!frmSwitchboard!optClassicficationSearchGroup = Null

'Clears Controls
Forms!frmSwitchboard!cboFindByEmployeeName = ""
Forms!frmSwitchboard!txtFindDate = ""

End Sub[CODE/]

Thanks,
SoggyCashew.....
 
nobull613 ... oxicottin ...
nobull613 said:
[blue]If the Combo Boxes and Option Group have a default value, like 0, then it won't be seen by the code as Trim(ctl)="" or IsNull.[/blue]
Is not the purpose of a [blue]default value[/blue] to provide a valid entry where none maybe input by the user? [surprise]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
oxicottin . . .

The following should do:
[ol][li]Be sure the question marks are in the [blue]Tag[/blue] property of the mentioned controls.[/li]
[li]In the [blue]code module[/blue] of the form, copy/paste the following function:
Code:
[blue]Public Function ValidateOK() As Boolean
   Dim ctl As Control
   Dim Msg As String, Style As Integer, Title As String, DL As String
   
   ValidateOK = True
   DL = vbNewLine & vbNewLine 'double CRLF
   
   For Each ctl In Me.Controls
      If ctl.Tag = "?" Then
         If Trim(ctl & "") = "" Then
            Msg = "'" & ctl.Name & "' is a required field!" & DL & _
                  "You'll have to go back and fix this before the record can be saved, or, " & _
                  "hit escape to cancel the record."
            Style = vbInformation + vbOKOnly
            Title = "Required Data Missing! . . ."
            MsgBox Msg, Style, Title
            
            ValidateOK = False
            Me(ctl.Name).SetFocus [green]'set focus to required field.[/green]
            Exit For              [green]'break the loop to take care of the problem.[/green]
         End If
      End If
   Next

End Function[/blue]
[/li]
[li]Replace the code in the forms [blue]Before Update[/blue] event with the following:
Code:
[blue]   If Not ValidateOK Then Cancel = True[/blue]
[/li]
[li]Finally, replace your button code with the following:
Code:
[blue]   Dim frm As Form, flgClose As Boolean
   
   Set frm = Forms!frmSwitchboard
   flgClose = True
   
   If Me.Dirty Then
      If ValidateOK Then
         Me.Dirty = False 'Save the record
      Else
         flgClose = False
      End If
   End If
   
   If flgClose Then 'ok to close the form
      DoCmd.Close acForm, "AccidentEntry"
      frm.Visible = True

      frm!optClassicficationGroup = Null
      frm!optClassicficationSearchGroup = Null

      frm!cboFindByEmployeeName = ""
      frm!txtFindDate = ""
   End If
   
   Set frm = Nothing[/blue]
[/li]
[li][blue]Perform your Testing! . . .[/blue][/li][/ol]

[blue]Your Thoughts? . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997 [blue]Worthy Reading![/blue] [thumbsup2]
Also faq181-2886 [blue]Worthy Reading![/blue] [thumbsup2]
 
Wow thanks AceMan! Thanks a million....

Thanks,
SoggyCashew.....
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top