|
-
Nov 5th, 2007, 01:15 PM
#1
Thread Starter
Hyperactive Member
dropdown question?
Hi,
I have a customer table that contains two fields customer ID, customer name, what i want to do is having a dropdown box for user to pick the customer name, but when save the data back to the database
I want to save the customer id value instead of the customer name.
Here is the code how I populate the dropdown from database when the form is loaded
p=0
Do Until customer_table.EOF()
Cbocompany.AddItem rs![customername], p
p = p + 1
rs.MoveNext
Loop
Could anyone help me how to do that in dropdown box.
Thanks
-
Nov 5th, 2007, 01:23 PM
#2
Re: dropdown question?
Does your query select both fields so both are in the resulting recordset?
-
Nov 5th, 2007, 01:24 PM
#3
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 5th, 2007, 01:27 PM
#4
Thread Starter
Hyperactive Member
Re: dropdown question?
Hi,
Yes both fields are in result recordset and it is vb6 application
Thanks!
-
Nov 5th, 2007, 01:33 PM
#5
Re: dropdown question?
When you put data into the control, also put the ID into the ItemData property.
You can see an example of how to do that in the Database FAQ How can I fill a ComboBox/ListBox with values from a database?
Then when you save, simply save the ItemData
-
Nov 5th, 2007, 01:33 PM
#6
Re: dropdown question?
Then try
Code:
cbocompany.Additem RS.Fields.Item("customername").Value & " " & RS.Fields.Item("customerid").Value
-
Nov 5th, 2007, 01:33 PM
#7
Re: dropdown question?
does the user NEED to see the id? or just the name....
you could store the ID in itemdata
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 5th, 2007, 01:45 PM
#8
Thread Starter
Hyperactive Member
Re: dropdown question?
 Originally Posted by Static
does the user NEED to see the id? or just the name....
you could store the ID in itemdata
I only want the use see the customer name, not the id.
-
Nov 5th, 2007, 01:56 PM
#9
Re: dropdown question?
cbocompany.itemdata(row) = id
each row can have its own itemdata.. so as you loop through and add the name, set the itemdata for that row
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 5th, 2007, 02:14 PM
#10
Thread Starter
Hyperactive Member
Re: dropdown question?
I use the code example to populate the dropdown, so the dropdown get populate with two fields value in it, but how can I tell which is the row that user pick then update the customer id back to the database
cbocustomer.itemdata(index) ?
-
Nov 5th, 2007, 02:21 PM
#11
Re: dropdown question?
cbocustomer.itemdata(cbocustomer.listindex)
or is it index? selected? hmmm.. i dont have VB with me to check... lol
JPnyc rocks!! (Just ask him!)
If u have your answer please go to the thread tools and click "Mark Thread Resolved"
-
Nov 5th, 2007, 02:22 PM
#12
Re: dropdown question?
Based on what you were doing here
 Originally Posted by mapperkids
p=0
Do Until customer_table.EOF()
Cbocompany.AddItem rs![customername], p
p = p + 1
rs.MoveNext
Loop
I thought you wanted to see both.
-
Nov 5th, 2007, 02:32 PM
#13
Thread Starter
Hyperactive Member
Re: dropdown question?
Hi,
based on the code example from DATABASE FAQ mentioned above, I did populate both customer name and customer id to the dropdown box. the customer id is store on itemdata of the dropdown.
Now, i want to put the data - customer id back to the database. how do I know which one is the user picked from the dropdown and write it back to the database.
I think I need to put some code in the dropdown box CHANGE/CLICK events?
Thanks!
-
Nov 5th, 2007, 02:50 PM
#14
-
Nov 5th, 2007, 02:51 PM
#15
Re: dropdown question?
Static was correct, to read the data from the ItemData you would use:
Code:
Cbocompany.itemdata(Cbocompany.listindex)
..as to where (and how) you would use it, that depends on what you want.
-
Nov 7th, 2007, 01:40 PM
#16
Thread Starter
Hyperactive Member
Re: dropdown question?
Hi,
Cbocustomer.itemdata(Cbocustomer.listindex) works, but the problem is when I pick the first one from the list which means the listindex is 0, it doesn't contain the customer ID for in the itemdata(0), just return the 0 and it crash my application.
Here is the code that I'm using
Code:
====================================
To Save the data back to the file
CboCustomer.ItemData(CboCustomer.ListIndex)
====================================
To populate the dropdown
Call FillCombo(CboCustomer, Conn1, "SELECT * FROM customer_tbl order by customer_name", "customer_name", "customer_id")
==================================================
Sub to fill the combon box
Public Sub FillCombo(objComboBox As ComboBox, _
Conn1 As ADODB.Connection, _
strSQL As String, _
strFieldToShow As String, _
Optional strFieldForItemData As String)
Dim oRS As ADODB.Recordset 'Load the data
Set oRS = New ADODB.Recordset
oRS.Open strSQL, Conn1, adOpenForwardOnly, adLockReadOnly, adCmdText
With objComboBox 'Fill the combo box
.Clear
If strFieldForItemData = "" Then
Do While Not oRS.EOF '(without ItemData)
.AddItem oRS.Fields(strFieldToShow).Value
oRS.MoveNext
Loop
Else
Do While Not oRS.EOF '(with ItemData)
.AddItem oRS.Fields(strFieldToShow).Value
.ItemData(.NewIndex) = oRS.Fields(strFieldForItemData).Value
oRS.MoveNext
Loop
End If
End With
oRS.Close 'Tidy up Set oRS = Nothing End Sub
End Sub
===================================================
Any idea?
Thanks!
Last edited by Hack; Nov 7th, 2007 at 01:42 PM.
Reason: Added Code Tags
-
Nov 7th, 2007, 01:43 PM
#17
Re: dropdown question?
When you save "crash the application" I assume that means you get a run time error.
What is the error?
-
Nov 7th, 2007, 01:55 PM
#18
Thread Starter
Hyperactive Member
Re: dropdown question?
Hi,
The run time error is related to the foreigen key not match on the SQL tables, since when I write the data back to the record, I have to put the customer ID from the itemdata(index) to the field in the customer iD field and
since it contain the value as 0, the relationship between the table is broken and that is the runtime error comes up.
 Originally Posted by Hack
When you save "crash the application" I assume that means you get a run time error.
What is the error?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|