msuserbd
Aug 28, 2009, 10:38 AM
I'm having trouble figuring out how to get the values from a list box into other fields on the form. I would appreciate any help you can give me.
Here is an example of my form in the attachment.
Thanks,
msuserbd
ScottGem
Aug 28, 2009, 11:41 AM
Um no attachment.
Are you using a List box or a combobox? Is the Multi-select property of the listbox turned on? Are these values from another table that you just want to display on your form?
msuserbd
Aug 28, 2009, 11:51 AM
Thanks for your quick response.
These values do come from a file and I want them on the form so the user is sure they selected the correct one and then I will be writing them to another file.
It is a combo box not a list .
ScottGem
Aug 28, 2009, 12:00 PM
Ok, first lets get some terms straight. Its hard to help someone if we aren't speaking the same language. A list box is different from a combo that's why you needed to identify the correct control you are using. Also, a file in Access terms is a container for several Access objects. I suspect you mean a table and not a file. A table is an Access object that stores your data.
So you do NOT want to write the same data to another table. This defeats the relational aspects of Access and violates normalization rules.
I wrote an article on displaying data from another table on a form. I'll paste in here as I think it will help you with your situation.
Displaying Data from related tables on a form
This is a very frequently discussed issue. But before I get into the methods, you need to understand one of the principles of relational databases. That principle is that data should exist in one place only. Having the same data in multiple tables is a violation of normalization. Related records are indicated by a Foreign Key within the record that holds the Primary Key value of the parent record. So when you want to have data from multiple tables on a data entry form, you set it up to display the data not store it in the form's Recordsource
There are basically four ways to display related data on a form; Subforms, The Column property, DLookups and Listboxes. Ill discuss each in turn and suggest where to use each.
1) Subforms. You can use a subform to display several fields from the related table. Create the form using the Subform wizard or create a separate form and place it on the main form as a subform (I generally create a separate form). Using the wizard, you go through the following steps.
• Select whether to use an existing form or create a new one
• If you are creating select the table and fields to use
• Select the linking fields, usually accept the defaults Access proposes
You can customize the subform, so it looks like part of the main form, by removing record selectors, navigation buttons, borders etc. I use subforms when I want to display 4 or more fields from the related record. Another advantage of using subforms is where you have a One to Many relation. Using a Continuous Form or Datasheet view, you can display multiple related records at once.
2) Column Property. Generally Foreign Keys are entered by selecting the related value from a combobox. The combobox uses a query as it Rowsource. This query displays the records from that parent table. At the least, the query includes the primary key field as its bound column and a description field. However, you can add as many other fields from the table as you want. These fields can then be reference using the Column property. Click the Build button […] next to the Rowsource property to enter Query Design Mode. In Query Design Mode you can add tables and fields to the query. You can control what fields actually display in the pull down list by setting their Column Width. Setting the width of an individual column to 0" will hide that column (Note: Column widths are entered separated by a ; for each column listed in the column Count). The combobox will only display the first non zero length column after selection. The following properties of a combo are key to using combos in this way: RowSource (the list), Bound column (what's actual stored), Column Count (how many columns in the list, Column Widths (the size of each column in the list).
You can then set the ControlSource for an unbound control to:
[comboxname].Column(x)
Where comboxname is the name of the control and x is the number of the column in the query for that field. Note: the column count starts with 0 so the 3rd column is 2.
Since the combobox selects a single record, the Column property will also reflect a single record. I use this method if I need to display 3 or less values from the related record.
3) DLookups. DLookups allow you pull a value from a field in a specific record. It uses the syntax: DLookup([fieldname]”,”table/queryname”,”Criteria”). The Criteria is used to specify the record you want to return. Since the Comboxname will store the FK value you would use a criteria like: “[keyfield] = “ & [Comboboxname]. This would also be used as the controlsource of an unbound control. Each DLookup should only be returning data from a single record. If its possible that the DLookup might not find a matching record you should use it within a NZ (NullZero) function to prevent errors. I use DLookups when I need to pull data from different tables based on a key value.
4) Listboxes. A Listbox can have multiple columns with column headers. It also can be set to display multiple matching records. I will, sometimes, use a Listbox in place of a continuous form or datasheet subform. Listboxes will also display multiple matching records.
There are two exceptions to the rule of not repeating data in multiple tables. The first is the PK value. Obviously, that value has to be repeated as the FK to relate the records to each other. The other exception is time sensitive data. Sometimes you need to freeze data that will change over time. The best example of this is price data. For example: In an order entry application, you want to freeze the price at the time of the order. In such a case, you would have the Price field repeated in the OrderDetails table. Generally you would use the Column property for this and populate the control in the After Update event of the Products combo use code like:
Me.txtPrice = Me.cboProduct.Column(2)
These guidelines should help you build forms that preserve normalization and are well organized and easy for the user to use.
msuserbd
Aug 31, 2009, 11:37 AM
Thank you for the information It worked great.