Vba to make the textbox show record value

pedie

Well-known Member
Joined
Apr 28, 2010
Messages
3,875
Hi, i want to know how to make Vba show record value in textbox1 in myform1.

when the form load, if the environ username is pedie1 then i want "pedie1" table's record1 of field1 show in textbox1.

Please please help me out.

Thanks in advance.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Is Pedie1 a value in a field also?

I think you could add your vba to the Form's Initialize Event to alter the form's record source dynamically:

Code:
Dim s As String
s = Environ("UserName")
s = "SELECT X FROM Y WHERE Z = '" & s & "';"
Me.RecordSource = s
 
Upvote 0
pedie

Do you have a table for each user?
 
Upvote 0
Correction - access forms have no intialize event. I mean the Load event.
 
Upvote 0
Xen, thanks for the code: I tried it this way and it errors so i excluded underlined line and now it does not error but it does not show anything as textbox1.value
'm i suppose to change anything in the code?:)
Code:
[/FONT]
[FONT=Courier New]Private Sub Form_Load()
Dim s As String
s = Environ("UserName")
s = "SELECT X FROM Y WHERE Z = '" & s & "';"
[U]'Me.RecordSource = s[/U]
End Sub

Norie, yes i do have table for each user.
Thanks again.
 
Upvote 0
I also did try this way...but i have no idea how to make this things work...
I have reviewed so many web pages but to no luck...
Code:
[FONT=Courier New]dim x as string[/FONT]
[FONT=Courier New]x = environ("Username")[/FONT]
 
[FONT=Courier New]Set rs = CurrentDb.OpenRecordset("SELECT " & x & ".Name FROM & " x & ";")[/FONT]
[FONT=Courier New]Me.Text2.Value = x!rs(0).Value[/FONT]
 
Upvote 0
Code:
s = "SELECT X FROM Y WHERE Z = '" & s & "';"

What's the correct SQL to get the data you need? Probably something in the sql query is wrong. Of course your form also needs to have controls that match the fields returned.
 
Upvote 0
Please tell me what should i exactly do...:)
my form name is "Myform1"
it want table "Pedie", 1st column that is "Names" 1st row value in Form textbox named "Textbox2".

I thought it should be simple like...
me.textbox2.value = Pedie!names(1) something like that:)

Thanks again...Please advice.
 
Upvote 0
Set the Record Source property of the form to pedie1.

You should now see a list of available fields.

Drag the field you want from that list onto the form.

That should create a textbox bound to that field.

When you open the form you should see the value of that field for the first record in the textbox.

That's it - no code needed.
 
Upvote 0
Norie, thanks, thanks alot..that was a learning...but what if i want textbox value to be "table2" name value when someone else opens the form then..
How do i take care of that

Also please show me the vba way too..
I tried this way and it show Myform1 in texbox value:)
Code:
[/FONT]
[FONT=Courier New]Private Sub Form_Load()
Dim s As String
s = Environ("UserName")
Me.RecordSource = s
Me.Text2.Value = Name
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,581
Messages
6,179,668
Members
452,936
Latest member
anamikabhargaw

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