This post is a compilation of two of my previous posts:
https://vbahowto.com/ms-access-object-security/
https://vbahowto.com/ms-access-login-form-revised/
This elaborates on the two posts a bit:
First, here are all of the database objects:
There are 2 tables here:
tblLogin and tblAdmin
Here are the main forms:
frmLogin – this is the form that opens when the database opens
Here is the code that gets executed when the “Login” button is clicked:
Private Sub btnLogin_Click() Dim rst As Recordset Dim strSQL As String strSQL = "SELECT UserName, UserPWD FROM tblLogin WHERE UserName = '" & Me.txtName & "' AND UserPWD = '" & Me.txtPWD & "'" Set rst = CurrentDb.OpenRecordset(strSQL) If Not rst.EOF Then 'open m_LoggedInUser = Me.txtName DoCmd.Close acForm, Me.Name MsgBox "Welcome: " & m_LoggedInUser DoCmd.OpenForm "frmMain" Else MsgBox "Your password is wrong" End If End Sub
The way it works is that the “tblLogin” table is checked for the existence of the user entered into the form. If a user record is found, they can proceed.
THIS FORM IS LOADED AS A DIALOG FORM WHEN THE DATABASE IS OPENED:
Once that form is passed, the user goes to the main user form, “frmMain”
The administrators form is called “frmAdmin”
Here the admin can add and modify user access rights to the forms and reports.
Here is the code:
Private Sub lblPolicy_Click() DoCmd.OpenTable "tblAdmin" End Sub Private Sub lblRegister_Click() DoCmd.OpenForm "frmRegister" End Sub Private Sub lblCurrent_Click() DoCmd.OpenTable "tblLogin" End Sub
This form will open another form called “frmRegister”, which will allow you to enter new users:
This is the code that executes when you click the “Register” button:
Private Sub btnclose_Click() DoCmd.Close acForm, Me.Name End Sub '================= 'frmRegister code: '================= Private Sub btnOK_Click() Dim strNewUser As String Dim strPassword As String Dim strSQLInsert As String strNewUser = Me.txtUser strPassword = Me.txtPassword strSQLInsert = "INSERT INTO tblLogin (UserName,UserPWD) VALUES ('" & strNewUser & "','" & strPassword & "')" CurrentDb.Execute strSQLInsert MsgBox "Thanks for registering!" DoCmd.Close acForm, Me.Name End Sub Private Sub txtPassword_AfterUpdate() Me.lblPassword.Caption = Me.txtPassword End Sub
That’s about all. I’ll load the database for you all here below.
Let me know if you need help.
Here is the database access database users and permissions.accdb
How can I interact with other Office applications (Excel) using VBA in Access?
Need to write your Access data or query to an Excel file? Here is the how to do it: Most people are familiar with Excel and know how to use it well (enough), and when you start talking about Access, they get scared off, and don’t know what to do anymore. Well, here you are […]
How To Generate A XML File With Access VBA
XML is used to structure your data. You can send a CSV file to another person, but using XML, you can structure your data in a more elegant format, and get very specific about your fields, and what data is contained within your tags (your field names). This is different than a basic CSV file […]
Mouse Click Counter On Access Form
This post will demonstrate how you can count the number of clicks on your button in a certain time frame. It will function like a game . Here is the database form all in one: Option Compare Database Public m_dteStartTime As Date Public m_dteStopTime As Date Private Sub btnClicker_Click() Dim intValue As Integer Dim rst […]
Shared Access Database Management
Here is a handy setup for those who need to manage a shared access database. Either you can create this from scratch as per the example or add the new tables to your existing database. This setup will track: 1. the users currently using the database 2. what time the user and computer name currently […]