|
-
Aug 14th, 2003, 02:41 PM
#1
Thread Starter
Junior Member
What's wrong with this code? [Resolved]
Dim intPress As Integer, strPass As String, x As Boolean
Private Sub Command1_Click()
strPass = Text1.Text
If PassCheck() = True Then
intPress = MsgBox("Correct Password!", vbExclamation, "The result is:")
Else
intPress = MsgBox("Incorrect Password", vbCritical, "The result is:")
End If
End Sub
Private Function PassCheck(strPass As String) As Boolean
PassCheck = False
If strPass = "loser" Then
PassCheck = True
End Function
Last edited by joe_h_31028; Aug 14th, 2003 at 03:37 PM.
-
Aug 14th, 2003, 02:48 PM
#2
Why dont you tell us what happens?
-
Aug 14th, 2003, 02:52 PM
#3
Hyperactive Member
You're not passing anything (namely a string) to SUB PassCheck().
-
Aug 14th, 2003, 02:54 PM
#4
Thread Starter
Junior Member
I get a compile error when the code gets to the PassCheck() function, it says "Argument not optional". I am practicing with functions, still learning of course, and I can't figure out why its not working.
-
Aug 14th, 2003, 02:57 PM
#5
CyberJar got it. But you will have another error too unless you close that If block with an End If in PassCheck
-
Aug 14th, 2003, 02:58 PM
#6
Thread Starter
Junior Member
strPass is being passed to PassCheck as an argument, the the value of PassCheck is returned in the form of a boolean to be used in the click procedure.
At least thats what I thought I coded, what do you think?
-
Aug 14th, 2003, 02:59 PM
#7
no, its not what you coded
Code:
If PassCheck() = True Then
you didnt put in the arguement
-
Aug 14th, 2003, 03:01 PM
#8
Thread Starter
Junior Member
So I have to have it like this :
PassCheck(strPass as string)
in both the calling and called procedures?
-
Aug 14th, 2003, 03:02 PM
#9
Frenzied Member
You are getting that error because you are not filling the required vailables. note that you have declared this..
VB Code:
Private Function PassCheck(strPass As String) As Boolean
PassCheck = False
If strPass = "loser" Then
PassCheck = True
End Function
But only pass this..
VB Code:
if pass check() = true then
You need to either change you function to this
VB Code:
Private Function PassCheck(optional strPass As String) As Boolean
to allow for not using a variable when you call it or you need to supply a value to strPass then you call the function..
VB Code:
if PassCheck(X) = true then
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Aug 14th, 2003, 03:02 PM
#10
-
Aug 14th, 2003, 03:20 PM
#11
Thread Starter
Junior Member
Thanks guys, I believe I have it now.
I fixed the code as per your instructions, but for learning sake, is this the best way to code this? I just want to be sure I am coding the right way.
---------------------------------------------------------------------------------
Dim intPress As Integer, strPass As String, x As Boolean
Private Sub Command1_Click()
strPass = Text1.Text
If PassCheck(strPass) = True Then
intPress = MsgBox("Correct Password!", vbExclamation, "The result is:")
Else
intPress = MsgBox("Incorrect Password", vbCritical, "The result is:")
End If
End Sub
Private Function PassCheck(strPass As String) As Boolean
PassCheck = False
If strPass = "loser" Then
PassCheck = True
End If
End Function
--------------------------------------------------------------------------------
-
Aug 14th, 2003, 03:25 PM
#12
Frenzied Member
It looks ok from here. I would make one suggestion though..
If you are not going to do anything with the response from the message box I would change this:
VB Code:
Private Sub Command1_Click()
strPass = Text1.Text
If PassCheck(strPass) = True Then
intPress = MsgBox("Correct Password!", vbExclamation, "The result is:")
Else
intPress = MsgBox("Incorrect Password", vbCritical, "The result is:")
End If
End Sub
to this:
VB Code:
Private Sub Command1_Click()
strPass = Text1.Text
If PassCheck(strPass) = True Then
call MsgBox("Correct Password!", vbExclamation, "The result is:")
Else
call MsgBox("Incorrect Password", vbCritical, "The result is:")
End If
End Sub
No need to handle a variable if not necesary.. Understand?
Rudy
10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".
-
Aug 14th, 2003, 03:28 PM
#13
Hyperactive Member
u were forgetting to pass the pw argument, and u forgot an 'end if' in ur passcheck func.
-
Aug 14th, 2003, 03:33 PM
#14
Thread Starter
Junior Member
Got it guys, special thanks to RudyL and Cander for your help and advice. I guess now I can finally move to hour 14 lol
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|