I do something similar. I do have a login form but it is only presented to user if the user is new and therefore need a new record in Users table. The login form is default when db opens. The login form is bound to another table called Updates. This code saves computer name into Users table so I know who and where users are. User initials are passed to the Menu form with the OpenArgs argument of OpenForm method. Guess I could bind the Menu form to Users table and filter to the user record, think I thought about doing that but decided not to. Code in Menu form Open event sets textbox to the OpenArgs value. Menu form never closes so the initials are always available for other procedures. Code behind Menu form nulls the computer name field when user quits.
This is code behind the Login form.
Code:
Private Sub Form_Load()Dim Shell
If Me.tbxVersion <> Me.lblVersion.Caption Then
If DLookup("Permissions", "Users", "UserNetworkID='" & Environ("UserName") & "'") <> "admin" Then
'Because administrator opens the master development copy, only run this for non-administrator users
'Check for updates to the program on start up
'If values don't match then there is a later version
Set Shell = CreateObject("WScript.Shell")
Shell.Run CurrentProject.Path & "\Update.vbs"
Dim WAIT As Double
WAIT = Timer
While Timer < WAIT + 3
DoEvents
Wend
Set Shell = Nothing
Application.Quit
End If
Else
Me.tbxVersion.Visible = False
UserLogin
End If
End Sub
Private Sub tbxUser_AfterUpdate()
If Me.tbxUser Like "[A-Z][A-Z][A-Z]" Or Me.tbxUser Like "[A-Z][A-Z]" Then
CurrentDb.Execute "INSERT INTO Users(UserNetworkID, UserInitials, Permissions) VALUES('" & VBA.Environ("UserName") & "', '" & UCase(Me.tbxUser) & "', 'staff')"
Call UserLogin
Else
MsgBox "Not an appropriate entry.", vbApplicationModal, "EntryError"
End If
End Sub
Private Sub UserLogin()
Me.tbxUser = DLookup("UserInitials", "Users", "UserNetworkID='" & Environ("UserName") & "'")
If Not IsNull(Me.tbxUser) Then
CurrentDb.Execute "UPDATE Users SET ComputerName='" & VBA.Environ("ComputerName") & "' WHERE UserInitials='" & Me.tbxUser & "'"
DoCmd.OpenForm "Menu", acNormal, , , , acWindowNormal, Me.tbxUser
DoCmd.Close acForm, Me.Name, acSaveNo
End If
End Sub
Here is code for the VBScript file that is called by the above procedure.
Code:
Const OverwriteExisting = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
'following will copy Access file
objFSO.CopyFile "\\dotatufs02\CRM\Lab\Database\Program\Install\MaterialsDatabase.accdb", "c:\", OverwriteExisting
'following will now open the Access file
Set oShell = CreateObject("WScript.Shell")
oShell.Run """C:\Program Files\Microsoft Office\Office12\msaccess.exe"" ""c:\MaterialsDatabase.accdb"""