Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Junior Member
 
Posts: 4
Default Heres my macro code...

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? 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
  #2   Report Post  
Member
 
Posts: 31
Default

Quote:
Originally Posted by JSHARP92 View Post
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? thank you :)

--------------------------------------------------------------------------
Hi there. Well, you were very nearly there with this, so well done. I have taken the liberty to change your loop for you, though feel free to change it back again if this doesn't meet what you have been tasked with. I thought it might be a hassle to capture the number of users and loop through this way - I have changed it so it doesn't matter how many users you have, it will check the whole list.

Take a look at this code and see if it suits your purpose. There are a few other things I'd fix in there to make it more efficient/tidy, however it works and meets the requirements of your assignment.

Let me know how you get on.

Code:
Sub LogIn()
    
    'stops the screen from flickering
    Application.ScreenUpdating = False
    
    'declares variables
    Dim uname As String
    Dim pword As String
    Dim PasswordFound As Boolean
    
    'moves worksheet and sets values to variables
    Sheets("LogInForm").Select
    uname = Range("Username")
    pword = Range("Password")
    
    'if both values are not empty then check to see if they are a valid combination
    If uname < "" And pword < "" Then
        
        ' Move to the sheet of username/password combos
        Sheets("UserDetails").Select
        Range("A1").Activate
        
        PasswordFound = False
        
        ' Keep reading until the bottom of the list of usernames
        Do Until ActiveCell = ""
            ' do the username and password match?
            If ActiveCell = uname And ActiveCell.Offset(0, 1) = pword Then PasswordFound = True
            ' move down to the next row
            ActiveCell.Offset(1, 0).Activate
        Loop
        
        'if correct uname and pword are correct values
        If PasswordFound Then
        
            'move worksheets and reset values , then moves sheets
            Sheets("LogInForm").Select
            Range("Username") = ""
            Range("Password") = ""
            Sheets("MainMenu").Select
            MsgBox "Your login was successful", vbOKOnly, "Login Success"
        'if values dont match
        Else
        
            MsgBox "Please try again, You must enter a valid username and password", vbOKOnly, "Login Unsuccessful"
            
        'ends if statement
        End If
    
        'if the boolean is true
        If ErrorX = True Then
            
            'moves sheets, clears form, displays message box
            Sheets("LoginForm").Select
            
            Range("Username") = ""
            Range("Password") = ""
    
            
            'ends if statement
        End If
    
    ' Both password and username not entered
    Else
        
        'displays error
        MsgBox "You must enter both a username and password", vbOKOnly, "Username and Password Required"
    
    'ends if statement
    End If

End Sub
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sum-macro or code puiuluipui Excel Discussion (Misc queries) 1 October 17th 09 03:10 PM
VB code to run macro? Tdp Excel Discussion (Misc queries) 8 November 3rd 08 03:52 PM
Macro Code muddan madhu Excel Discussion (Misc queries) 4 April 13th 08 07:23 PM
Deleting code from a macro (by a macro) Brettjg Excel Discussion (Misc queries) 2 May 8th 07 10:14 PM
Macro code Shu of AZ Excel Discussion (Misc queries) 2 January 29th 07 06:29 PM


All times are GMT +1. The time now is 02:29 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"