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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need Help Passing Form Controls 1

Status
Not open for further replies.

maetrix

Technical User
Jul 27, 2002
14
CA
Hello,

I'm tring to understand how Modules work, (as I under stand it so far, it's where you store procedures and Functions to be called by various forms)

What I have having troubles understanding is how to pass control to and from.. (let's say a function).

I want a button the check the Password field of a table. I have the code to check this all written up, but I want to be able to use this on multiple buttons.

So it would go something like this:

On click
.. v--- ??
strPassCheck = CheckPass(cmdButton)

if strPassCheck = True Then..
Load This form
Else ...
Return to Menu.
End if.

How would I start the CheckPass sub in the module?

Thanks

--Toby Kliem-- Novice Programmer;
Expert Hack


sapere aude: Dare to be wise
 
You invoke any function or procedure by referencing it in code. You get access to code by selecting the events you want to control on the Events tab on form or control Properties and selecting EventProcedure.

As an example, lets assume you have a control called txtPassWord on your form. When you click the Before Update event on the Properties Event tab, it will ask you what you want to do. If you select Event Procedure it will create a little block of code namde txtPassWord_BeforeUpdate for you in the Form's module (not a separate module from the Modules
tab) and pass control to you. That is where you invoke your validation function.

If you have created your validation function in a Module and declared it Public, then the form will find it for you.

Lets say you have created a password validation funcion as follows in a separate module.

Public Function ValidatePassWord(strPassWord As String) As _
Boolean

If IsNull(strPassWord) or Len(strPassWord) = 0 Then
ValidatePassWord = False
Exit Function
End If

Other validation code

If the validation was successful Then
ValidatePassWord = True
Else
ValidatePassWord = False
End If

End Function

You return a value for a function by setting its name to whatever value you want to return. In this case, since we defined it as a Boolean it has to return either true or false.

Now, in the txtPassWord_BeforeUpdate code you would do something like the following:

Private Sub txtPassWord_BeforeUpdate(Cancel As Integer)

If Not ValidatePassWord(txtPassWord) Then
Cancel = True
txtPassWord.Undo
Exit Sub
End If

Possibly other code

End Sub

The reason I suggested the Before Update event is because it passes a Cancel parameter. If you set that to true, the control will automatically be returned to txtPassWord regardless of what the user did. By invoking the Undo method it will clear whatever value they entered.

Good Luck!




 
Thank you very much SBendBuckeye.. your answer was very clear and explained a lot to me.

--Toby-- Novice Programmer;
Expert Hack


sapere aude: Dare to be wise
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top