How to fill a form field after upon click, prompt a login, and fill the form field with the username of the logged in

lbradbury

New Member
Joined
May 14, 2020
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a form that has a text box in a PruchaseReqHeader form, that upon click it opens another form. This form is frm_login, and prompts for a username and password. The goal is once the user puts in their username and password, the field that was originally clicked populates with the username The code underneath the frm_login, upon clicking the login button is below. This is where I'm stuck, I don't know how to get the text box to populate with the username.

VBA Code:
Private Sub cmd_login_Click()

  Dim db As DAO.Database
  Dim rst As DAO.Recordset
  Dim strSQL As String
 
  If Trim(Me.txt_username.Value & vbNullString) = vbNullString Then
    MsgBox prompt:="Username should not be left blank.", buttons:=vbInformation, title:="Username Required"
    Me.txt_username.SetFocus
    Exit Sub
  End If
 
  If Trim(Me.txt_password.Value & vbNullString) = vbNullString Then
    MsgBox prompt:="Password should not be left blank.", buttons:=vbInformation, title:="Password Required"
    Me.txt_password.SetFocus
    Exit Sub
  End If
 
  'query to check if login details are correct
  strSQL = "SELECT FirstName FROM tbl_login WHERE Username = """ & Me.txt_username.Value & """ AND Password = """ & Me.txt_password.Value & """"
 
  Set db = CurrentDb
  Set rst = db.OpenRecordset(strSQL)
  If rst.EOF Then
    MsgBox prompt:="Incorrect username/password. Try again.", buttons:=vbCritical, title:="Login Error"
    Me.txt_username.SetFocus
  Else
    MsgBox prompt:="Hello, " & rst.Fields(0).Value & ".", buttons:=vbOKOnly, title:="Login Successful"
    DoCmd.Close acForm, "frm_login", acSaveYes
  End If
 
 Set db = Nothing
 Set rst = Nothing

End Sub
 

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
1,343
As you can see I am new member so the rules are NOT drilled into my head
Glad to hear you got the solution.

Since you are a new member, you might not know how it works. You just need to mark the solution post that answered your question instead of your closure post.
Therefore, I switched the marked post with the actual solution post.
 

Some videos you may like

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

lbradbury

New Member
Joined
May 14, 2020
Messages
46
Office Version
  1. 365
Platform
  1. Windows
Glad to hear you got the solution.

Since you are a new member, you might not know how it works. You just need to mark the solution post that answered your question instead of your closure post.
Therefore, I switched the marked post with the actual solution post.
His solution wasn't actually the right one, the one I posted was the correct solution.
 

smozgur

BatCoder
Joined
Feb 28, 2002
Messages
1,343
His solution wasn't actually the right one, the one I posted was the correct solution.

The solution has been provided in the post that I marked as the solution. It doesn't have to be your exact implementation since nobody is interested with your control names, etc.

As the final note, the following is the key snippet that is answering the original question.
VBA Code:
Forms!frm1.txt_approval = strUser
 

lbradbury

New Member
Joined
May 14, 2020
Messages
46
Office Version
  1. 365
Platform
  1. Windows
The solution has been provided in the post that I marked as the solution. It doesn't have to be your exact implementation since nobody is interested with your control names, etc.

As the final note, the following is the key snippet that is answering the original question.
VBA Code:
Forms!frm1.txt_approval = strUser
Understood
 

Watch MrExcel Video

Forum statistics

Threads
1,127,191
Messages
5,623,289
Members
415,963
Latest member
PatrickDurning

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top