Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a list box or combobox to select from a table

Status
Not open for further replies.

cassandra

Programmer
Oct 10, 2000
21
US
Could you please help me? I have a table with different paper types. I would like to set up a combo box or a list box for the user to select the paper type. The paper weight-coefficient and thickness-coefficient depends on the paper type. I designed my project to use text boxes for the user to input the length and width of the paper and they must select the paper type, however, depending on the type I want to use the weight-coefficient to calculate the weight of the paper.

Scenario:
Paper 1 weight = Length*Width*weight-coefficient

Any help is greatly appreciated.
Cassie [sig][/sig]
 
If you just want to use the table to fill a combo box, use the code below - substitute in the code table name, field name, combo name,....

Dim db AS Database
Dim rs AS RecordSet
Dim sSql as String

Set db = Workspaces(0).OpenDatabase(databasepath, False, False)
sSql = "SELECT DISTINCT fieldname FROM tablename ORDER BY fieldname"
Set rs = db.OpenRecordset(sSql, dbOpenSnapshot)
combobox.Clear
Do Until rs.EOF
combobox.AddItem rs("fieldname")
rs.MoveNext
Loop
rs.Close
Set db = Nothing

Simon [sig][/sig]
 
Simon
Thanks for your help, but I think I need to make myself clearer. I do not have want to use a database table, but two-dimensional array to look up the correct weight-coefficient. I added my list contents at design time because they will never change. My problem is that I can't figure out a way for the user to input the Length and width for paper 1 and select the paper type thus giving me the ability to use a weight-coefficient depending on the type selected type.

Example:
User enters data in text boxes: Length = 8.5; Width = 11;
User selects from list paper type = 18 bond.
I know the weight-coefficient for 18 bond is 0.0015

My first thought was to use an if then else, but I have 40 different paper types to consider. My brain has gone to sleep.
Thanks for any help.
Cassandra [sig][/sig]
 
If you hard coding the paper types into the list property of the combo box at design time, then also code the corresponding weight coefs into the ItemData property - be careful to get them in the same order.
Then when the user selects a paper type, instead of using the listindex property or text property of the combo box, use the ItemData property.

You can also set this at run time by adding the following line into the do loop:

cboBox.ItemData(cboBox.NewIndex) = rs("weightfield")

and changing the select statement to include the weightfield as well, if you want to read the data in at run time - always better than hard coding things.

Simon [sig][/sig]
 
Cassandra if I was you I would use a Select case based on the input from the user. as long as the input is constant, then the select case could apply a value to a variable which you could then use in your calculation.

example
dim weightCoefficient as float
Private Sub comboBox_Change(Index As Integer)

Select case index

Case 0 'user chosen 18 bond being index 0
weightCoefficient = 0.0015
Case 2 'user chosen another bond type
weightCoefficient = 0.0020
End Sub

Hope that's what you are looking for. It seems like an easier solution if you have constant choices.

Chris [sig][/sig]
 
Swilliams and Chrissine,
Thanks a lot guys. Betwen your responses I got a solution for my project.
Cassandra [sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top