Ready to watch this entire course?
Become a member and get unlimited access to the entire skills library of over 4,900 courses, including more Business and personalized recommendations.Start Your Free Trial Now
- View Offline
- Naming conventions and best practices
- Working with joins
- Using comparison operators
- Defining criteria for select queries
- Creating parameter queries
- Creating calculated fields
- Working with dates and times
- Using the Expression Builder
- Creating conditional statements
- Making, deleting and appending records
- Building reports
Skill Level Intermediate
In the last movie we look at how we could take the value from a drop-down menu on a form and use it as a criteria and a query. In this movie, I want to take a quick look at how the combo box was created and how the state names are populated in the drop-down list. I've currently got that form frm_ EmployeeLookup opened. Let's go ahead and switch into Design view. We'll take a look at the Properties for this form by clicking on the object. If it is not open, you can open up the Property Sheet and then we'll switch to the Data tab. We can see that the Row Source for this particular object includes this long code.
Now I can see that the select statement here indicate that this is a query in SQL view. If I click on the Build button we'll open the same query in the Query Design window. Now this query is embedded in the form. You won't find it anywhere in the navigation pane. I can run the query to see the results that are being returned and we notice that we have a column for the abbreviation for the state and the state name. Let's take a look at this in Design view again. So the query that's populating the drop- down menu is using the table States. From that we've got a field for the abbreviation and the field for the state name.
We're also using a second field of the StateName to sort it ascending, but we're not showing it, so this is providing the alphabetical list. Now this brings up an interesting question. In the drop-down menu we only see the full state nam. We're not seeing the abbreviation and now that we're thinking about it, how do the full state name in the form match up to the abbreviated state name that the query returned? Let's run that query again so you can see what I'm saying. We'll close this query, we'll return to the Form view, and we will run our form. I'll go ahead and select Colorado.
We'll run our query and we'll see that Access is matching the full state Colorado to the query with the abbreviated state CO. So what's going on here? Both of these questions can be answered by taking a look at the default way that the Combo Box Wizard creates an embedded query. Let's go to the wizard and create another combo box to see what's going on. We'll go back to the form and switch into Design view. We'll take our combo box object, click on that once, and we'll set a second combo box anywhere in the form.
If you click once, the Combo Box Wizard will open. Now the first step here is to create the combo box and it'll get the values from another table or query. We'll go ahead and say Next. We'll select our States table here and say Next. Access will ask us what fields from the states contain the values you want to include in the box? Let select Abbreviation and StateName. We add that to the Selected Fields and say Next. You can go ahead and sort ascending by StateName to provide the alphabetical list and we'll say Next again.
Finally here's where the answer is. By default Access hides the key column. If you turn that off Access will display the key column. Let's go ahead and leave ours on. We'll go ahead and say Next. Go ahead and select StateName from the Available Fields to show in the combo box. And we'll say Next and we'll just accept the default name. Press Finish. Let's take a look at this combo box and see how it differs from the one we created previously. We will go into Form view, use the new combo box, and because we decided not to show the abbreviated field, we're getting both here in the drop-down menu.
I can select one and Access displays the first column only in the drop-down menu. So with the ID column hidden, which is Access' recommended option in the wizard, the state name is displayed while the value of the combo box remains linked to the hidden abbreviation. When the query runs it grabs the value of the combo box, not necessarily what's being displayed to the user. Now obviously this could be pretty confusing behavior, which is why I recommend building the embedded query manually as we'll see in Chapter 7. But now that we know that the abbreviation field is simply hidden, we can go back to our original dropdown menu and reveal it with a simple tweak to its properties.
We'll go back into Design view, we'll take a look at the properties for the first drop-down menu, and under the Format tab we'll look for this Column Widths section. We'll see that we have two values here. The first one is 0, the second one is 1. Let's change 0 to 0.5. Click off of it to save that value, switch back into Form view, and now we can see that in our dropdown menu we've revealed both the abbreviation and the full name for the state. So we can see that the value of the box, the abbreviated field, was always there.
It just wasn't displayed; it had a zero width. So that's how we managed to link the full name of the state to the abbreviation in the query. All of this is thanks to the default recommended behavior of the Combo Box Wizard.