Populate textbox data to other forms' textbox - vba

I have a ‘Login’ Form that opens when accessing database. It includes a textbox 'txtLoginID' bound to LoginID field in Employee table which has another field name called Fullname. On clicking OK button, a main form is opened. So far it’s OK.
I am trying to populate the textbox data to other form or subform's textbox to show the Fullname of the employee. I’ve put that other form textbox
controlsource=ReturnUser() ReturnUser is is public Function that is coded like that:
Public Function ReturnUser() As String
ReturnUser = DLookup("Fullname", "Employees", "loginID=" & Forms!Login!txtLoginID)
End Function
Still can’t get txtLoginId populated to other form’s textbox. I appreciate help

a textbox 'txtLoginID' bound to LoginID field in Employee table
That won't work. Make the textbox unbound and call your function in the AfterUpdate event of the textbox.

Related

How can I get the name of the Textbox in which a function is being run in MS Access form?

I am trying to get create a function (or some expression) that can be run from any textbox on a Form, which will return the Name of the textbox as the Value of that textbox, i.e. each box will display it's name in the box itself.
I'm looking for something like Me.ActiveControl.Name, but that only returns the same value for each textbox. Is it possible to be self-referential like that? I haven't been able to find anything that does that.
Cell? self-referential?
The answer is Me.ActiveControl.Name which does return the name of the active control (textbox).
That name doesn't change from record to record. If you wish to identify individual records, use the ID of the record or the CurrentRecord property of the form.
You can make a control the ActiveControl of the form with:
Me!TheControlName.SetFocus
or:
Me.Controls("TheControlName").SetFocus
or loop the Controls collection of the form to list the textboxes:
Dim Control As Control
For Each Control in Me.Controls
If Control.ControlType = acTextBox Then
Debug.Print Control.Name
End If
Next

AfterUpdate On ComboBox: Display selection without requiring other action

I have a form with a combo box that has approval codes in it.
I created an AfterUpdate event so that when the user selects the approval code today's date will be placed in another field.
The problem is when I tab out of the combo box the date doesn't display in the field unless I click in that field or save the record.
I know I can do a Me.Refresh after the code, but I don't want to save the record until the user is done with everything they need to input.
Private Sub AppArch_AfterUpdate()
Me.DATE_RCVD_ARCH = Date
End Sub
Must reference textbox name so if field and textbox have same name, then your existing code should work and new value display immediately. Apparently that is not the case. I always name controls different from field, like tbxDateRA. Then code can be:
Me.tbxDateRA = Date
or
Me!tbxDateRA = Date
but use the first to trigger intellisense tips.

Setting ComboBox RowSource property to query in "GotFocus()" method leaves blank values in ComboBox Access VBA

I want to populate my ComboBox in an Access form using VBA based on data from another table. Previously to do this I did the following:
In the Property Sheet -> Data tab I filled out Row Source and Row Source Type fields with this information:
Now whenever I clicked on the dropdown for my combobox, it would populate the dropdown list with all of the names from t_people table.
This limited me however to when data changed in the t_people's name column. In order to get an updated list, I must close the form and re-open it so that the query runs again. I have limited the access to this Access file so that the user is only presented with x number of forms, and cannot close/open them or others.
My solution is to remove the query on the form load, and instead run the query every time the combobox gains focus, has a click event or something of the same sorts. I did this with the following event in VBA:
'Run when the "name" combobox gains focus
Private Sub nameCb_GotFocus()
[nameCb].RowSource = "SELECT name FROM t_people"
End Sub
I have set breakpoints and this code does run. However, the the combobox is not populated after it does. How can I get the combobox to populate with the values from a query for each time the combobox gains focus?
Set the RowSource in design and add a .Requery when entering the control.
Private Sub nameCb_Enter()
nameCb.Requery
End Sub

Passing TextBox value between two UserForms

I have two user forms in Excel VBA. I enter a value in the TextBox at the first UserForm. How do I pass the value to the second UserForm to display same value in a TextBox too?
Assuming that your forms are named:
Userform1 for this first form
Userform2 for the second form
And assuming that you have two textboxes one in each form as follow:
TextBox1 for the first textbox located in your first form
TextBox2 for the second textbox located in the second form
You assign the value of the first TextBox1 to TextBox2 using Userform2.TextBox2.Text=Userform1.TextBox1.Text
You can declare a public variable:
i.e X. In user form 1 x=textbox1.value and in user form2 textbox2.value=X.

How we can grab value from list box and fills automatically related text box in new form

I have a list box and its row source is "tblitems" with bellow fields.
Now I like when right click on this list box and select one option for example "new task" it opens new task form and automatically Grab the item number from a list box and fills related text box "item number" in "new task" form that is bounded to table "tbltask"
Now when I press "Apply" button it insert new record in "tbltask".
tblItems
item number (pk)
item name (text)
tbltask
task number (auto number,pk)
item number
enter image description here
Getting a value from a list box in a form is:
Me.List_Box_Name.Value
Or if you have unbound values then get it based on the column:
Me.List_Box_Name.Column(2)
or whichever column you need.
You can then populate fields with DLookup or a recordset. Then if you want this to happen when you open a new form, you may want to look into this:
Private Sub Form_Load()
'Stuff you want to happen when that form loads
End Sub
Updated
The following will print out the value each time you click it. You can use this method to trigger a new form to open, or you can have a user click a submit button afterwards.
Private Sub Test_List_Click()
Debug.Print Me.Test_List.Value
DoCmd.OpenForm "Form_Name"
End Sub
Now I'm not exactly sure the best way to add your variable to the opened form. If it were me with my limited knowledge, I would add a global or public string and have the Form_Load() check to see if that string length is greater than 0. If yes then it will populate the field.
Hope that helps
Update 2
Actually this link will help you populate a field from previous form:
MS Access - open a form taking a field value from a previous form