Link to home
Start Free TrialLog in
Avatar of andyb7901
andyb7901

asked on

VBA - Login Script

I have taken over some coding from someone and am getting a little confused with some of the methids they have used. I have the following script which should look at a text box called txtName and a password bov called txtPassword, and chech to see if the user has enetered in the ocrrect details. Once the user clicks on the image labelled imgContinue they should be able to know if they have succesfully ligged into the system or not. Could someone please check the code and offer some help of where the code or I have gone wrong. As I have said i am trying to combine my own code along with another coders.

Thanks



Option Compare Database
Option Explicit

Private Sub Form_Load()
'on load maxmise this form
DoCmd.Maximize

End Sub

Private Sub imgContiniue_Click()

'on the click of the continue button
On Error Goto Err_cmdContinue_Click

Dim username As String
Dim stDocName As String
Dim stLinkCriteria As String
Dim Userstat As String

    'define target form
    stDocName = "frmMainMenu"
    'open target form
    DoCmd.Maximize
   
    'Userstat = Me.UserStatus.Caption
    stDocName = "connekt4"
   
    On Error Resume Next
   
    username = Me.txtName

    If Me.imgContinue_Active.Visible = True Then
   
        Application.Echo False
        DoCmd.Hourglass True
            'if the password is right then on click of the blue continue display buttons
           
            Me.imgContinue_Active.Visible = False
            Me.imgContinueUnactive.Visible = False
           
            'check to see if the user can have access to the administration screen
               
                'Client_SQL_Search.startClientDetails
                DoCmd.Close acForm, "Form_frmFlash"
                DoCmd.OpenForm stDocName, , , stLinkCriteria
                     
                Application.Echo True
                DoCmd.Hourglass False
    End If
               
 ' if the password is wrong then bring up error message
    If Me.imgContinue_Active.Visible = False Then

        Me.txtPassword.SetFocus

    End If
   
Exit_cmdContinue_Click:
    Exit Sub

Err_cmdContinue_Click:
'error code
    MsgBox Err.Description
    Resume Exit_cmdContinue_Click

End Sub

Private Sub cmdChangePassword_Click()
Application.Echo False
DoCmd.Hourglass True

DoCmd.Close acForm, "Form_frmFlash"
DoCmd.OpenForm "UserSetup"
With Form_UserSetup
.RecordsetType = 0
.Licencee_Name.Locked = False
.fldUserName.Locked = False
.UserPassword.Locked = False
Application.Echo True
DoCmd.Hourglass False
.Licencee_Name.SetFocus
End With
End Sub

Private Sub cmdclose_click()

'dialog box to continue
Dim message As String
Dim buttonsandicons As Integer
Dim title As String
Dim Response As Integer

'the message of the dialog box
message = "Do you want to exit Connekt4?"

'the title of the dialog box
title = "Exit Connekt4"

'the look of the dialog box
buttonsandicons = vbYesNo + vbQuestion

'displaay dialog and get users response
Response = MsgBox(message, buttonsandicons, title)

'evaluate the user's response
If Response = vbNo Then

End

Else

DoCmd.Quit
End If
End Sub

Private Sub cmdcontinue_Click()
Dim username As String
Dim stDocName As String
Dim stLinkCriteria As String
Dim Userstat As String
   
    'Userstat = Me.UserStatus.Caption
    stDocName = "connekt4"
    On Error Resume Next
   
    username = Me.txtName

If Me.imgContinue_Active.Visible = True Then
Application.Echo False
DoCmd.Hourglass True
    'if the password is right then on click of the blue continue display buttons
   
    'Me.cmdContinue.Visible = False
    Me.imgContinue_Active.Visible = False
    Me.imgContinueUnactive.Visible = False
   
    'check to see if the user can have access to the administration screen
       
            'Client_SQL_Search.startClientDetails
            DoCmd.Close acForm, "Form_frmFlash"
            DoCmd.OpenForm stDocName, , , stLinkCriteria
             
             Application.Echo True
            DoCmd.Hourglass False
End If
               
 ' if the password is wrong then bring up error message
If Me.imgContinue_Active.Visible = False Then

        Me.txtPassword.SetFocus

        End If

   
End Sub

Private Sub cmdHelp_Click()
Dim HlpPth As String

HlpPth = Application_Paths.HlpPth

Me.cmdHelp.HyperlinkAddress = HlpPth

End Sub


Private Sub cmdOpenMainSys_Click()
On Error Goto Err_cmdOpenMainSys_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim Userstat As String
     Application.Echo False
DoCmd.Hourglass True
    Userstat = Me.UserStatus.Caption
   
'form_Clients.RecordSource = Client_SQL_Search.startClientDetails
    stDocName = "Connekt4"
    'set mainsystems record source to a's
    Client_SQL_Search.startClientDetails
    DoCmd.Close acForm, "Form_frmFlash"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
     Application.Echo True
DoCmd.Hourglass False
 
   

Exit_cmdOpenMainSys_Click:
    Exit Sub

Err_cmdOpenMainSys_Click:
    MsgBox Err.Description
    Resume Exit_cmdOpenMainSys_Click
   
End Sub

Private Sub txtPassWord_AfterUpdate()
If Me.txtPassword.Value = "sysadm" Then
    'cmdContinue.Visible = True
    imgContinue_Active.Visible = True
    imgContinueUnactive.Visible = False
    cmdChangePassword.Visible = True
    End
Else

    If Me.txtPassword.Value = "5J6Y19" Then
        'cmdContinue.Visible = True
        imgContinue_Active.Visible = True
        imgContinueUnactive.Visible = False
        cmdChangePassword.Visible = True
        End
   
Else
    If Me.txtName.Value = Me.fldUserName Then
        If Me.txtPassword.Value = Me.UserPassword Then
            'cmdContinue.Visible = True
            imgContinue_Active.Visible = True
            imgContinueUnactive.Visible = False
            End
            Else
            MsgBox "You have entered an incorrect user name or password", vbOKOnly, "Incorrect User Name or Password"
            Me.txtName.SetFocus
        End If
   
Else
    MsgBox "You have entered an incorrect user name or password", vbOKOnly, "Incorrect User Name or Password"
    Me.txtName.SetFocus
       
    End If
    End If
End If
End Sub
Avatar of jefftwilley
jefftwilley
Flag of United States of America image

give this a try:

Private Sub txtPassWord_AfterUpdate()
Dim strPassword As String
Dim strUserName As String
Select Case strPassword
Case "sysadm"
    Me.imgContinue_Active.Visible = True
    Me.imgContinueUnactive.Visible = False
    Me.cmdChangePassword.Visible = True
Case "5J6Y19"
    Me.imgContinue_Active.Visible = True
    Me.imgContinueUnactive.Visible = False
    Me.cmdChangePassword.Visible = True
Case strUserName
    If Me.txtPassword.Value = Me.UserPassword Then
    Me.imgContinue_Active.Visible = True
    Me.imgContinueUnactive.Visible = False
     End If
Case Else
    MsgBox "You have entered an incorrect user name or password", vbOKOnly, "Incorrect User Name or Password"
    Me.txtName.SetFocus
End Select
end sub
Sorry, slight mod

Function passwordmanage()
Dim strPassword As String
Dim strUserName As String
strPassword = Nz(Me.txtPassword.Value)
strUserName = Nz(Me.UserPassword.Value)
Select Case strPassword
Case "sysadm"
    Me.imgContinue_Active.Visible = True
    Me.imgContinueUnactive.Visible = False
    Me.cmdChangePassword.Visible = True
Case "5J6Y19"
    Me.imgContinue_Active.Visible = True
    Me.imgContinueUnactive.Visible = False
    Me.cmdChangePassword.Visible = True
Case strUserName
    If strPassword = strUserName Then
        Me.imgContinue_Active.Visible = True
        Me.imgContinueUnactive.Visible = False
     End If
Case Else
    MsgBox "You have entered an incorrect user name or password", vbOKOnly, "Incorrect User Name or Password"
    Me.txtName.SetFocus
End Select
End Sub
Avatar of andyb7901
andyb7901

ASKER

Would this just go in its own little section then? Would all i need for the code to work is the following code; All I need it to do is check against a hardcoded varibale for a user and password. If username doesnt match or password, through a message box error, but if it does go to next page. I keep getting silly little errros when i try to do it??

Option Compare Database
Option Explicit

Private Sub Form_Load()
'on load maxmise this form
DoCmd.Maximize

End Sub

Private Sub cmdOpenMainSys_Click()
On Error GoTo Err_cmdOpenMainSys_Click

    Dim stDocName As String
    Dim stLinkCriteria As String
    Dim Userstat As String
     Application.Echo False
DoCmd.Hourglass True
    Userstat = Me.UserStatus.Caption
   
'form_Clients.RecordSource = Client_SQL_Search.startClientDetails
    stDocName = "Connekt4"
    'set mainsystems record source to a's
    Client_SQL_Search.startClientDetails
    DoCmd.Close acForm, "Form_frmFlash"
    DoCmd.OpenForm stDocName, , , stLinkCriteria
     Application.Echo True
DoCmd.Hourglass False
 
   

Exit_cmdOpenMainSys_Click:
    Exit Sub

Err_cmdOpenMainSys_Click:
    MsgBox Err.Description
    Resume Exit_cmdOpenMainSys_Click
   
End Sub

Function passwordmanage()

Dim strPassword As String
Dim strUserName As String

strPassword = Nz(Me.txtPassword.Value)
strUserName = Nz(Me.UserPassword.Value)

Select Case strPassword

Case "sysadm"
    Me.imgContinue_Active.Visible = True
    Me.imgContinueUnactive.Visible = False
    Me.cmdChangePassword.Visible = True
Case "5J6Y19"
    Me.imgContinue_Active.Visible = True
    Me.imgContinueUnactive.Visible = False
    Me.cmdChangePassword.Visible = True
Case strUserName
    If strPassword = strUserName Then
        Me.imgContinue_Active.Visible = True
        Me.imgContinueUnactive.Visible = False
     End If
Case Else
    MsgBox "You have entered an incorrect user name or password", vbOKOnly, "Incorrect User Name or Password"
    Me.txtName.SetFocus
End Select
End Sub

Private Sub imgContinue_Click()

    'on the click of the continue button
On Error GoTo Err_cmdContinue_Click

Dim username As String
Dim stDocName As String
Dim stLinkCriteria As String
Dim Userstat As String

    'define target form
    stDocName = "frmMainMenu"
    'open target form
    DoCmd.Maximize
   
    'Userstat = Me.UserStatus.Caption
    stDocName = "connekt4"
   
    On Error Resume Next
   
    username = Me.txtName

    If Me.imgContinue_Active.Visible = True Then
   
        Application.Echo False
        DoCmd.Hourglass True
            'if the password is right then on click of the blue continue display buttons
           
            Me.imgContinue_Active.Visible = False
            Me.imgContinueUnactive.Visible = False
           
            'check to see if the user can have access to the administration screen
               
                'Client_SQL_Search.startClientDetails
                DoCmd.Close acForm, "Form_frmFlash"
                DoCmd.OpenForm stDocName, , , stLinkCriteria
                     
                Application.Echo True
                DoCmd.Hourglass False
    End If
               
 ' if the password is wrong then bring up error message
    If Me.imgContinue_Active.Visible = False Then

        Me.txtPassword.SetFocus

    End If
   
Exit_cmdContinue_Click:
    Exit Sub

Err_cmdContinue_Click:
'error code
    MsgBox Err.Description
    Resume Exit_cmdContinue_Click


End Sub

The bit of code I pasted replaces your Function passwordmanage() event as you've pasted in above.  It performs the validation on the passwords based on the user's entry.  If it's successful, ie the passwords are ok, it makes different things visible on the form. If unsuccessful, it pops that message saying "You have entered an incorrect user name or password" . What errors are you getting?
J
when i run the code i ge the following error on this line (**)

    On Error Resume Next
   
    username = Me.txtName

   ********* If Me.imgContinue_Active.Visible = True Then
   
        Application.Echo False
        DoCmd.Hourglass True

Method od Data memeber not found: Compile error
IF the code cant be fixed is there anyway not to display and redisplay things, but just to check the user and password and when the user clicks on the continue button just through an error or let them pass. Keep it very simple??
Definately. the message you're getting above is because the control (a button) called "me.imgContinue_Active" can't be found on the form. Either the object has been renamed or is no longer on the form.

A simple password form can be created..but really, all this one is doing is looking for a button it can't find. What buttons do you have on your form? Can you list them here?
J
just a button named imgContinue. Thats all. All I have one my form is the two text boxes and the imgContinue acting as a button. The button works normally on just an onclick event?
This code is made to evaluate what a user is...meaning, if he's admin, then it is supposed to activate a button that will open up an admin form. If it's joe user, then it looks like it takes him to a form called "connekt4". There's also another option to go to a form where the user can change his password.

In evaluating the password, these options are enabled or disabled using this code. If there are controls missing, then you'll get errors.

If all you'd like to do is simply allow or disallow entry into the database, then what form do you want to open if the password is accepted? What would you like to do if it fails?
J
I have solved the error above, but need to make it more simple. All I want is for the user to enter in a name (txtName) and password (txtPassword). Say admin and password. If they are correct they are allowed to login. If they are not correct, a simple message box should appear saying your wrong. No button hiding or anything, just simple, with some error messages.
I would ideally however like to have a script that looks at the username and if it is admin, with a password of password then a new "admin" button to become visible. However, if the username is simply user and password of userpassword then they can only click on continue
we'll use what we already have...but change it some.
Make your two buttons invisible for starters. One is Continue, the other is Admin. Set their default to invisible.

Create a "OK" button. Make this the On_Click code for it.

Private Sub OK_Click()
    Dim strPassword As String
    Dim strUserName As String
   
    strPassword = Nz(Me.txtPassword.Value)  '<---Nz ensures that the value isn't null
    strName = Nz(Me.txtName.Value)
    If strName = "admin" And strPassword = "password" Then
        Me.Admin.Visible = True
        Me.Continue.Visible = True
    ElseIf strName = "User" And strPassword = "userpassword" Then
        Me.Continue.Visible = True
    Else
        MsgBox "You have entered an incorrect user name or password", vbOKOnly, "Incorrect User Name or Password"
        Me.txtName.SetFocus
    End If
End Sub
It kind of works and kind of desont. When ever i simply try to add the username and password in, it throws the error message up saying wrong password. However, if i debug it and run it, everything goes fine? Do you know why?
When you debug it, are you trying out each combination of username and password? You might check to see if there's an after update event for either of the txtboxes...ensure that both textboxes are unbound. I don't know if it makes a difference, but in the code I pasted above, I capitolized "User". You could try changing that to lower case.
J
everything is lower case. Should I just put me.refresh or update after each statement?? The text boxes are unbound as well. When I debug I go through it line by line and it works fine. But doesnt if I dont debug?
There doesnt seem to be any events going on either??
What are the settings on your form...allow additions, etc...also, is your form set up as pop-up or modal? This one has me stumped. You might simply try creating a new form with the same controls.

Another thought, you have a lot of code that runs once the buttons are activated...do you still have all that in place?
J
My complete code is as follows::

Option Compare Database
Option Explicit

Private Sub Form_Load()

Dim backgrd As String
Dim imgContinue As String
Dim imgAdmin As String
Dim imgOther As String

    'on load maxmise this form
    'DoCmd.Maximize
    DoCmd.MoveSize 3500, 2000, 12000, 8000
   
    backgrd = Application.CurrentProject.Path & "\PNG\hEADED PAPER\MainLogon copy_new Logo.png"
    imgContinue = Application.CurrentProject.Path & "\PNG\continue.png"
    imgAdmin = Application.CurrentProject.Path & "\PNG\Blue_ButtonFlash.png"
    imgOther = Application.CurrentProject.Path & "\PNG\Blue_ButtonFlash.png"

    Me.admin.Visible = False
    Me.continue.Visible = False
   
    Me.Picture = backgrd
    Me.Picture = imgContinue


End Sub

Private Sub OK_Click()

Dim strPassword As String
Dim strName As String
Dim stDocName As String
Dim stLinkCriteria As String

    stDocName = "frmMainMenu"
   
    strPassword = Nz(Me.txtPassword.Value)  '<---Nz ensures that the value isn't null
    strName = Nz(Me.txtName.Value)
   
    If strName = "admin" And strPassword = "password" Then
   
        Me.admin.Visible = True
        'Me.continue.Visible = True
       
    ElseIf strName = "user" And strPassword = "userpassword" Then
   
        'Me.continue.Visible = True
       
    Else
   
        MsgBox "You have entered an incorrect user name or password", vbOKOnly, "Incorrect User Name or Password"
        Me.txtName.SetFocus
       
    End If
   
        DoCmd.Close acForm, "frmFlash"
        DoCmd.OpenForm stDocName

End Sub


It looks like all aditions and also all "allow" options are set to yes. Im not sure about pop up or modal, I cant seem to find it??
The code seems sound except for on thing.

  backgrd = Application.CurrentProject.Path & "\PNG\hEADED PAPER\MainLogon copy_new Logo.png"
    imgContinue = Application.CurrentProject.Path & "\PNG\continue.png"
    imgAdmin = Application.CurrentProject.Path & "\PNG\Blue_ButtonFlash.png"
    imgOther = Application.CurrentProject.Path & "\PNG\Blue_ButtonFlash.png"

    Me.admin.Visible = False
    Me.continue.Visible = False
   
    Me.Picture = backgrd            <----you're setting me.picture twice.
    Me.Picture = imgContinue

Otherwise...I see no reason why it won't work without stepping through it.
That was just me playing around, on that I need to put another picture on the screen, but not to sure how to do it? Do you have any suggestions. With the other problem im still not sure of how to do it? Could there be a setting somewhere you think?
I have noticed whilst im debugging that the first time i try to login the password field does not get passed across. It simply shows as blank when i debug the vairiable. However, if I login a second time the variable populates itself? Do you know any reason why this could be?
ASKER CERTIFIED SOLUTION
Avatar of jefftwilley
jefftwilley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the code. I have tried it and I get the error invlaid use of null on this line;

strPassword = Me.txtPassword.Value

Again it looks as if the data isnt being passed accross??
Im going round the bend here!! I have renamed, redone, deleted, everything and the little git text box is still being a sod!!
ok, if you're getting invalid use of null for that text box, then the name is not correct. Is the textbox called txtPassword?
It is yeah. I have even renamed it. I have also copied and pasted it from one place to another. Also, it does work after a few attempted logins, so I know the data is being passed accross sometimes??
Perhaps you have strPassword set up inside this database as a global variable. Try changing the variable name from strPassword to strPass in all the places it's used.
Im  still getting invlaid use of null. Gooing to throw the thing out of the iwndow in a second! Why would the name string work but not the password;

strPass = Me.Textpass.Value
I have finally tempoaryly fixed it by placing a .text apposed to a .value. Dont know why this would make a difference? However, if I tab for username to password, and then hit enter, it highlights the username box again. I then click on Continue and the following error comes up;

You cant reference a control or property for a control unless the control has the focus.

Any ideas why??
What's the code behind that button?
Private Sub OK_Click()

Dim strPass As String
Dim strName As String
Dim stDocName As String
Dim stLinkCriteria As String

    stDocName = "frmMainMenu"
   
    strName = Me.txtName.Value
    strPass = Me.Text26.Text

    If strPass = "" Or strName = "" Then
       MsgBox "You must enter both a username and a password", vbOKOnly, "Blank User Name or Password"
            Me.txtName.Value = ""
            Me.Text26.Value = ""
            Me.txtName.SetFocus
       Exit Sub
       
    End If
    If strName = "admin" And strPass = "password" Then
   
        Me.admin.Visible = True
        Me.continue.Visible = True
       
    ElseIf strName = "user" And strPass = "userpassword" Then
   
        Me.continue.Visible = True
       
    Else
   
        MsgBox "You have entered an incorrect user name or password", vbOKOnly, "Incorrect User Name or Password"
            Me.txtName.Value = ""
            Me.Text26.Value = ""
            Me.txtName.SetFocus
        Exit Sub
    End If
   
        DoCmd.Close acForm, "frmFlash"
        DoCmd.OpenForm stDocName

End Sub
You said " I then click on Continue and the following error comes up;"

You cant reference a control or property for a control unless the control has the focus.

Is there code for "Continue"?....or is that the OK?

in any event...comment these out "Me.txtName.SetFocus"
J
OK is my continue. However, I have taken out the .value or .text fields and all seems to be working now?? Thanks for all you great help and suggestions though!!

I know this is cheeky and a different question, and I dont ming opening a new question and giving you the points but, do you know anything about adding images at run time. I can add one image, but not multiple images??
If you're talking about adding images to your buttons....why don't you just insert them as "picture" objects right onto your form? Are they too big and make your database fat or something?

I need to add them at run time so the image folder can be moved. I dont want to hardcode them on to the page that was all. As you have said as well, they are to large to fit onto a button so they cant be added.
Create an OLE Unbound box set it's picture property link type to linked

Call it whatever you want....myOLEOBJECTNAME

then on form open, set it's picture property


Private Sub Form_Open(Cancel As Integer)
Me.OLEUnbound15.Picture = "C:\Image1.jpg"
End Sub

something like that

I have done the following but it says it diesnt suuport the propert of method. Do you think it is because i am not using the right file type, or simplu a code mismatch?

Private Sub Form_Open(Cancel As Integer)

Dim sParent As String
Dim backgrd As String
Dim imgContinue As String
Dim imgAdmin As String
Dim imgOther As String
   
    backgrd = Application.CurrentProject.Path & "\PNG\DemoVersion\LogoOn.png"
    imgContinue = Application.CurrentProject.Path & "\PNG\continue.png"
    imgAdmin = Application.CurrentProject.Path & "\PNG\Blue_ButtonFlash.jpg"
    imgOther = Application.CurrentProject.Path & "\PNG\Blue_ButtonFlash.png"

    Me.admin.Visible = False
    Me.continue.Visible = False
   
    Me.Picture = backgrd

    Me.admin.Visible = False
    Me.continue.Visible = False

Me.PictureBox1.Picture = imgAdmin

End Sub
I think that your application doesn't support .png files. try making a copy of these images, and saving the copy as a .bmp file. Access should allow it. Otherwise you're going to have to use an ActiveX object. there is some information about that here on EE if you search for PNG.
J
I have tried to use both a bmp and a jpg but with no luck, I keep getting the same error. I dont know if I am doing it wirte. I have created an OLE image box name imgBack. I have created an unbound one. I have then assigned its type to bmp. I have then done the following piece of coding;

Private Sub Form_Open(Cancel As Integer)

Dim sParent As String
Dim bckParent As String
Dim path As String

'Define the parent folder of images
sParent = CurrentProject.path & "\PNG\"
bckParent = CurrentProject.path & "\PNG\DemoVersion\"

    'Now set the pictures for your image controls
    Me.imgBack.Class = "jpegfile"
    Me![imgBack].Picture = sParent & "Blue_ButtonFlash.jpg"

End Sub


Now I have no diea if this will work, but have no idea if i am doing the right thing? Can you help?
Go to your form in design view.
Go to the menu bar and select the Unbound Object Frame button
Create a frame on your form

The Insert Object Wizard will pop up.

Select "Create from File" then browse to find your image.

Once you find it, select OK

Now go to that OLEUnbound box that you just created and right click on it. Select the Change To option and select "Image".
It will tell you the operation can't be undone...say ok

now go to the properties for this object. Where it says Picture...(Bitmap), or whatever type you chose..change that to (none)

Where it says picture Type, change that to linked.

Save the form.

Now you can assign the object it's picture like this

Me.YOUR OLEUnbound Box's Name.Picture = imgAdmin

Give that a go.
J

Good God, its actually working!! Thanks for all your great help!!!
TaaaaDaaa!!! lol
Glad to hear it. Let me/EE know if there's anything else you need help with. Good luck!
J