Hi,

I have a login screen where user enters their name and password. The username and password are stored in access. Then when the user opens forms i need their username to appear in the form. Can somebody give me code for that please.

Thanks
Krs

Dani AI

Generated

Short answer: store the logged-in name somewhere all forms can read and set it as soon as the login succeeds. Below are three practical, commonly used ways (with pros/cons) and tiny examples you can paste into your project.

  • Use TempVars (recommended if you have Access 2007+). TempVars are global, available to forms, macros and queries, and survive while the current database is open.
  • Pass the name with OpenArgs when you open a form (best when you open forms directly from the login or main form).
  • Keep the name on a single always-open “global” form (or a module-level holder). This matches ’s idea but is less robust than TempVars because module variables can be reset by a compile/error.

Example — TempVars (set at login, read anywhere, clear on logout):

' After validating credentials
Dim sUser As String
sUser = Me.txtUserName.Value
On Error Resume Next
TempVars.Remove "CurrentUser"   ' remove old value if present
On Error GoTo 0
TempVars.Add "CurrentUser", sUser

' On any form (Form_Load)
Dim vUser As Variant
On Error Resume Next
vUser = TempVars("CurrentUser").Value
If Err.Number = 0 Then Me.lblUser.Caption = vUser
On Error GoTo 0

' On logout
On Error Resume Next
TempVars.Remove "CurrentUser"
On Error GoTo 0

Example — OpenArgs (pass when opening a form):

' From the login/main form
DoCmd.OpenForm "frmMain", , , , , , sUser

' In frmMain (Form_Open)
If Not IsNull(Me.OpenArgs) Then Me.lblUser.Caption = Me.OpenArgs

Notes and troubleshooting

  • ’s DB fetch is fine for validation; avoid hitting the DB again to display the name — read the saved value instead. ’s sample app shows a full flow; if attachments won’t open, ask for plain-text source.
  • TempVars require Access 2007+. If you must support older Access, use a hidden always-open form or a public module variable (but handle reinitialization after errors).
  • Security: never store plaintext passwords; protect SQL (use parameterized queries/DAO/ADODB parameters) and don’t pass sensitive secrets via OpenArgs.
  • If a form shows an empty name, ensure the login code runs and sets the TempVar (or passes OpenArgs) before other forms try to read it.

Recommended Answers

All 15 Replies

Hi Krs13,
See my Attached help. I think this is exactly what you want.
Hope this helps
Thanks

nice example. I have nothing to say after it.

Hi Krs13,
See my Attached help. I think this is exactly what you want.
Hope this helps
Thanks

Hi,

I couldnot open the attachment what you have given.. Can you please help.

Krs

1st download & unzip it. and then open it.

1st download & unzip it. and then open it.

Hi,

I did the same thing what you have told but when i unzip and open it opens microsoft picture manager and shows a icon, thats it.

Can you send the unzip file or zip file again.


Thanks
Krs

OK then I am sending it again. Try it once again. Here it does not allow me to attach frm, vbp, and mdl files. If this doesn't work then we will need to fine other alternative so that I can send.
Thanks

OK then I am sending it again. Try it once again. Here it does not allow me to attach frm, vbp, and mdl files. If this doesn't work then we will need to fine other alternative so that I can send.
Thanks

Sorry its not opening it for me..Is it not possible to write code in text file and send it.

Krs

it opens microsoft picture manager and shows a icon,

I have send private message for you any way try the following once.
1. Locate Logon.vbp file and
2. Instead of double click, right-click and select Open with
3. Select Visual Basic from the Select Options dialog box

I hope this will solve your problem
Thanks

Hi,

I have a login screen where user enters their name and password. The username and password are stored in access. Then when the user opens forms i need their username to appear in the form. Can somebody give me code for that please.

Thanks
Krs

I suggest you to use a MDI Form with a Status Bar, where you can make appear the Username, User number, Date, Time etc... inside the each panel.

Make use of a public sub as in (Depending on your database used, in this case MS Access -

Public Sub GetDbAll(conn As ADODB.Connection, strUsername As String)
Set conn = New ADODB.Connection

conn.Open "provider = microsoft.jet.oledb.4.0;persist security info=false;data source =" & strpath & ".mdb"  ' C:\MyDatabase.mdb" 'MyDatabase being your database name

conn.CursorLocation = adUseClient
conn.Open

Dim rsDb As ADODB.Recordset
Set rsDb = New ADODB.Recordset

rsDb.Open "SELECT * FROM MyLoginTable", conn, adOpenStatic, adLockOptimistic

strUserName = rsDb!MyUsername 'The field for your username
End Sub

'Under a form load event - 

Dim conn As ADODB.Connection
Set conn = New ADODB.Connection

GetDbAll conn, Text1

'This will set the text in Text1 to the username.

Make use of a public sub as in (Depending on your database used, in this case MS Access -

Public Sub GetDbAll(conn As ADODB.Connection, strUsername As String)
Set conn = New ADODB.Connection

conn.Open "provider = microsoft.jet.oledb.4.0;persist security info=false;data source =" & strpath & ".mdb"  ' C:\MyDatabase.mdb" 'MyDatabase being your database name

conn.CursorLocation = adUseClient
conn.Open

Dim rsDb As ADODB.Recordset
Set rsDb = New ADODB.Recordset

rsDb.Open "SELECT * FROM MyLoginTable", conn, adOpenStatic, adLockOptimistic

strUserName = rsDb!MyUsername 'The field for your username
End Sub

'Under a form load event - 

Dim conn As ADODB.Connection
Set conn = New ADODB.Connection

GetDbAll conn, Text1

'This will set the text in Text1 to the username.

AndreRet,

What you have posted is to get username from mdb(i.e) login page. But i want the username in other forms after logging in the main form.

Krs13,
I have sent you private message giving my email address. So mail me and in your reply I will send my previous attached help. or Just give me your email address.
Thanks

Since that your connecting to your database, if the log in is successful, I suggest to store the inputted username in a global variable,..declare the global variable in a BAS module..

Public curUsername as String

so that you can access anytime whose the user that is currently logged in...
then when the user logs out, clear the global variable's content like this:

curUsername = ""

Krs13, If you look at my code, the username can be called whenever you need it and in whichever form you need it -

'Under a form load event -

Dim conn As ADODB.Connection
Set conn = New ADODB.Connection

GetDbAll conn, Text1

'This will set the text in Text1 to the username.

The Text1 part is the holding string for the username, which you can write into a textbox (Text1) or call it to any other function you need it for.

Krs13,
I have sent you private message giving my email address. So mail me and in your reply I will send my previous attached help. or Just give me your email address.
Thanks

Kinwang,

Thanks i opened your attachement in another system and could see the code. Thanks for your help.

Krs

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.