Results 1 to 4 of 4

Thread: Query to Listbox to UDT Question

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2004
    Posts
    25

    Query to Listbox to UDT Question

    Hello everyone,

    I'm a ColdFusion programmer whose been tasked with creating a VB6 application for the marketing department of my company. It's a simple application, and here's basically how it works:

    You search for a product from a textbox. The results of the search populate a combo box. When you select something from the combo box, it populates a series of text fields with information about the product (title, format, catalog number, price, etc...). From there you can click an "add to shopping cart" button, which stores that product into a shopping cart database table. Make sense so far?

    Okay, so I have the search working. What I can't get is populating the combo box and subsequent text fields once you select something in the combo box. After reading a few of the books we have lying around, I figured it would be best to create a UDT to store the information. So I did that:

    VB Code:
    1. Private Type ProductUDT
    2.     the_title As String
    3.     Title As String
    4.     Format As String
    5.     Price As String
    6.     ID As String
    7.     SKU As String
    8. End Type
    9. Dim Products() As ProductUDT, i As Long

    Then I run the query:

    VB Code:
    1. Dim SQL_Search As ADODB.Recordset
    2.     Set SQL_Search = New ADODB.Recordset
    3.  
    4.     Dim searchtext As String
    5.     searchtext = txt_search.Text
    6.  
    7.     SQL_Search.Source = "SELECT (title & '('&Format&')') As the_title, title, ID, format, sku, price FROM lkpProduct WHERE (title LIKE '%" & searchtext & "%') OR (sku LIKE '%" & searchtext & "%') ORDER BY title"
    8.     Set SQL_Search.ActiveConnection = db
    9.     SQL_Search.Open

    Then I have the code to populate the combo box (but it doesn't work yet):

    VB Code:
    1. For i = LBound(Products) To UBound(Products)
    2.         combo_search_results.AddItem Products(i).Title
    3.         combo_search_results.ItemData(combo_search_results.NewIndex) = i
    4.     Next

    Based on what the book says, there needs to be code to tie together the query results to the "Products" UDT that is created. Unforunately, the author purposely omitted the code to do that.

    If someone can please help me to figure out (a) how to tie the query to the UDT; and then (b) how to tie the query information from the selected item in the combo box to the text fields.

    MUCH APPRECIATED! The bosses will be pleased if I can get this working.

    Josh Sager
    www.tlavideo.com

  2. #2
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    Why the UDT? You already have the data you need in the recordset. Just populate the combo box from the recordset.

    VB Code:
    1. Set SQL_Search.ActiveConnection = db
    2. SQL_Search.Open
    3.  
    4. Do
    5.   combo_search_results.AddItem SQL_Search.Fields("Title").Value
    6.  
    7.   'Note, you cannot assign a value to NewIndex.  It is a read only property.  
    8.   'Also ItemData can only be numeric.  If Id is character data this won't work
    9.   combo_search_results.ItemData(combo_search_results.NewIndex) = SQL_Search.Fields("Id").Value
    10.    
    11.     SQL_Search.MoveNext
    12. Loop Until SQL_Search.EOF

    If you insist on using the UDT - again loop through the recordset

    VB Code:
    1. Dim Products() As ProductUDT, i As Long
    2.  
    3. 'Make sure RecordCount is valid before using this statement
    4. 'depending on how you open the recordset it may return -1
    5. Redim Products(SQL_Search.RecordCount - 1)
    6.  
    7. i = 0
    8. Do
    9.    'only use this if RecordCount is invalid.
    10.    Redim Preserve Products(i)
    11.  
    12.     Products(i).Title = SQL_Search.Fields("Title").Value
    13.     Products(i).Id = SQL_Search.Fields("Id").Value
    14.     Products(i)...
    15.     SQL_Search.MoveNext
    16.     i = i + 1
    17. Loop Until SQL_Search.EOF

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2004
    Posts
    25

    Got That, But...

    I got the population of the combo box working; thanks. Now I need to figure out how to populate the text fields once the user selects an item from the combo box.

    Is there a way to do this without having to run a second query, since I already have the information I need from the first query? In this particular case, "ID" is numeric, but since you're saying that I can't assign a value to NewIndex, how would I go about correcting this and doing what I need to.

    Thanks so much for your help.

    Josh


    To be clear as to where I am in the game now, I've attached the code I'm working on. The top is the part of the function that runs the query. The second sub is the routine that will populate the text labels/fields when an item is clicked in the combo box.

    VB Code:
    1. ' Disable the submit button so it's not clicked multiple times
    2.     cmd_search.Enabled = False
    3.  
    4.     ' Search for a title by text or sku when entered in the search text field
    5.     Dim SQL_Search As ADODB.Recordset
    6.     Set SQL_Search = New ADODB.Recordset
    7.  
    8.     Dim searchtext As String
    9.     searchtext = txt_search.Text
    10.  
    11.     SQL_Search.Source = "SELECT (title & '('&Format&')') As the_title, title, ID, format, sku, price FROM lkpProduct
    12. WHERE (title LIKE '%" & searchtext & "%') OR (sku LIKE '%" & searchtext & "%') ORDER BY title"
    13.     Set SQL_Search.ActiveConnection = db
    14.     SQL_Search.Open
    15.  
    16.     With SQL_Search
    17.         ' Loop through the results and populate the combo_search_results box
    18.         Do While Not .EOF
    19.             ' Populate the combo box with the query results
    20.             combo_search_results.AddItem SQL_Search.Fields!the_title.Value
    21.             .MoveNext
    22.         Loop
    23.         .Close
    24.     End With
    25.    
    26.     ' When the query has finished, re-enable the submit button
    27.     cmd_search.Enabled = True
    28.    
    29. End Sub
    30.  
    31.  
    32.  
    33. Private Sub combo_search_results_Click()
    34.     label_selected_product_title.Caption = combo_search_results.Text
    35.     ' label_price_amt.Caption = SQL_Search.
    36. End Sub
    Last edited by kagejs; Feb 4th, 2004 at 08:40 AM.

  4. #4
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758
    I mis-read your initial post and for some reason thought you were trying to do this

    combo_search_results.NewIndex = i

    Anyways, since you do not want to do another query, you would need to keep your recordset open. Give the SQL_Search recordset greater scope by declaring it private to the Form.

    Where do you close the connection? Do you keep it active during the entire program or do you close it after getting a recordset?

    If you close it, use a disconnected recordset (ie the recordset is still open after the database connection has been closed).

    VB Code:
    1. SQL_Search.Source = "SELECT (title & '('&Format&')') As the_title, title, ID, format, sku, price FROM lkpProduct
    2. WHERE (title LIKE '%" & searchtext & "%') OR (sku LIKE '%" & searchtext & "%') ORDER BY title"
    3.     Set SQL_Search.ActiveConnection = db
    4.  
    5.     [b]SQL_Search.CursorLocation = adUseClient
    6.     SQL_Search.Open ,, adOpenStatic, adLockReadOnly, adCmdText
    7.     Set SQL_Search.ActiveConnection  = Nothing[/b]
    8.  
    9.     .MoveNext
    10.  
    11. 'to access the recordset row when the user selects a combobox item use the AbsolutePosition property
    12. SQL_Search.AbsolutePosition = combo_search_results.ListIndex + 1
    13.  
    14. Once the row is set just populate your text boxes.
    15.  
    16. Text1.Text = SQL_Search.Fields("Id").Value

    Alternatively, you could call the Recordset Filter or Find methods when the user selects a combobox item.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width