Access 2016 issue - Can't get 2nd combo box to populate depending on value of 1st combo box - ms-access

I have an Access database with a form. The form has 2 combo boxes (Combo0 and Combo2). I'm trying to get Combo2 to populate based on the value in Combo0. I'm trying to do this with an AfterUpdate event.
When I try to select a value in Combo2, I get the following message:
The record source" SELECT........" specified on this form or report does not exist.
The name of the recordsource may be misspelled, the recordsource was deleted or renamed, or the recordsource exists in a different database.
In the Form or Report's Design view, or Layout view, display the property sheet by clicking the Properties button, and then set the RecordSource property to an existing table or query"
This seems like a simple thing to do but I'm obviously missing something here. Any suggestions would be GREATLY appreciated .....Thank you

I have a Combobox1 set for my Client Company and then a Combobox2 set for the Client Contact. My Combobox2 changes depending on which Client Company I select.
Combobox2 Row Source Type = Table/Query
Combobox2 Row Source = see below
SQL
SELECT [Clients - Contacts].ID, [Clients - Contacts].[Full Name], [Clients - Contacts].Company
FROM [Clients - Contacts]
WHERE ((([Clients - Contacts].Company)=[Forms]![dshbProjectDetails]![CompanyID]));
Picture: Query Builder
After Update Event on Combobox1 = Macro: Requery; Control Name Combobox2

Related

Setting properties for combobox in the form.open event Access 2019

I have the following piece of code located in the open event of a form.
If GetUserName = "Bob" Or GetUserName = "Ned" Then
Me.cbo_Position2.LimitToList = False
Me.cbo_Position2.AllowValueListEdits = True
Me.cbo_Position2.ListItemsEditForm = frm_MasterDropDown_lookup
End If
I'm using this code to set who can edit a combo box list. The combo box is bound to a field on the form and the combobox info is in a separate lookup table. What I want to know is whether or not these properties can be set as the form opens.
When I run this code, it executes with no errors but the properties do not change. I also tried doing 'Me.Refresh' at the end but that did not help. My assumption at this time is that they can't be "set on the fly" so to speak. If not, I was wondering if there is something I'm missing to get this to work.
TIA,
Tim
From Access help:
1.If you set the combo box's BoundColumn property to any column other than the first visible column (or if you set BoundColumn to 0), the LimitToList property is automatically set to Yes.
2.When the LimitToList property of a bound (bound control: A control used on a form, report, or data access page to display or modify data from a table, query, or SQL statement. The control's ControlSource property stores the field name to which the control is bound.) combo box is set to No, you can enter a value in the combo box that isn't included in the list. Microsoft Access stores the new value in the form's underlying table (table: A database object that stores data in records (rows) and fields (columns). The data is usually about a particular category of things, such as employees or orders.) or query (query: A question about the data stored in your tables, or a request to perform an action on the data. A query can bring together data from multiple tables to serve as the source of data for a form or report.) (in the field specified in the combo box's ControlSource property), not the table or query set for the combo box by the RowSource property. To have newly entered values appear in the combo box, you must add the new value to the table or query set in the RowSource property by using a macro or Visual Basic event procedure (event procedure: A procedure that is automatically executed in response to an event initiated by the user or program code, or that is triggered by the system.) that runs when the NotInList event occurs.
Example for setting property:
Forms("Order Entry").Controls("States").LimitToList = False

Using a calculated field value from a form text box in a query

I have a calculated text box on a form. How can I use that value in a query? The value is based on many other calculations done on the form as well.
I was hoping I wouldn't have to re-do all the calculations in the query.
Providing the form remains open when the query is evaluated, you can reference the value of any control on the form using the following syntax in your query:
Forms![Your Form Name]![Your Control Name]
In the case of referencing the value held by a control on a subform, consider that the subform is just another control on the parent form, and so the chain of references becomes:
Forms![Your Form Name]![SubForm Name].Form![SubForm Control Name]
You can test the value obtained by simply creating a new query in Access with the SQL code:
select Forms![Your Form Name]![Your Control Name] as FormValue
When run, this will yield a single record displaying whichever value was held by the control Your Control Name on the open form Your Form Name at the time of execution.

How to get a subform field value?

I have an Access form with (continuous) subform and one of the combobox fields on the subform is populated with data depending on the value of another field.
For this I use the following in the Data Row Source:
SELECT VendorName FROM VendorsPerAction WHERE (Action= Forms![LocalSubformActions]![fldAction]) UNION SELECT distinct null FROM VendorsPerAction ORDER BY VendorName;
This works fine when I test the form outside the mainform. But when I test this as part of the mainform Access keeps asking me for the parameter. I tried changing it into:
(Action= Forms![LocalRequest].[LocalSubformActions]![fldAction])
and many other variations but I keep getting the parameter question.
Is there anyone who knows what I should use? Thank you!
Probably
Forms![LocalRequest].[LocalSubformActions].Form![fldAction]
(assuming the subform control has the same name as the subform)
See Refer to Form and Subform properties and controls
--> Forms!Mainform!Subform1.Form!ControlName

Access 2010 - combobox - unbound form - displaying data from an existing record

The problem I am having is displaying the correct information in a combobox on an unbound form when a users views/edits data.
The table that populates the combobox:
tblLocation
idLocation
Location
Location Description
In the tblPerson table, there is a FK field called idLocation. I have a form that allows a user to pick a person from a listbox and displays the information in textboxes and comboboxes.
The combobox is setup with these items:
idLocation <--- column width set to 0
Location
The problem I am having is having the data show up correctly in the comobox when I view/edit a new person.
When a person is selected from a ListBox, the information from tblPerson should display in textboxes and comboboxes. The textboxes work just fine. However, I'm struggling with the comboboxes. Keep in mind all of the fields
My research finds only two methods on solving this problem:
DLOOKUP
Manual check and set
If I use the DLOOKUP method:
cmbLocation = (DLookup("Location", "tblLocation", "idLocation=" & .Fields("idLocation")))
The problem is that msgBox cmbLocation will display the text and not the FK. If the user tries to edit the data, but makes no changes, it will try to save the text and not the FK.
I found a manual way that does work, but I'm not sure it is the best approach:
For i = 0 To (cmbLocation.ListCount - 1)
If Val(cmbLocation.Column(0, i)) = Val(.Fields("idLocation").Value) Then
cmbLocation = cmbLocation.ItemData(i)
Exit For
End If
Next
Again, this works - but I have to think that I'm doing something wrong - probably something obvious.
Is there a better way to do this?
you can dynamically change which data is displayed in a combobox. in your scenario, i suggest you use the OnClick event of the listbox (once the person is selected). Add the following code:
YourComboBoxName.RowSource = "SELECT * FROM tblLocation WHERE idLocation=" & FK
If after clicking on a person, the data doesn't change in the combobox, you may need a Me.Refresh
Base the form on a query that left joins in the location column.
Then that field when bound to a text box will will display automatic and without any code. And better is if the actual location value changes, then no update code etc. is required. In fact this is the whole beauty of a relational database!

How to use a query as a source for a report in MS Access 2007?

I did the following in MS Access: I made a form which had a combo box and a button. You select an option from there and click on the button and it is supposed to open a report. Now, I wrote a query selecting a few fields from a table and in the where clause, gave the condition as where name=str(combo1.value) and the report source was specified as this query. Now, when I select the value and click on the button, it opens a blank report. How can I make it load only those particular values?
I am not saving the combo box value anywhere. It said that it would remember the value for later use. Am I doing the right thing by not saving it? What should I do to make this work? Please help me!
Edit: The combo box is using values from a column 1 in a table X. I've not bound the value to any field and am using the "Remember the value for later use" option provided. The combo box is essentially a list of hotels and the report is a list of people staying at the selected hotel. When I put the ID of the field (as defined in the X), it works. But the thing is, it should refer to the names of the hotels and not the ID, which I am supposed to enter in a popup that asks for it. What do I do?
Edit 2: The query is as follows:
SELECT Table_1.Hotel_Name, Table_2.Name_of_Delegate, Table_2.Address, Table_2.City, Table_2.Center, Table_2.Spouse_Present, Table_2.No_of_Children, Table_2.No_of_Guests, Table_2.No_of_Rooms
FROM Table_1 INNER JOIN Table_2 ON Table_1.ID=Table_2.Hotel_of_Residence
WHERE Table_1.Hotel_Name=FormName.Combo7.Text;
When I click on the button (which opens the report), it asks for the name of the hotel in a popup box. How can I avoid this? What I am doing wrong?
You can use a WhereCondition with the DoCmd.OpenReport Method as a "dynamic WHERE clause" for your report's record source. Use something like this in the click event of the command button which opens your report.
DoCmd.OpenReport "YourReport", , , "[name]=" & Me.combo1
Remove the WHERE clause you added, where name=str(combo1.value), from the report's query.
I surrounded name with square brackets because name is a reserved word. See Problem names and reserved words in Access
Edit: In a comment, you said this about the combo box:
"Row Source is SELECT [Table_Name].[ID], [Table_Name].[Name] FROM [Table_Name];. Bound Column is 1 (which I assume shows the names I wish to be displayed in the combobox.)"
When you refer to the value of a combo box, that value is the value of the "Bound Column". So in your case, the bound column is 1, which means the combo value will be [Table_Name].[ID]. However, you want to open your report based on the [Name] column of the combo. So change the bound column of the combo from 1 to 2.
To open a report using the value of your combobox, in your report query you need to do the following:
SELECT fields
FROM table
WHERE YourValue = [Form]![FormName]![ComboBox Value]
You have to specify the name of the Form, plus the value so the report query knows the value. Hope this helps.