Can a button check password and filter?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • beginneraccess
    New Member
    • Feb 2019
    • 27

    Can a button check password and filter?

    Sorry to bother you all again...
    I am trying to create a login page where upon inputting the correct UserName AND password, it would filter a subform below so that they could see only their comments. Is this possible? I have it so that there are two buttons where one checks the Username and Password via VBA code and another button filters using a Macro... But I would like for one button to do everything so that if the password is incorrect, then they cannot see anything. The filter button currently works without a password and as long as there is a correct username it will filter that without looking at the password. I was thinking about just having it click the other button at the end?

    I want to say this works but it doesn't after line 25

    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub btnLogin_Click()
        Dim rs As Recordset
       
            Set rs = CurrentDb.OpenRecordset("tbl1Employees", dbOpenSnapshot, dbReadOnly)
        
        rs.FindFirst "UserName='" & Me.txtUserName & "'"
        
        If rs.NoMatch Then
            Me.lblWrongUser.Visible = True
            Me.txtUserName.SetFocus
            Exit Sub
        End If
        Me.lblWrongUser.Visible = False
        
        If rs!Password <> Nz(Me.txtPassword, "") Then
            Me.lblWrongPass.Visible = True
            Me.txtPassword.SetFocus
            Exit Sub
        End If
        Me.lblWrongPass.Visible = False
    
        ' Dim SQL As String
        'SQL = "SELECT tbl1Comments.comment, tbl1Comments.CommentDate, tbl1Comments.Followup" _
         '& "FROM tbl1Comments;"
        'Me.subComment.Form.RecordSource = SQL
        'Me.subComment.Form.Requery
        Dim search_text As String
        search_text = Me.txtUserName
    
        If Nz(Me.txtUserName.Value, "") = "" Then
            Me.FilterOn = False
            Me.txtUserName.SetFocus
            Exit Sub
        End If
        Me.Filter = "UserName like '*" & Me.txtUserName.Value & "*' or userName like '*"
        Me.FilterOn = True
        Me.txtUserName.SetFocus
        Me.txtUserName.Value = search_text
        Me.txtUserName.SelStart = Len(Nz(Me.txtUserName.Value, "")) & 1
    
    End Sub
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3653

    #2
    Beginner,

    1. You're no so much of a beginner after all...

    2. Do not EVER save passwords in your database unless they are encrypted. Better yet: do not EVER save passwords in your database.

    3. If you want User-specific productivity in your DB, take a good, long look at this Article on Creating User Permissions in an Access Database. Yes, it is a bit much to chew on, but I have been using this for many years, and it is the best way for you to keep out of the username and password business and leave that up to your network folks. Once you know who is using your DB, you can control all forms based on that user.

    4. I admit, I have not looked at your code much, mainly because of point number 2 above.

    Hope this hepps!

    Comment

    • beginneraccess
      New Member
      • Feb 2019
      • 27

      #3
      Dear twinnyfo,

      Thank you for the compliment! I actually have been taking bits of other people's code and putting them together

      Do you think it will be ok to have a separate database that holds just the comments for the employees separately? I was planning to have the tables perhaps linked to another database in which they would need access to. That way they cannot look up other people's password and can only see their filtered data.

      I figured out how to do it. I made a macro and made it run macro at the end of the code that way it can go through the username and password first then do the macro of filtering the data. :)

      Comment

      • twinnyfo
        Recognized Expert Moderator Specialist
        • Nov 2011
        • 3653

        #4
        If you took a look at the article, you would see that if you are able to identify the user, based upon their system login credentials, you can then use the User data to filter your forms so they can only see what you want them to see. This is not limited to switchboard menus (as described in the artcle), but can apply to all manner of data. As long as you associate a user's ID with a record, you can filter by User ID.

        Make sense?

        Again, trying to gently convince you that saving passwords in your DB (in any way) is not hte best way to go. If you must, make sure it is encrypted--and encryption is not my strong suit, but there are several experts who really understand the principles better.

        My method is a great place to start. The only way this would not work is if you have an "open" machine that multiple users gain acces to but don't have to log into "the machine"--which again, is not wise.

        Comment

        • beginneraccess
          New Member
          • Feb 2019
          • 27

          #5
          Thank you for your quick reply!

          Ok I will look further into User-specific productivity. I may have questions in a bit, but I will try my best to understand!

          Comment

          • twinnyfo
            Recognized Expert Moderator Specialist
            • Nov 2011
            • 3653

            #6
            Great - standing by to assist with any additional questions....

            Comment

            Working...