Results 1 to 2 of 2
  1. #1
    GGCR is offline Novice
    Windows 7 64bit Access 2007
    Join Date
    Jan 2014
    Posts
    6

    Log In Screen programming problems


    I am trying to create a login page in Access 2007 but can not seem to get it to work. Below is the code i am using. I am a beginner at this and not overly savy in programming. Can anyone figure out what I am doing wrong? Thank you in advance for your help.

    Private Sub cmdlogin_Click()
    Dim dbs As Database
    Dim rstUserPwd As Recordset
    Dim bFoundMatch As Boolean


    Set dbs = CurrentDb


    Set rstUserPwd = dbs.OpenRecordset("qryUserPwd")


    bFoundMatch = False


    If rstUserPwd.RecordCount > 0 Then
    rstUserPwd.MoveFirst
    Do While rstUserPwd.EOF = False
    If rstUserPwd![Username] = Form_frmLogin.txtUsername.Value And rstUserPwd![Password] = Form_frmLogin.TxtPassword.Value Then bFoundMatch = True
    Exit Do
    End If
    rstUserPwd.MoveNext
    Loop

    End If


    If bFoundMatch = True Then
    DoCmd.Close acForm, Me.Name
    DoCmd.OpenForm "frmSwitchboard"


    Else
    '
    MsgBox "Incorrect Uasernam or Password"
    End If
    rstUserPwd.Close
    End Sub

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You didn't specify what errors you are getting.... did you single step through the code to see where the error(s) were?
    For help on debugging, see http://www.cpearson.com/Excel/DebuggingVBA.aspx

    I always specify which library I using, especially in A2007. You need to set a reference to "Microsoft DAO 3.6 Object Library". (IIRC, in A2007 the default library is ADO.)

    The IF() function has two forms, the single line form or the block form. Inside the do loop, you combined the two - cannot do that.

    "Form_frmLogin.txtUsername" is how Access refers to a form. If the code is in the form module, you should use "Me.frmLogin.txtUsername" or "Forms!frmLogin.txtUsername".
    If the code is in a standard module, you would have to use "Forms!frmLogin.txtUsername".

    You do not have to add ".Value", since "Value" is the default property. It doesn't hurt, but it is not necessary.

    Added clean up stuff.


    My changes are in blue:
    Code:
    Private Sub cmdlogin_Click()
       Dim dbs As DAO.Database
       Dim rstUserPwd As DAO.Recordset
       Dim bFoundMatch As Boolean
    
       Set dbs = CurrentDb
       bFoundMatch = False
    
       Set rstUserPwd = dbs.OpenRecordset("qryUserPwd")
    
       If rstUserPwd.RecordCount > 0 Then
          rstUserPwd.MoveFirst
          Do While rstUserPwd.EOF = False
             If rstUserPwd![UserName] = Me.txtUsername And rstUserPwd![Password] = Me.TxtPassword Then
                bFoundMatch = True
                Exit Do
             End If
             rstUserPwd.MoveNext
          Loop
       End If
    
    
       If bFoundMatch = True Then
          DoCmd.Close acForm, Me.Name
          DoCmd.OpenForm "frmSwitchboard"
       Else
          '
          MsgBox "Incorrect Username or Password"
       End If
    
       'clean up
       rstUserPwd.Close
       Set rstUserPwd = Nothing
       Set dbs = Nothing
    
    End Sub
    PS I have not executed (tested) the code, just reviewed it.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to have a Subform fit to screen?
    By Astron2012 in forum Forms
    Replies: 2
    Last Post: 08-18-2012, 09:03 AM
  2. Replies: 1
    Last Post: 01-06-2012, 03:55 PM
  3. Log-in Screen
    By imintrouble in forum Forms
    Replies: 8
    Last Post: 10-11-2011, 04:24 AM
  4. Replies: 11
    Last Post: 06-05-2011, 09:51 PM
  5. print screen
    By SlowPoke in forum Access
    Replies: 1
    Last Post: 10-06-2010, 04:09 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums