VBA issue with a Login Form

Breanna

Registered User.
Local time
Today, 08:31
Joined
Dec 29, 2014
Messages
49
I am using Access 2013 and starting to create a login form (based off a video I found online) so that it wont open without the correct login.

So far I have created a form with Username and Password text boxes. Then OK and Cancel Buttons. I also have a table that has UserName and Password set up. It is my understanding that to start I have to write code for the OK button. I have it written out but when I open the form type in the username and password then click ok it gives me a syntax error and opens the VBA for the button. I think from reading online that error means something is misspelled or not typed correctly but it has the top line highlighted with a yellow arrow leading me to believe the issue lies within that line. Am I correct in this? if so does anyone have any ideas on how to fix that part?
The top line just says:

Private Sub Command1_Click()

I am 100% new to using access let alone VBA. Any help would be appreciated or even a link to a good website that is easy to understand.
If needed I can post the entire code I have but I didn's want to make this post to much at first. I just need to know where a good place to start is.
 
Please post the related vba code.
 
Please post the related vba code.
This is my very first time doing this so I hope its not too messed up. :)

Private Sub Command1_Click()


If IsNull(Me.txtUserName) Then
msgBox "Please Enter User Name", vbinformation "UserName Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
msgBox "Please Enter Password", vbinformation "Password Required"
Me.txtPassword.SetFocus
Else
'process the job
If (IsNull(DLookup("[UserName]", "UsernamePassword", "[Username]='" & Me.txtUserName.Value & " and password='" & Me.txtPassword.Value & ""))) Then
MsgBox "Incorrect UserName or Password"

Else
MsgBox "UserName and Password Correct"
DoCmd.OpenForm "Main Form"

End If

End If
End Sub
 
Your Code is missing an End If statement ;
I've reformatted it and indented the If Then Else statements to make it more obvious;
Code:
Private Sub Command1_Click()

If IsNull(Me.txtUserName) Then
	msgBox "Please Enter User Name", vbinformation "UserName Required"
	Me.txtUserName.SetFocus
Else
	If IsNull(Me.txtPassword) Then
		msgBox "Please Enter Password", vbinformation "Password Required"
		Me.txtPassword.SetFocus
	Else
	'process the job
		If (IsNull(DLookup("[UserName]", "UsernamePassword", "[Username]='" & Me.txtUserName.Value & " and password='" & Me.txtPassword.Value & ""))) Then
			MsgBox "Incorrect UserName or Password"
		Else
			MsgBox "UserName and Password Correct"
			DoCmd.OpenForm "Main Form"

		End If

	End If
[COLOR="Red"]End If[/COLOR]
End Sub
If in the Code editor you click the Debug Option, then select Compile it will normally highlight any specific errors.
I would advise having a good read of the FAQ's but if this is your first go at coding it looks pretty good!

Moving forwards I would change the default command button names to something easier to find and debug later. Command1 isn't going to mean a thing to you in a couple of months. ;)
 
Your Code is missing an End If statement ;
Not quiet right, because Breanna use in the code "ElseIf", (show in the Help-file the syntax for it)!
But was wrong is there are some ' missing:

Code:
If (..., "[Username]='" & Me.txtUserName.Value & "[B][COLOR=Red]'[/COLOR][/B] and password='" & Me.txtPassword.Value & "[B][COLOR=Red]'[/COLOR][/B]") ...
 
Doh - I thought that was a formatting error...
And as another member pointed out there are missing , after the VBInformation tags
Code:
msgBox "Please Enter User Name", vbinformation [COLOR="Red"],[/COLOR] "UserName Required"
Me.txtUserName.SetFocus
ElseIf IsNull(Me.txtPassword) Then
msgBox "Please Enter Password", vbinformation [COLOR="Red"],[/COLOR] "Password Required"
 
I did everything you all suggested and it totally works!
Also great tip about the debug button I didn't even know it was there and that will really help me in the future as well and not just with this one part.

I know this is probably a silly question but when you posted a reply with the code how did you get it in its own box in the reply?

I did go back and indent mine like you had it. so much easier to read. and I changed the name of the button as well. Thanks for the tips!
 
..
I know this is probably a silly question but when you posted a reply with the code how did you get it in its own box in the reply?
..
Click the "Quote" button, (it is in the same position as the "Thanks" button)!:)
 
Click the "Quote" button, (it is in the same position as the "Thanks" button)!:)

Haha I knew it would be simple like that. Ive used that before but didnt realize you can add to it. Thanks! :D
 

Users who are viewing this thread

Back
Top Bottom