Validatae and clear function in-harmonious

timothyl

Registered User.
Local time
Yesterday, 21:11
Joined
Jun 4, 2009
Messages
92
Good Morning, I have written the two functions below but they cant be used together, one is a validation function the other is a clearing function, if I place a c and v in the tag proprty box they are both ignored I tried changing one to I as Integer and placing a 1 in the tag box but did not work.
Can some one see how to either alter one of them or call them differently so they can work together, Thanks


Function ClearAll(c As String, f As Form)
Dim ctl As Control
For Each ctl In f.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" And ctl.Tag <> "c" Then
ctl.Value = Null
End If
Case Else
End Select
Next ctl

End Function

And

Function Validate("v" As String, frm As Form)

Dim nl As String, ctl As Control
nl = vbNewLine & vbNewLine
For Each ctl In frm.Controls
Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If Trim(ctl & "") = "" And ctl.Tag = "v" Then
Msg = "Data Required for '" & ctl.Controls(0).Caption & "'" & nl & _
"Required Data!" & nl & "Please enter the data and try again ... "
Style = vbInformation + vbOKOnly
Title = "Required Data..."
MsgBox Msg, Style, Title
ctl.SetFocus
Cancel = True
End If
Case Else
End Select
Next ctl
End Function
 
If you place a "c" and a "v" in the tag of course this code is going to be ignored... Your tag is "cv" not "c" or "v"........
My first question for the code is...... why?
Your clearing textboxes, combos, whatever... that then will obviously return the error message when the second code runs. Use a seperate command button to "Clear All"
 
It is possible to put multiple value in the Tag property.

You will need to change your code. Your current code assume that that there can only be a single value in the Tag.

To handle multiple values try this:

Instr(ctl.Tag,"v") > 0

so your code would be something like:

Code:
If ctl.ControlSource = "" And Instr(ctl.Tag,"c") > 0 Then


Your code looks like it has some issues.
 
I cleaned up your code an removed unused stuff.

The .TAG can be "cv" with the following code:

Code:
Function ClearAll(f As Form)
Dim ctl As Control

For Each ctl In f.Controls

 Select Case ctl.ControlType
     Case acTextBox, acComboBox, acListBox, acCheckBox
          If ctl.ControlSource = "" And Instr(ctl.Tag,"c") > 0 Then
                  ctl.Value = Null
          End If

 End Select

Next ctl

End Function


And

Code:
Function Validate( frm As Form)

Dim nl As String, ctl As Control

Dim Style as long
Dim Title as string

nl = vbNewLine & vbNewLine

For Each ctl In frm.Controls

     Select Case ctl.ControlType
          Case acTextBox, acComboBox, acListBox, acCheckBox
               If Trim(ctl & "") = "" And Instr(ctl.Tag,"v") > 0 Then
                   Msg = "Data Required for '" & ctl.Controls(0).Caption & "'" & nl & _
                   "Required Data!" & nl & "Please enter the data and try again ... "
                   Style = vbInformation + vbOKOnly
                   Title = "Required Data..."
                   MsgBox Msg, Style, Title
                  ctl.SetFocus

                        '>>>  Cancel = True   ' Is this a global variable?

             End If

 End Select

Next ctl

End Function


I would highly urge you to add this to the top of your code module:

Code:
Option Explicit


This will help you find your coding issues.
 
For Each ctl In frm.Controls

Your running different code on the exact same thing...... Why do you need different tags? Call it "c" or "v" or whatever...... or use a "not isnull"
 
HiTechCoach, Thank you for responding and cleaning up my code, they are my first two function so there similar as I based the validation off the clear function. Is it possible to change the clear code so that you place a c where you don't want to clear the ctl as it use to be, I played some but could not get it yet. Thank you again. Tim
 
CEH, Could you exsplane further, not entirely following your explanation, very new to writing functions, Thank you
 
First, its seems rather odd to be putting these two together..... Your clearing all the text, combo, list and checkboxes..... then running code to execute a message box if they are null.... Of course they are going to be null, you just cleared them. So for this to ever get past the message box some tags need "c" others "v"..... if they have both just won't float...... Separate the functions would be my advice.
 
CEH, thanks for responding, sorry I was not clear in my posting, they are separate
 
Oh-boy, I got it on my own

Function ClearAll(c As String, f As Form)

'If you use with Validate function in same control Tag window then must be v then c e.g vc
Dim ctl As Control
For Each ctl In f.Controls

Select Case ctl.ControlType
Case acTextBox, acComboBox, acListBox, acCheckBox
If ctl.ControlSource = "" And InStr(ctl.Tag, "c") = 0 Then
ctl.Value = Null

End If
If ctl.ControlSource = "" And InStr(ctl.Tag, "c") = 1 And ctl.Tag <> "c" Then
ctl.Value = Null

End If
End Select
Next ctl

End Function

Thanks again HiTechCoach
 

Users who are viewing this thread

Back
Top Bottom