username() query

gmazza76

Well-known Member
Joined
Mar 19, 2011
Messages
767
Office Version
  1. 365
Platform
  1. Windows
Good afternoon

I have a userform that I have managed to get to show a persons ID when they open a userform, but is there anyway I can get the userform to show the persons name as well?
I have a table that has the agentID, AgentName and Department, but how do I get this to load in the "Form_Open" below

The source table has the above headings and I would need the AgentName to show in Text23

Code:
Private Sub Form_Open(Cancel As Integer)
Dim Username As String
Username = Environ("USERNAME")
tb_racf.Value = Username

thanks in advance
Gavin
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Use DlookUp()
Try and give your controls meaningful names. Text23 is not going to mean anything to anyone 6 months down the line. :(
 
Upvote 0
If persons ID and AgentID are the same thing then sure. If not, I see nothing that relates the field names to what's being asked for. Maybe the DLookup is on the Username value?
Probably better to use Load event when populating calculated controls.
 
Upvote 0
thanks @welshgasman,

I am going to rename the "Text23" to a meaning, but would I use the DLookup in the "Control Source" or in the userform open in VBA?

thanks
 
Upvote 0
You could do either TBH.
It is a matter of preference. If you do put it as control source, then you would not be able to modify that control (should you ever want to)

I would probably set it in the Form Load event, as that is the normal place controls can first be seen.
 
Upvote 0
thanks @welshgasman

I have tried =DLookUp("[Sort_Name]","DataAll","[AID] = " & [Me].[tb_racf]) in the source control and get "#NAME ?"

and

Code:
Dim strX As String

strX = DLookup("[Sort_Name]", "DataAll", "[AID] = " & Me.tb_racf)
Me.AgentName = strX

but I get an error "Run-time error '2471'- The expression you entered as a query parameter produced this error" 'mazzag' which is my AID

Any suggestions as I am unsure what I have done wrong as the table I am looking at is "DataAll" and the column to return the value from is "[Sort_Name]"

thanks
 
Upvote 0
I tend to put the criteria into a string variable for domain functions if not a single simple criteria.
Then I can debug.print them to see if I have the syntax correct.
Then when correct, I can use them in the function.
Plus, if you cannot see the error, you can copy and paste here or eslewhere, so someone can point out the error.?

Strings need to be surrounded by single quotes ' unless it contains a single quote, then triple double quotes works, I think?
Date literals with # and in mm/dd/yyyy or yyyy-mm-dd format
Numbers do not need anything.
 
Upvote 0

Forum statistics

Threads
1,215,039
Messages
6,122,799
Members
449,095
Latest member
m_smith_solihull

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
Back
Top