Help for my VB code

meenctg

Learn24bd
Local time
Tomorrow, 04:47
Joined
May 8, 2012
Messages
133
I have 3 form that are login, welcomeFrm, wrongFrm when i open my db it starts with login form. login form has a text box for put password and a command box. i made a program in VB for valid password open welcomeFrm form and invalid password for wrongFrm form. But when i put password and enter it shows wrongFrm for valid and invalid password . please help me any one if i have any mistake in my VB code and suggest me what will i do...........

Private Sub Command2_Enter()
Dim valita As Variant // valita is my text box name
valita = Val(Text)
If valita = "123" Then // 123 is my fixed password
DoCmd.OpenForm "welcomeFrm"
Else
DoCmd.OpenForm "wrongFrm"
End If
End Sub
 
Hmmm, it's hard to know where to start.

Dim valita As Variant // valita is my text box name

Is not the way to reference a control. It already exists as an object you can refer to in code. You don't need to Dim it and even if you did you wouldn't do it like that.

valita = Val(Text)

What the thinking behind this is I'm not sure. 'Text' hasn't been declared (I guess you don't have Option Explicit so VBA will declare it as a new variable there and then and it will have no value). And you're using the Val function is just bizarre in a password system.

Are your passwords just numbers? Any reason why?

However, we'll ignore that (and just get rid of it).

Lastly, comments are done with an apostrophe (') not //

To do what it appears you want it to do the code should be:

Code:
Private Sub Command2_Enter()
    If Me.valita = "123" Then '123 is my fixed password
        DoCmd.OpenForm "welcomeFrm"
    Else
        DoCmd.OpenForm "wrongFrm"
    End If
End Sub

But even that's a pretty bad way to do, if I may comment on it.

Firstly, you're storing the password in the code, which can be read in plain text by opening the database in a text editor (especially with that comment after it) and can only be changed in the code. It's better to put it in a table and ideally in some way encrypted.

Secondly, most logon systems let you try again if the password is wrong. This one seems to give you one shot at it and you're at the wrongFrm if you get it wrong. I hope there's an easy way to get back to the logon form from there.

Lastly, you're presumably going to want to close the logon form if they get it right:

Code:
Private Sub Command2_Enter()
    If Me.valita = "123" Then '123 is my fixed password
        DoCmd.OpenForm "welcomeFrm"
        [B]DoCmd.Close acForm, Me.Name[/B]
    Else
        DoCmd.OpenForm "wrongFrm"
    End If
End Sub
 
Last edited:
Pretty posts do not help.

They are difficult to read and tend to confuse.

Command2 is not at all a good name for a command button.

Try cmdOpenForm or anything that describes the purpose of the Button.

Remember you might have to come back to this code in a Year or Two, or even Six Months. Will you remember what a Command2 is used for.

It is not usual to use Variant as a Variable Type.

Hope this helps.
 
Thanks!
The first code which you have given. It has worked.
 
Glad to hear it.

But do take on board my comments. Even if this is just a throwaway database and it doesn't matter enough to get it right. In future when creating logon systems like this you should be making it much stronger if you really want to keep unauthorised people out.

And Rain's comments too, naming controls is never a waste of time and variants should be very rarely used. Off the top of my head the only times to use them are when you don't know what the data type will be at runtime or when declaring arrays as function arguments.
 

Users who are viewing this thread

Back
Top Bottom