Full Version: CheckValidate Function not working properly...need another set o
UtterAccess Forums > Microsoft® Access > Access Forms
petite39
I have this function that I got a long while ago off of utteraccess and works fine with the form that's in use. I want to use it on another form and I can't get it to work. I get an error message saying "CheckValidate variable not defined".
'--------------------------------------------------------------
Function CheckValidate() As Integer
Dim currctl As Integer
Dim numctls As Integer
Dim ctl As Control
numctls = Me.Count
CheckValidate = 0
For currctl = 0 To numctls - 1
Set ctl = Me(currctl) 'do we want to check this control?
If ctl.Tag = "validate" And ctl.Visible = True Then
If IsNull(ctl) Or ctl = "" Or ctl = Empty Then
MsgBox "Please fill in the field that is currently red and then click the button.", _
48, " Fill In Required Field's"
ctl.SetFocus
ctl.BackColor = 255
ctl.ForeColor = vbWhite
CheckValidate = 1
xit Function
Else
ctl.BackColor = vbWhite
ctl.ForeColor = vbBlack
End If
End If
Next currctl
End Function
Function ResetColor()
Me.ActiveControl.BackColor = vbWhite
Me.ActiveControl.ForeColor = vbBlack
End Function
'---------------------------------------------------------
Here's what I did on my form called frmCoverPage.
I have a field on the form called DocID. I set Tag to equal "validate".
On the Close button on click event on the form I have this code:
Private Sub Command425_Click()
On Error GoTo Err_Command425_Click
If CheckValidate = 0 Then <<<<<<< CheckValidate variable not defined
DoCmd.Close
Else
Exit Sub
End If
Exit_Command425_Click:
Exit Sub
Err_Command425_Click:
MsgBox Err.Description
Resume Exit_Command425_Click

End Sub
In the afterUpdate event of field DocID I call function Reset Color
I'm obviously missing something but I copied what I had which works....
fkegley
Where is function CheckValidate stored? If in the form, then it cannot be "seen" by the other form.
would suggest moving it to a standard Module.
petite39
I never thought of that....dahh!
copied it and created a new module and called it "CheckValidate".
2 things...1) I can't see it as a separate module...When I search for it it shows up but it must be imbedded in another even though I click on the New wizard.
2) I get an error on the equal sign on the stmt....If checkvalidate = 0. Says type mismatch.
Any ideas?
fkegley
NO, do not give the module the same name as the function. Access will then register the function name as a module name.
hange the name of the module to Module 1 or anything else but the name of the function. Don't worry, Access will find it, as long as it is a PUBLIC function.
petite39
Thank you. I did what you suggested, reran the code.
Here's my new error: Invalid use of Me keyword
numctls = Me.Count It highlights the Me in blue
do I need to change it to Me! ?????????
fkegley
No, it's even worse than that. Me will need to be replaced by the complete form reference. This means that you will need to modify the function so that it accept the form name, so you can get at the control itself.
When you call the function you will need to pass in the name of the form:
CheckValidate(Me.Name)
and modify the function's signature line:
Function CheckValidate(FN As String) As Integer
For Each ctl In Forms(FN).Controls
Next ctl
petite39
Thanks for the directions. As you can tell, I'm not good with functions. I'm an amature at best.
understand changing the funtions signature to what you indicated.
I just don't know how to pass the form name to the function.
Can you modify my example that I originally supplied and note where I need to do this?
Thanks for all your help.
fkegley
Function CheckValidate(FN As String) As Integer
Dim currctl As Integer
Dim numctls As Integer
Dim ctl As Control
numctls = Forms(FN).Count
CheckValidate = 0
For currctl = 0 To numctls - 1
Set ctl = Forms(FN).Controls(currctl) 'do we want to check this control?
If ctl.Tag = "validate" And ctl.Visible = True Then
If IsNull(ctl) Or ctl = "" Or ctl = Empty Then
MsgBox "Please fill in the field that is currently red and then click the button.", _
48, " Fill In Required Field's"
ctl.SetFocus
ctl.BackColor = 255
ctl.ForeColor = vbWhite
CheckValidate = 1
xit Function
Else
ctl.BackColor = vbWhite
ctl.ForeColor = vbBlack
End If
End If
Next currctl
End Function
Private Sub Command425_Click()
On Error GoTo Err_Command425_Click
If CheckValidate (Me.Name) = 0 Then <<<<<<< CheckValidate variable not defined
DoCmd.Close
Else
Exit Sub
End If
Exit_Command425_Click:
Exit Sub
Err_Command425_Click:
MsgBox Err.Description
Resume Exit_Command425_Click
End Sub
petite39
Thank you very much. I will try it out tomorrow.
fkegley
You're welcome. I am glad I could help. Please let me know how it goes.
petite39
The code you gave me works great. I'm making very good progress. Had to change the Reset Color function a bit. It's working great now. I'm no expert at functions, but I did learn a thing or 2 yesterday. Thanks for all your help.
fkegley
I am glad to hear it. My code doesn't always "work great" so I am pleased that I got it right this time. Thanks for getting back to me.
You're welcome. I am glad I was able to help.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.