How do I use ONE control source with MULTIPLE combo boxes from a USER FORM, in Access? - sql

I am currently developing A quote sheet for work, I have 21 Combo boxes that are all populated by the same row source. The problems that I am having are When all my combo boxes are set to the same control source all the list values in the combo box are filtered and set to one value. For example combo box 1 is selected and prep is selected now the other 20 combo boxes are set to prep, and I can't select the other options. Now if I have no control source for my combo boxes than previous example works. If I select combo box 1 and set it to prep then I can select combo box 2,3,4... and put any value I want for each one. I want to be able to have each combo box with its own value.
The reason I am wanting the combo boxes to have the same control source is so when a combo box has a selected value it will add a new record to my table. So if 10 combo boxes have values selected then there will be 10 new rows added to my table when I click my add record button.
I am thinking a maybe having a loop do the work, but am not to familiar with with using vba to run sql??

You can't use the same control sources to generate multiple records on the same form. They must be unbound. At least, I don't know how to make that work the way you have it set up, but can still help.
Delete the control sources but keep the row sources.
You will need a query to save the values to your table:
INSERT INTO MyTableName ([MyFieldName]) VALUES ('[ComboboxValue]');
Copy this code and paste in query builder. Overwrite "MyTableName" and "MyFieldName" with the appropriate object names for where you want a single combobox value to be saved. It only adds one row at a time. [ComboboxValue] will be the parameter we use to pass each combo value to the query to execute.
Name the query whatever you want and save it. I will use the placeholder "MyQuery," make sure you update it!
Create a command button on your form, and in its On Click event, paste in this VBA code (you will need to update the combobox references where I use Me!ComboboxName):
Dim qdf As DAO.QueryDef
Dim combo As String
Set qdf = CurrentDb.QueryDefs("MyQuery")
combo = Me!Combobox1Name
qdf!ComboboxValue = combo
qdf.Execute
combo = Me!Combobox2Name
qdf!ComboboxValue = combo
qdf.Execute
'Copy those three lines for each combobox, adjust your combobox references accordingly,
'or use a loop
'End your procedure like this:
Set qdf = Nothing
If you are using a multi-column combobox, you may need to adjust the reference from Me!Combobox1Name to Me!Combobox1Name.Column(1).

Related

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,

Access: Execute Code when a value in dropdown is selected

I have a subform, which consists of a table.
In this table, one columns values are a dropdown field (with 3 types to choose from: Var1, Var2, Var3).
If a user now selects a value from this dropdown I want to execute a specific VBA code in the background.
For my understanding, that means I have to set up an event like "run code module xy if dropdown value changes".
I am a beginner and have no idea how to implement that. Hopefully you are able to help.
Thanks so much in advance!
My Code:
Private Sub runMacro()
''MyCodeHere
End Sub
My form with the table as a subform. You can select 3 different values for "solution". If one is selected, I want to run VBA code:
You have to open the form, which contains the Combo Box (the Drop Down) control, in Design View.
Then select this Combo Box control and open the Property Sheet if not already shown (you can swap its visibility with the key F4).
In the Property Sheet select the tab named Event and click into the event After Update.
Using the ...-button you can select the Code Builder to open the VBE.
It already prepared the event handler for you, where you now can place your code.

sql show listbox values only when combobox is populated

Firstly, I'm pretty new to access so I apologise if this is basic but I can't find the right answer either.
I've had some help setting this up so there are parts that have been created that I don't fully comprehend.
I have created a form that is to be used for data entry which consists of the following fields:
TEAMS - Combobox the rowsource of which is a table TEAMS; CALLREASONS - Multi-Select Listbox the records of which currently exist in a table CALLREASONS; ACTIONS - Multi-Select Listbox the records of which currently exist in a table CALLACTIONS; SUBMIT - Button with VBA which sends selected data to a number of tables.
The database works fine and things a saving where they should be and in the format they should be, the problem I'm having is with the display of the form.
I would like for the form to display blank when opened and after each time a record is submitted. However, if I set the rowsource of the listboxes to the tables all options show when the form is loaded and after each submission the options and the previous selections show.
How can I make it so that the first listbox CALLREASONS only displays possible options once a selection has been made in the combobox TEAMS and the second listbox CALLACTIONS only displays possible options once at least one selection is made in the listbox CALLREASONS ?
Thanks in advance for any help.
You can either change the controls row sources on the Update or Enter events, or you can simply enable/disable them on the events.
Lets use 3 nested comboboxes for an example.
Combo1 Has rowsource set permanently as it is the top level.
Combo2 and Combo3 Have no rowsource set.
On the Enter event of Combo2, you can set the rowsource to
SELECT Pkey, DisplayText FROM tblOptions WHERE OptionGroupField = " & Forms!Form1!Combo1BoundField
On the Enter event of Combo3, you can set the rowsource to
SELECT Pkey, DisplayText FROM tblOptions WHERE OptionGroupField = " & Forms!Form1!Combo2BoundField
So on so forth.

MS Access: text box value show #Name?

I have created a form and set the form's RecordSource to a query. Below is my query:
SELECT GeneralT.*, SalaryT.[Status]
FROM GeneralT INNER JOIN SalaryT ON GeneralT.Deposits = SalaryT.Deposits;
In the form I have 4 textboxes. In the first 3 textboxes I show value from GeneralT table and in fourth textbox I show SalaryT.[Status] value. But this fourth value doesn't show in the textbox rather it show #Name?.
If I open the query in datasheet view I can see all the value properly. I don't understand what is wrong here. Please help me to show the result properly in the form.
Displaying #Name? for a field says that field has a control source that does not match with the query linked to the form. Some things to check:
Make sure Status is a selection from one of the pre-existing options in the Control Source drop down combo box. Click on the combo box to make sure.
Double check to make sure it is a "Text Box" and not a custom
control.
Make sure there isn't another text box named Status
Try chaning the control source to just Status instead of SalaryT.[Status]. If the field name does not conflict with the naming of a field in GeneralT, the selected SalaryT.[Status] will actually be displayed named Status.

Pass criteria from a combo box to another combo box in access 2010

I am trying restrict the records in combo box 2 (cboNames) based on the selection of combo box 1 (cboClass).
Combo box 1 and combo box 2 are both based on records in the same table, tblNames.
Combo box 1's row source is set too
SELECT [tblNames].[ChildID], [tblNames].[Class] FROM tblNames;
Combo box 2's row source is set too:
SELECT tblNames.ChildID, tblNames.[Full Name], tblNames.Class FROM tblNames WHERE (((tblNames.Class)=[Forms]![frmInsertNewRecord]![cboClass]));
I was under the impression this should work but it obviously inst. Combo box 2 is blank.
Can anybody identify what i am doing wrong?
Once the value of the first combo box changes, you need to refresh the recordset of the second one.
You need to either hit the refresh button (F5) or add the following code in the _AfterUpdate event of the 1st box:
Private Sub cboClass_AfterUpdate()
Me.cboName.Requery
End Sub