PDA

View Full Version : [SOLVED:] Message above vba code



JLSHARP92
03-09-2011, 01:51 PM
So I have my macro code and a great big thanks to xld for making it possible for me, but I have another problem. I need to leave a message above each piece of code starting with ' in order to make the system more effective and to understand more what the code means. If somebody could help me with putting the messages above the code that would be a great help. Thank you


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
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

Rob342
03-09-2011, 02:59 PM
JL

Filled in the ' boxes for you i think they are ok.
You can get the meanings for any of the keywords by highlighting the word then pressing F1 from the code window.
Have Fun.



'// requires all variables to be declared ( good pratice to include)
Option Explicit

Sub LogIn()
'// stops the screen from flickering does not update screen display functions until = true
Application.ScreenUpdating = False
'// Dimenions variables to be used in this sub routine
Dim uname As String
Dim pword As String
Dim rng As Range
'// selects "loginform" sheet & sets default values to variables
Sheets("LogInForm").Select
uname = Range("Username")
pword = Range("Password")
'// If the values in uname and pword are less than or greater than. ie not empty
If uname <> "" And pword <> "" Then
'// With = the selected sheet name below,do certain things in the code
With Sheets("UserDetails")
'// If the code does not find the sheet in the with statement then go to the next statement
On Error Resume Next
'// Sets the range(rng) to column(1) find uname
Set rng = .Columns(1).Find(uname)
'// if no name found then error returns to 0
On Error GoTo 0
'// If not statement returns either true of false, depends what is in Set rng
If Not rng Is Nothing Then
'// Resets the sheets & range below to "" ie blank
Sheets("LogInForm").Range("Username") = ""
Sheets("LogInForm").Range("Password") = ""
'// rng.Offset persumes it has a header row on the sheet, downward 1 row
If pword = rng.Offset(0, 1).Value Then
'// Selects the named sheet in quotes
Sheets("MainMenu").Select
'// Update the screen with any changes handled below
Application.ScreenUpdating = True
'// Exit sub routine and also handles error code 0
Exit Sub
'// Used in conjuction with the If statements
End If
'// windows message box displays if the user enters an invalid name and password returns OK only prompt
MsgBox "Please try again, You must enter a valid username and password", vbOKOnly
'// Ends the routine in the With statement
End With
'// used with the With statement or if statements and displays another message box
Else
'// Displays message box part of the above else statement
MsgBox "You must enter both a username and password"
'// End the previous IF statements
End If
'// Dont need the one above ? you have this statement above!
Application.ScreenUpdating = True
'// Exit sub routine.
End Sub


Rob

JLSHARP92
03-09-2011, 03:09 PM
Thank you very much for this :) And thanks for the shortcut tip, I'll keep that in mind when I have to create more parts of my system.

Rob342
03-09-2011, 03:32 PM
jl can you mark post as solved if ok
had a look at your new post, there is a user form on the system that could be possibly what you want or could modify to suit.
But i cant find it or remember the link.
perhaps you can ask Simon Lloyd or MD they might be able to find it for you or point you in the right direction.

Rob

JLSHARP92
03-09-2011, 03:37 PM
Marked it as solved for you :) and thanks I'll have a look for that user form