Putting a column from a recordset into a textbox (1 Viewer)

cigarprofiler

Registered User.
Local time
Yesterday, 17:53
Joined
Mar 25, 2017
Messages
32
Here's what I'm trying to do:

I have an unbound login form that looks up credentials in tbl_users. So far, so good, thank you Google, this forum and copy-paste.

The match is made on user name and password, but I also want to retrieve the userid and put it in a textbox on the login form. Here's what I tried so far (in bold):

Code:
Private Sub btn_login_Click()

    Dim rs As Recordset
    [B]Dim str_userid As String[/B]
    
    Set rs = CurrentDb.OpenRecordset("public_tblusers", dbOpenSnapshot, dbReadOnly)
    
    rs.FindFirst "username='" & Me.txt_user_name & "'"
        
    If rs.NoMatch Then
        Me.lbl_user_not_found.Visible = True
        Me.txt_user_name.SetFocus
        Exit Sub
    End If
    Me.lbl_user_not_found.Visible = False

'   look-up column userid from public_tblusers that belongs to the username
'   and put it in txt_userid on the open login form

   [B]str_userid = rs!userid[/B]    ' QUESTION: how do I put it in txt_userid?
    
    If rs!userpassword <> Nz(Me.txt_password, "") Then
        Me.lbl_password_incorrect.Visible = True
        Me.txt_password.SetFocus
        Exit Sub
    End If
    Me.lbl_password_incorrect.Visible = False
    DoCmd.OpenForm "frm_review"
    Me.Form.Visible = False

End sub

I have tried to set the control source of txt_userid to str_userid but that doesn't work (it's not an option in the drop-down and if I do =str_userid it converts to =[str_userid] and puts #NAME? in the textbox).

Can someone give me a hint?
 
Last edited:

isladogs

MVP / VIP
Local time
Today, 01:53
Joined
Jan 14, 2017
Messages
18,261
the line

Code:
 me.txtuserid= struserid

Should work fine providing txtuseid is an unbound textbox
BUT you mentioned a drop down

It WON'T work if txtuserid is a combo box.
 

cigarprofiler

Registered User.
Local time
Yesterday, 17:53
Joined
Mar 25, 2017
Messages
32
Thanks, it works. And so simple!

How do I set the OP to "Solved"? Or is this something the admins do?
 

isladogs

MVP / VIP
Local time
Today, 01:53
Joined
Jan 14, 2017
Messages
18,261
You're welcome

To mark this as solved, click Thread Tools in the blue bar at the top then you'll see the option you need to click
 

Users who are viewing this thread

Top Bottom