Hi,
You could follow these steps to make a successfully user Login input. Also,
you could email me asking this sample
I could be reached at
(e-mail address removed) (Leave online for spam
1. Create a new mdb files
2. Create a table "tblUsers"
id autonumber primarykey
userid text
passowrd text
remember to input some data to the table for testing
3. Create a form, named "frmLogin", add label "Login", add text "UserId",
add text "Passowrd", add command button "OK", add command button "Cancel"
4. The following codes are added to the "frmLogin"
''''''''''''''''''START of
Codes''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Compare Database
Private Sub Cancel_Click()
' This code created by Command Button Wizard.
On Error GoTo Err_Cancel_Click
' Close form.
DoCmd.Close
Exit_Cancel_Click:
Exit Sub
Err_Cancel_Click:
MsgBox Err.Description
Resume Exit_Cancel_Click
End Sub
Private Sub OK_Click()
On Error GoTo Err_OK_Click
Dim strMsg As String, strTitle As String
Dim intStyle As Integer
' If Sales by Year report is not being opened for previewing or
printing,
' cause an error. (blnOpening variable is true only when report's Open
event
' is being executed.)
If Not Forms![Form1].blnOpening Then Err.Raise 0
Dim varx As Variant
varx = DLookup("userid", "tblUsers", "userid = '" &
[Forms]![frmLogin]![Text3] & "' and password = '" &
[Forms]![frmLogin]![Text5] & "'")
Debug.Print "1"
Debug.Print [Forms]![frmLogin]![Text3]
Debug.Print [Forms]![frmLogin]![Text5]
Debug.Print varx
If IsNull(varx) Then GoTo Input_Error
' Hide form.
Me.Visible = False
Exit_OK_Click:
Exit Sub
Err_OK_Click:
strMsg = "To use this form, you must preview Form1 from the Database
window or Design view."
intStyle = vbOKOnly
strTitle = "Open from Form1"
MsgBox strMsg, intStyle, strTitle
Resume Exit_OK_Click
Input_Error:
strMsg = "Input Error, please check your userName and password is valid"
intStyle = vbOKOnly
strTitle = "Incorrect Input"
MsgBox strMsg, intStyle, strTitle
Exit Sub
End Sub
''''''''''''''''END of
Codes''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
5. Create a new from Form1 (I assume Form1 as the form you will see after
login, you could assign it to whatever form you want)
The following codes should be add to form1
''''''''''''''''''START of
Codes''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Option Compare Database ' Use database order for string comparisons.
Option Explicit ' Requires variables to be declared before they are used.
Public blnOpening As Boolean
Private Sub Form_Open(Cancel As Integer)
Dim strDocName As String
strDocName = "frmLogin"
' Set public variable to True so Sales by Year Dialog knows that report
' is in its Open event.
blnOpening = True
' Open form.
DoCmd.OpenForm strDocName, , , , , acDialog
' If Sales by Year Dialog form isn't loaded, don't preview or print
report.
' (User clicked Cancel button on form.)
If IsLoaded(strDocName) = False Then Cancel = True
'Set public variable to False, signifying that Open event is finished.
blnOpening = False
End Sub
''''''''''''''''END of
Codes''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
6. Now, it finishes. If you open Form1, you will be shown the form frmLogin
asking you input username first; If you open frmLogin directly, you will be
asked to open Form1
Hope it could meet your requirement and thank you for your patience and
cooperation. If you have any questions or concerns, don't hesitate to let
me know. We are here to be of assistance!
Sincerely yours,
Mingqing Cheng
Microsoft Developer Community Support