Solved how to display a field value of a table in a messagebox (1 Viewer)

musmoniem

Member
Local time
Today, 17:08
Joined
Apr 30, 2020
Messages
30
i made a login form for the database and its controlled from a table had usernames and passwords .. i also made a messagebox when the username and password is correct i want to view the name of the user ... any help please
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:08
Joined
May 7, 2009
Messages
19,233
I am guessing for your table structure:

FullName (string)
Username (string)
Password (string)

on your login form, to test if valid user:

Dim strName As String
StrName = DLookup("FuillName", "tblUsers", "UserName ='" & Me!txtUserName & "' An Password ='" & Me!txtPassword & "'") & ""
If strName <> "" Then
Msgbox "Welcome " & strName
Else
Msgbox "Invalid user!"
End If
 

musmoniem

Member
Local time
Today, 17:08
Joined
Apr 30, 2020
Messages
30
I am guessing for your table structure:

FullName (string)
Username (string)
Password (string)

on your login form, to test if valid user:

Dim strName As String
StrName = DLookup("FuillName", "tblUsers", "UserName ='" & Me!txtUserName & "' An Password ='" & Me!txtPassword & "'") & ""
If strName <> "" Then
Msgbox "Welcome " & strName
Else
Msgbox "Invalid user!"
End If

thats the code i used .. could you kindly edit it for me ??? thnks in advance

the table structure is
Name
Username
Passord

Private Sub Command1_Click()
If IsNull(Me.txtUsername) Then
MsgBox "Please Enter Username", vbInformation, " Username Required"
ElseIf IsNull(Me.txtPassword) Then
MsgBox "Please Enter Password", vbInformation, " Password Required"
Else
If Nz(DLookup("Password", "Users", "Username = '" & Me.txtUsername & "'"), "Password") = Me.txtPassword Then
MsgBox "Welcome Back" & Me.Name, vbInformation, "Access Allowed"
Else
MsgBox "Sorry .. Wrong Username or Password", vbExclamation, "Access Denied"
End If
End If
End Sub
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:08
Joined
May 7, 2009
Messages
19,233
Code:
Private Sub Command1_Click()
    If IsNull(Me.txtUsername) Then
        MsgBox "Please Enter Username", vbInformation, " Username Required"
        Me.txtUsername.SetFocus
        Exit Sub
    End If
    If IsNull(Me.txtPassword) Then
        MsgBox "Please Enter Password", vbInformation, " Password Required"
        Me.txtPassword.SetFocus
        Exit Sub
    End If

    ' all have values then validate it agains the table
    Dim sName As String

    sName = DLookup("theFieldForFullName", "Users", "Username = '" Me.txtUserName & "' And Password = '" & Me.txtPassword & "'") & ""
    If sName <> "" Then
        MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed"
        DoCmd.Close acForm, Me.Name
    Else
        MsgBox "Sorry .. Wrong Username or Password", vbExclamation, "Access Denied"
    End If
End Sub

you need to replace "theFieldForFullName" with the field name in table "Users" of the Real name of the user.
 

musmoniem

Member
Local time
Today, 17:08
Joined
Apr 30, 2020
Messages
30
Code:
Private Sub Command1_Click()
    If IsNull(Me.txtUsername) Then
        MsgBox "Please Enter Username", vbInformation, " Username Required"
        Me.txtUsername.SetFocus
        Exit Sub
    End If
    If IsNull(Me.txtPassword) Then
        MsgBox "Please Enter Password", vbInformation, " Password Required"
        Me.txtPassword.SetFocus
        Exit Sub
    End If

    ' all have values then validate it agains the table
    Dim sName As String

    sName = DLookup("theFieldForFullName", "Users", "Username = '" Me.txtUserName & "' And Password = '" & Me.txtPassword & "'") & ""
    If sName <> "" Then
        MsgBox "Welcome Back " & sName, vbInformation, "Access Allowed"
        DoCmd.Close acForm, Me.Name
    Else
        MsgBox "Sorry .. Wrong Username or Password", vbExclamation, "Access Denied"
    End If
End Sub

you need to replace "theFieldForFullName" with the field name in table "Users" of the Real name of the user.

it gives me this error
1588247738131.png
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:08
Joined
May 7, 2009
Messages
19,233
forgot, add ampersand (&) before Me.txtUserName:

"Username = '" & Me.txtUserName & "' And Password = '" & Me.txtPassword & "'") & ""
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 23:08
Joined
May 7, 2009
Messages
19,233
you're welcome, bro.
 

Users who are viewing this thread

Top Bottom