autopopulating text boxes from combo boxes - ms-access

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 )

Related

Populate a ComboBox with the Result of a Text Box

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

MS Access - Identify a specific textbox from the result of a combobox selection

I have a combobox on a form for selecting a specific shelf location. I also have a number of textboxes imitating a shelf location. I would like to use the result of my combo box selection to change the color of a matching textbox label, for example combobox selection is F14. Textbox label name is LabelF14. How do I code that for event afterupdate to select LabelF14 from the combobox result, F14?
Build control name by concatenating literal text and variable, like:
Me("Label" & Me.comboboxname)
If need to set multiple controls' BackColor at same time, can use a For Next loop. If there are say 15 controls with similar names like LabelF1, LabelF2, ... LabelF15:
For x = 1 to 15
Me("LabelF" & x).BackColor = IIf(Me("LabelF" & x).Name = "Label" & Me.comboboxname, vbGreen, vbWhite)
Next
Make sure BackStyle property is set to Normal, not Transparent.
Keep in mind that programmatically setting properties of a control will affect all instances of control.
If you colored textboxes instead labels, could probably use Conditional Formatting instead of VBA.

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.

How to connext a combo box with txt boxes?

I have a table named "monades" with three fields
1.monada
2.nomos
3.perioxi
My entry in the 1st field (monada) has a unique entry in the 2nd field(nomos) and in the 3rd (perioxi).
I create a form with a combo box for the field "monada" and two text boxes,one for each other field.
I would like to update the 2 text boxes each time i change the value of my combo box.
For example,if i choose "1o MAL" in the combo box,i would like to get "crete" in nomos txt box and "chania" in perioxi txt box.
In order to get this,i have added in the BeforeUpdate method of each txt box the line
=[monades]![nomos} and = [monades]![perioxi].
My form is connected with the table "monades"
How could i fix it and get my job done??I have wasted many hours to get it work..:(
UPDATE:
Something like this doesn't works too
Private Sub monada_AfterUpdate()
If Not IsNull(Me!monada) Then
Me!nomos.Value = Me!monada.Column(1)
Me!perioxi.Value = Me!monada.Column(2)
Me.Requery
End If
End Sub
My fault is that i had to set "Column Count=3".
If i fix that,and i paste the Update code from above everything works fine!!
Thanks for your time mates!!:)

combobox cascade effect not working for different rows

I have two comboboxes on a subform.The first combobox is used to populate the second combobox. These are placed in the detail section of the form. I want them to work this way:when I select any value from the first combobox, I want the second combobox of the same row to get populated by relevant value.
As of now, I have tried to implement this and as I select any value from the first combobox of row 1 I see the second combobox of the same row gets populated but as I go on selecting values from the first set of comboboxes I see that the values in the second set of the comboboxes above changing or becoming null.
Here's the code:
The 1st combobox is cboRCMTask:
Private Sub cboRCMTask_AfterUpdate()
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
Me.cboRCMTaskOptions.Requery
End Sub
cboRCMTaskOptions is the second combobox.
The form_current event:
Private Sub Form_Current()
Me.cboRCMTask.RowSource = "SELECT ID, RCMTask FROM tblRCMTask;"
If IsNull(txtRCM_ID) Then
Me.cboRCMTask = Me.cboRCMTask.ItemData(0)
End If
Me.cboRCMTaskOptions.RowSource = "SELECT ID, RCMTaskOptions FROM tblRCMTaskOptions WHERE RCM_ID=" & Me.cboRCMTask.Column(0) & ";"
If IsNull(txtRCMOption_ID) Then
Me.cboRCMTaskOptions = Me.cboRCMTaskOptions.ItemData(0)
End If
End Sub
From your description, you are using a continuous form. While it looks like a continuous form has many rows, from the point of view of coding you can consider it to have just one row, the current row. I suspect that the control source for combo 2 is a hidden, numeric column in the combo, when you change the row source for the combo, the visible row can no longer be found, so it cannot be displayed. You will either have to provide a pop-up form for editing, or a textbox to store the value for the form and a slightly dodgy combo to edit that value. You can do a little to improve the appearance with conditional formatting.
in your first piece of code, shift the code to the on_click event. I am not sure what you are trying to achieve with the reference to ItemData but I think it is unnecessary, comment out that line.
Similarly the third to last line in the Form_current event, replace with a requery.