PDA

View Full Version : Solved: vba macro code help :(



JLSHARP92
03-09-2011, 10:04 AM
Ok, Iv'e got my macro code for a login form and will post it shortly. The whole idea for the login for is to be able to type in a username and password and will dispaly a confirmation message which lets the user know they have successfully logged in. The user will then be automatically directed to the main menu part of my system. I have successfully got the login for to display an error message when the user doesnt type in a username or password but I cannot get the system to log the user in if they enter a username and password. Could someone please look at my code below and give me an idea were ive went wrong? :dunno

Thank you :)

--------------------------------------------------------------------------

Sub LogIn()

'stops the screen from flickering
Application.ScreenUpdating = False

'declares variables
Dim uname As String
Dim pword As String
Dim RowNum As Integer
Dim usercount As Integer
Dim rowcount As Integer

'moves worksheet and sets values to variables
Sheets("LogInForm").Select
uname = Range("Username")
pword = Range("Password")
RowNum = Range("NumUsers")

'if values are not empty
If uname <> "" And pword <> "" Then

'set variables as values to move through rows
usercount = 1
rowcount = 1

'move sheets
Sheets("UserDetails").Select

'
Do While usercount <= RowNum

'if correct uname and pword are correct values
If uname = Range("A" & rowcount) And pword = Range("B" & rowcount) Then

'move worksheets and reset values , then moves sheets
Sheets("LogInForm").Select
Range("Username") = ""
Range("Password") = ""
Sheets("MainMenu").Select
Exit Sub

'if values dont match
Else

Error = True
usercount = usercount + 1
rowcount = rowcount + 1

'ends if statement
End If

'starts loop
Loop

'if the boolean is true
If Error = True Then

'moves sheets, clears form, displays message box
Sheets("LoginForm").Select

Range("Username") = ""
Range("Password") = ""
MsgBox "Please try again, You must enter a valid username and password", vbOKOnly

'ends if statement
End If

'if the boolean is false then display error message
Else

'displays message box
MsgBox "You must enter both a username and password"

'ends if statement
End If

End Sub

Bob Phillips
03-09-2011, 10:21 AM
Sub LogIn()

'stops the screen from flickering
Application.ScreenUpdating = False

'declares variables
Dim uname As String
Dim pword As String
Dim rng As Range

'moves worksheet and sets values to variables
Sheets("LogInForm").Select
uname = Range("Username")
pword = Range("Password")

'if values are not empty
If uname <> "" And pword <> "" Then

With Sheets("UserDetails")

On Error Resume Next
Set rng = .Columns(1).Find(uname)
On Error GoTo 0
If Not rng Is Nothing Then

Sheets("LogInForm").Range("Username") = ""
Sheets("LogInForm").Range("Password") = ""

If pword = rng.Offset(0, 1).Value Then

'move worksheets and reset values , then moves sheets
Sheets("MainMenu").Select

Application.ScreenUpdating = True
Exit Sub
End If
End If

MsgBox "Please try again, You must enter a valid username and password", vbOKOnly
End With
Else

MsgBox "You must enter both a username and password"
End If

Application.ScreenUpdating = True

End Sub

JLSHARP92
03-09-2011, 10:32 AM
Thank you very much for this. It works perfectly! :bow:

JLSHARP92
03-09-2011, 10:46 AM
Just one more thing, do you think that you could put a message at the top of every piece of code that you have re-wrote so I can understand the code a little more? please