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

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

Related

Is there a way to update a TextBox when is not visible?

I have a TextBox nested inside a TabControl.
Form->TabControl1->TabPage1->TabControl2->TabpPage2->GroupBox->TextBox
When the TabPage1, TabPage2 is selected, so the TextBox is visible to the user all the TextBoxEvents works OK, but when the user selects another TabPage it doesn't work.
I have a timer that send data periodically to know if an external device is present on a specific virtual COM port.
When the external device answer I put that data in that TextBox and set a global flag(boolean) to let the rest of the program that a device is present.
I'm processing the received data on a Private Sub and changing that TextBox with a Lambda expression like this
Me.Invoke(Sub()
Me.VersionFirmwareTxt.Text = RespX.Substring(5)
End Sub)
You can access the TextBox by name from anywhere whether it is visible or not, via casting (CType), and update its text value using the syntax:
'(from within the same Form, e.g., Form1)
CType(TabControl1.TabPages(1).Controls("VersionFirmwareTxt"), TextBox).Text = RespX.Substring(5)
'(from a different Form)
CType(Form1.TabControl1.TabPages(1).Controls("VersionFirmwareTxt"), TextBox).Text = RespX.Substring(5)
If you dynamically added the TextBox to the controls of the parent (TabPage), you will know exactly where the TextBox is located, since you would have already used, e.g.:
TabControl1.TabPages(1).Controls.Add(VersionFirmwareTxt)
Whereas if you manually added the TextBox to the TabPage, you also know the parent control.

Getting a Value if a Field and Record number is known?

Good Evening,
I am working on a Combo Search Form that is designed to search for information by criteria. The form has a combo box containing field values and a text box beside it. The selection of a field value in the combo box will fill in the text box beside it with the relevant information for that record, all the relevant information is contained in the PetTable.
I have managed to get the combo box to display the fields from the PetTable by setting the rowSource to PetTable and the sourceType to Field List... however that's where I hit my dead end.
In the Text Box beside the combo-box I tried grabbing the value of the combo box and putting it into the textbox by making the Text box control source "=ComboBox", however this just created a textbox which has a literal text string to that of the combo box.
My next thought was to make the text box Control source "=PetTable.PetComboBox" my thought was that the PetTable references the table with my information and the "PetComboBox" becomes the field a need to get. This did not work either and gave a #Name error"
What should be happening is: In the Combo-box if I selected [Pet Name], I would hope that the textbox beside it becomes "Fido" but instead it also becomes [Pet Name].
Any and all help would be appreciated!
Thanks
Desired Effect
What you need to do is to change the Row Source Type of the Combo Box to "Table/Query". Then in the "Row Source" click on the "..." to open up the Query Builder. Select the table that you want. Add the columns that you want. I would suggest the table's primary key PetID, and then any other fields - in your case at least PetName. You may also want to sort by PetName to make it easier for the user to scroll through. Close the Query Builder and save the changes. Change the combo box's ColumnCount to 2, and set the Column Widths to be "0cm;6cm" (setting the first column to have a width of 0 means that it is not displayed to the user).
Now move to you TextBox, and set the Control Source to be:
=[Combo0].Column(1)
Note that columns in a combox box are 0-indexed, so the first column is column 0, the second (in your case containing PetName) is column 1.
As you actually want to show the field names, rather than the data in the combo box, then you will need to set the RowSourceType to be "Field List", and then select the table name as the RowSource.
You will then need a small piece of VBA to lookup the value of that field in the table for the current record:
Sub sListFieldData()
If Not IsNull(Me!Combo0) Then
Me!Text2 = DLookup(Me!Combo0, "tblPet", "PetID=" & Me!PetID)
Else
Me!Text2 = ""
End If
End Sub
And you will then need to call this procedure in the combo box's AfterUpdate event (to catch when it has been changed by the user) and also in the form's Current event (to catch when the user moves between records):
Private Sub Combo0_AfterUpdate()
Call sListFieldData
End Sub
Private Sub Form_Current()
Call sListFieldData
End Sub
Regards,

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

MS Access: How to bound text box of form with query?

I created a form in MS Access 2010 and added a textbox here. Then I created a simple query (for example SELECT 10 AS studval;) and tried to set in Properties (of textbox) -> Data -> Control Source this query, but I got error #Name?.
How do I fix this error?
All names of query, textbox, query return values are correct. Or maybe are there any other ways to bound textbox and custom SQL query?
There is no easy way to do it, but it is possible using the form's On Activate event. First set up a query (Query1) with a single value called "studval" then open the form properties and add an Event Procedure for On Activate. It should look like this:
Private Sub Form_Activate()
Dim myString As String
myString = CurrentDb.QueryDefs("Query1").OpenRecordset.Fields("studval")
Me.Text0.SetFocus
[Text0].Text = myString
End Sub
You need to set the Control Source of the form to the query rather than the control source of the text box. A text box control source can only refer back to it's form's control source.
If you want just one text box bound to a query you have to create a subform linked to the parent form with that text box in it.

how to clear all tools in the vb.net from

i have used many of the controls in vb.net forms, including a textbox and combobox, but I want to clear all my text boxes at once, while not typing textbox1.clear() for each one.
Is there any other way to clear all my textboxes?
If I understand you question right, you should be able to loop through all the controls on your form and check to see what Control type they are. Based on their type, either set the textbox Text property to String.Empty, or your ComboBox to the index of a blank ListItem (presumably item zero).
Something like:
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is TextBox Then
CType(ctrl, TextBox).Text = String.Empty
Else
' do something similar for your ComboBox
End If
Next
You can parse through each control on your form and test what type of control that is and handle each type separately, but it is easier to simply set each control manually.
Consider writing a ClearForm routine that does all of this, that way all you have to do is call the method.