VB validate security code

Charlie

Board Regular
Joined
Mar 1, 2002
Messages
134
I am trying to code an app that will validate a user entered security code.
So far I havent got very far.
I am trying to use sub-procedures which are new to me.

Option Explicit

Private Sub BuildCode(pCodeEntered As String, pNumber As String)

End Sub

Private Sub cmdAddOne_Click()

'Output digit in picbox
picOutput.Print (1)
End Sub

Private Sub cmdAddTwo_Click()

'Output digit in picbox
picOutput.Print (2)
End Sub

Private Sub cmdAddThree_Click()

'Output digit in picbox
picOutput.Print (3)
End Sub

Private Sub cmdCheckCode_Click()
Dim pNumber As String
pNumber = 331221
If pNumber = pNumber Then
picOutput.Print "Access Granted"
ElseIf pNumber < pNumber > pNumber Then
picOutput.Print "Access Denied"
End If
End Sub

Private Sub cmdExit_Click()
'Terminate Application
End
End Sub

Private Sub Form_Load()
'Declare required storage
Dim CodeEntered As String




Show
CodeEntered = ""
Digits = 0
picOutput.Print "Enter a six digit number"
End Sub

Charlie
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.

phantom1975

MrExcel MVP
Joined
Jun 3, 2002
Messages
3,962
I see a couple of holes in your code that may be causing the problem. Here is a sample code that might help you fix your own code:

Private Sub Workbook_Open()
Dim SecCode as String
Dim UserCode as String
Beginning:
SecCode = "112233"
UserCode = InputBox("Please enter your security code.","Enter Security Code","Enter code here")
If UserCode<> SecCode Then
MsgBox "Access Denied",vbOkOnly+vbCritical,"Access Denied"
Goto Beginning
End If
MsgBox "Access Granted",vbOkOnly,"Access Granted"
End Sub

Does this help? (FIXED)


_________________
The only dumb question is the one that isn't asked. Pass on what you have learned.
This message was edited by phantom1975 on 2002-11-02 18:27
 

Coder-X

New Member
Joined
Oct 31, 2002
Messages
1
Phantom-need to add >> (<< to the line:

End If
MsgBox "Access Granted",vbOkOnly, "Access Granted")
End Sub

Later!

X
This message was edited by Coder-X on 2002-11-02 18:28
 

Charlie

Board Regular
Joined
Mar 1, 2002
Messages
134

ADVERTISEMENT

Tried this but I get "Invalid Code" 100% of the time.
I know its not exactly like your example but I thought it might work.

Private Sub cmdCheckCode_Click()
'Declare required storage
Dim pCodeEntered As String
Dim pNumber As String
pNumber = "321232"
If pCodeEntered<> pNumber Then
picOutput.Print "Invalid Code"
ElseIf pCodeEntered = pNumber Then
picOutput.Print "Access Granted"
End If
End Sub

Charlie

What is vbOkOnly+vbCritical ive never seen this expression before,ive been told using GoTo is bad programming practice.
Do you think so?
I dont know I am only a beginner at this.
This message was edited by Charlie on 2002-11-02 18:45
 

phantom1975

MrExcel MVP
Joined
Jun 3, 2002
Messages
3,962
The point of the GoTo was to restart the code if access is denied. vbCritical, vbOkOnly, etc are just different ways to have a MsgBox come up. vbCritical makes a red circle with a white X show up in the MsgBox.

One of the problems with your code might be in your declaration of your pCodeEntered string. I am guessing that you are using a UserForm and that the code is entered in a TextBox. If so, you need to make sure that you PUBLICALLY declare your variable. To do this, go to the very top of the UserForm Module (the place where you put all of the code for your UserForm) and type the following:

Public pCodeEntered as String

You will not need to Dim pCodeEntered because it is already declared at the beginning of the module. By publicly declaring your variable, it makes it available to all of the procedures within THAT module.

Does this help?
 

Charlie

Board Regular
Joined
Mar 1, 2002
Messages
134

ADVERTISEMENT

This is my code as you can see the user enters 6 digits via 3 buttons with the values of 1,2,3.
Option Explicit

Private Sub BuildCode(pCodeEntered As String, pNumber As String)

End Sub

Private Sub cmdAddOne_Click()

'Output digit in picbox
picOutput.Print (1)
End Sub

Private Sub cmdAddTwo_Click()

'Output digit in picbox
picOutput.Print (2)
End Sub

Private Sub cmdAddThree_Click()

'Output digit in picbox
picOutput.Print (3)
End Sub

Private Sub cmdCheckCode_Click()
'Declare required storage
Dim pCodeEntered As String
Dim pNumber As String
pNumber = "321232"
If pCodeEntered <> pNumber Then
picOutput.Print "Invalid Code"
ElseIf pCodeEntered = pNumber Then
picOutput.Print "Access Granted"
End If
End Sub


Private Sub cmdExit_Click()
'Terminate Application
End
End Sub

Private Sub Form_Load()
'Declare required storage
Dim CodeEntered As String
Dim Digits As Single



Show
CodeEntered = " "
Digits = 0
picOutput.Print "Enter a six digit number"

End Sub

Charlie
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
On 2002-11-02 18:37, Charlie wrote:

What is vbOkOnly+vbCritical ive never seen this expression before,ive been told using GoTo is bad programming practice.
Do you think so?
I dont know I am only a beginner at this.
This message was edited by Charlie on 2002-11-02 18:45

Hi Charlie
If there is something you are unsure of eg syntax etc....just highlight it and Press F1 for help.
Your point about using Goto = bad programing
is like everything else, use in moderation.
Goto is handy when you have it structured and not too many....as Phantom has done, BUT using it willy nilly can create what coders refer to as Spagetti code and can be hard to decipher.
 

Charlie

Board Regular
Joined
Mar 1, 2002
Messages
134
F1 works if you have the help files installed which I dont.
I always thought a loop would be preferable to a GoTo.

Charlie
 

Ivan F Moala

MrExcel MVP
Joined
Feb 10, 2002
Messages
4,209
On 2002-11-02 19:16, Charlie wrote:
F1 works if you have the help files installed which I dont.
I always thought a loop would be preferable to a GoTo.

Charlie

Does the Goto work ? If so then wheres the problem, there's only One goto there, what ever works, so long as you don't get caught up in too many Gotos or Loops for that matter
Can you not install the help files?
 

Forum statistics

Threads
1,144,330
Messages
5,723,749
Members
422,513
Latest member
Meathead2022

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top