Populate a ComboBox with the Result of a Text Box - ms-access

I have a form in my Access database called:
formAcademyRegister
Inside this form I have a combo box called:
EmployeeCode
This combo box retrieves its data from a table called:
Employees
The primary key on this field is EmployeeID
To make it easier for the user I created a form called formsEmployeeSearch. In this form use some text fields to make it easier to search for the employee using a first name or last name, returing the values to a list box on this form. This list box has three fields:
EmployeeID
FirstName
LastName
All these fields are also populated using the Employees table so there won't be any data type issues.
Below the list box on the search form I have a button called
butUpdateEmployeeID
When the button is clicked, I want the following to happen:
The EmployeeCode combo box in the formAcademyRegister be updated with the employeeID as selected in the list in the formsEmployeeSearch form
The formsEmployeeSearch to be closed
I am comfortable with the process of closing the form, I am however struggling with the code to populate that combo box with the value selected in the list box.
I tried the following:
Me.Form!formAcademyRegister!EmployeeCode = Me!lstEmpSearchResult.Value
But this is not working.

Try the below.
Forms.formAcademyRegister.EmployeeCode.Value = lstEmpSearchResult.Value
DoCmd.Close acForm, Me.Name, acSavePrompt
A couple of notes.
The keyword Me is not required when the code is behind the form itself, but in some situations it makes the code more readable.
The .Value property is default property (where applicable) thus can be omitted. It's best practice to keep it for clarity.

Me.Form is reference to current Form (where code is).
Forms is collection. Assignement to collection item "Collection(ItemNameOrItemIndex)
Forms("formAcademyRegister").cmbEmployeeCode.Value = Me.lstEmpSearchResult

Related

How to filter subform using listbox

Good day,
I have one form call Check Router. In that subform I have one text box (TxtPartNo), one list box (LstPartNo) and one subform (CheckRouteSubform).
These are the roles of those function
TxtPartNo --> User will key in the Item Description that they wanted to check its Route.After that,they will click add button.
LstPartNo --> its row source type is value list. After the user click Add Button, the Item Description that they key in will insert in this list box. Then they will select all data list and click on check button.
CheckRouteSubform --> This subform is based on CheckRouteQuery. After check button is click, I wanted it to be filter based on that Item Description. If it contain but not exactly equal that part_No, it will show in this subform.
example : user key in = FCLF-8531
subform will show data that contain FCLF-8531-RX, FLCF-8531-AX.
here I attached the picture of the form
Below are the picture of query :
As you can see i create criteria of Item Description based on the listbox.
and in check button I used this code:
Private Sub CmdCheckRoute_Click()
Dim i As Integer
For i = 0 To Forms!CheckRoute!LstPartNo.ListCount - 1
Forms!CheckRoute!LstPartNo.Selected(i) = True
Next i
DoCmd.RunSQL "CheckRouteQuery"
End Sub
However, it does not want to show what I need. :( Help me please.

Clear out dropdown value once another drodown value is changed in vba access?

I have three combo box controls(MFG, Code, and GrpID) in my VBA access form. Once the user selects an option from the first combo box (MFG), rest of combo boxes give me available options. But I need to do some validation i.e. what if the user decided to change the value of first combo box? The values of rest combo box should be cleared. All I need to do is once the first combo box is changed the second and third combo box need to be cleared out or at least set to focus on them so that users will realize that they can't use old values as first value is cleared in the first combo box. I added a code block 'AfterUpdate for first combo box as shown below:
Private Sub MFG_AfterUpdate()
Code.Value = " "
GrpID.Value = 0
End Sub
The problem after writing above code is: they don't get empty until they(Code and GrpID) get clicked. In other words, I need to click them to empty them every time I change the value of MFG. Can anyone direct me how do I clear them or at least focus them?
Set the combo to null to wipe any displayed value in them;
Me.Code = Null
Me.GrpID = Null
This assumes your combo controls are called the same as your field names.
Edit for clarity: Including the Me. makes sure your are changing the form control values. If your field names are the same as the controls then Access will change the underlying field values, and as you have discovered on your form these aren't reflected until you click in that fields bound control.

Populate textboxes with value from listbox selection on child form

I realise this question has been asked before and I have tried numerous suggestions, but I guess I am just too much of a novice when it comes to VBA coding.
Here is my scenario:
I have an Access 2007 "Application" with a few forms. On the main form, I have 2 text boxes. For simplicity let's call them textbox1 and textbox2.
textbox1 is used to enter a style code. a button on the main form then opens another in modal/dialog mode and runs a query with the style code as the where clasue. the modal popup gives me a list box which is populated from the database based on the query that was passed. I then need to select one of the products in the list and upon closing the popup, populate textbox2 with the brand (column2 in the list from the popup) number.
Please remember that I am a novice. Any help would be greatly appreciated.
I would put a button on the second form, and in the _Click() function, put something like this
If ListBox1.ListIndex = -1 Then
MsgBox "Nothing Selected!"
Exit Sub
End If
UserForm1.TextBox1.Text = ListBox1.List(ListBox1.ListIndex)
Unload Me
Where ListBox1 is the list box containing the content you need the user to select from, where UserForm1 is the name of the calling form, and TextBox1 is the name.
Explanation
The ListIndex property of the list box returns the index of the selected list item. If nothing is selected, it returns a -1.
To reference an item on one from from another, you reference the form, the object and the property.
In The example I gave, the form is UserForm1, the object is TextBox1 and the Property is Text. While typing, intellisense should auto complete the object and properties once you type the period.

Opening a combo box from another object

I would like to know if it is possible to open a combo box and see it's elements listed from another object on the form. I do not like the 'down arrow' on the combo box and would like to create something more 'stylish' if it is possible. I am wondering if there is a routine one could write and put on another object so that upon the 'on click' event, the user will see all the elements listed within the combo box and have the opportunity to select one of them.
For that you can use the Dropdown method of Combobox object.
Example:
Dim cmb1 As ComboBox: Set cmb1 = Me.Combo1
cmb1.SetFocus ' necessary
cmb1.Dropdown
Note: The control needs to have focus so you have to set it programmatically first.

autopopulating text boxes from combo boxes

I have created a form to input new work orders in access. I want the text boxes in my form to populate from the selection in the combo box.
The combo box lists serial number for equipment, and once a serial number is selected, I want CompanyName, PurchDate, LastMaintenanceDate, & ModelNo to populate.
I saw an example of dlookup and tried this expression on the Company Name textbox:
=DLookUp("[CompanyName]","SerialNoInfo","[SerialNoInfo].SerialNo= " & Str([Combo341].[Text]))
I keep getting an error #Type or #Error in the box depending on what I do. When I try to build a code for the event on update, nothing happens at all. I have no idea what I am doing wrong.
You can alternatively use :
Private Sub COMBOBOX_Change()
me.TEXTBOXNAME.Text = me.COMBOBOX.Column(X)
End Sub
Where TEXTBOXNAME is the the textbox you want to fill
Where COMBOBOXis the the combobox from which you want to get data
and X is the number of column with data (starting from 0 )