Results 1 to 7 of 7

Thread: Displaying selected item from listbox into textbox.

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Exclamation Displaying selected item from listbox into textbox.

    hello,
    i am doing a project on mobile store in vb6.
    i have a mobile store.mdb access(2007) database.
    The layout for the form is given below..



    The database has tables like...APPLE,BLACKBERRY,NOKIA,HTC etc.
    every table has fields..Model_Number,Type,Platform etc.

    when i select the table name from the combobox (like APPLE in the screenshot). The listbox displays the Model_Number field in APPLE table...along with the pic. in the frame.
    The problem is...i want that when the user clicks the item say "iPhone 4 16 GB" , the 3 textboxes below the frame should display the Model Number,Type and platform for the selected item in listbox into the corressponding textbox.

    Plz help..it's urgent

  2. #2
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: Displaying selected item from listbox into textbox.

    Code:
    With cmd
        .ActiveConnection = cn
        .CommandType = adCmdText
        .CommandText = "SELECT * FROM APPLE WHERE Product_Name = ?"
        .Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, lstProductName.text)
        Set rs = .Execute
    End With
    
    txtModelNumber.text = rs.fields("Model_Number").value
    txtPlatform.text = rs.fields("Platform").value
    txtType.text = rs.fields("Type").value

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Displaying selected item from listbox into textbox.

    Thanks for the reply Letsgetcoding...

    Ur method worked but only partially.
    It works for only the APPLE table (SELECT * FROM APPLE) and that too for the first selected entry(any) in listbox. i want the textbox fields to change dynamically as the user clicks different items in the list box. And the code should work for not only APPLE table but for other table also listed in the combobox i.e Nokia,Blackberry,HTC.....etc.

    Anyway these are the changes i made into your code..plz check if i have written something wrong...coz i doubt i made some mistakes..because there is no field in the table (any) with the name Product_Name and am i supposed to replace the "?" in your code with Model_Number.
    Code:
    With cmd
        .ActiveConnection = con
        .CommandType = adCmdText
        .CommandText = "SELECT * FROM APPLE WHERE Product_Name = Model_Number"
        .Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, List1.Text)
        Set rs = .Execute
    End With
    
    Text1.Text = rs.Fields("Model_Number").Value
    Text2.Text = rs.Fields("Platform").Value
    Text3.Text = rs.Fields("Type").Value
    If you need additional information..Plz tell me.
    Sorry for my bad English. Plz help i am new to vb.
    Thnks in advanced...waiting for your reply.
    Last edited by prasenjit007; Sep 30th, 2010 at 10:42 PM.

  4. #4
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: Displaying selected item from listbox into textbox.

    Hey,

    You will need to store the table name in a string.

    Code:
    Dim strTable as string
    Dim strSelected as string
    Dim i as integer
    
    strTable = (This is where you put the selected table)
    i = list1.listindex
    strSelected = list1.list(i)
    
    With cmd
        .ActiveConnection = con
        .CommandType = adCmdText
        .CommandText = "SELECT * FROM " & strTable & " WHERE Product_Name = ?" 'Leave this as a question mark. It will reference to the parameter below. 
        .Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, strSelected)
        Set rs = .Execute
    End With
    
    Text1.Text = rs.Fields("Model_Number").Value
    Text2.Text = rs.Fields("Platform").Value
    Text3.Text = rs.Fields("Type").Value
    Last edited by Letsgetcoding; Sep 30th, 2010 at 10:46 PM. Reason: Mistake

  5. #5

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Displaying selected item from listbox into textbox.

    Hey frnd,
    I am getting an error on the line "Set rs = .Execute"
    ERROR:-Run-time error '-2147217904(80040e10)'
    No value given for one or more required parameters.


    Could you plz tell me what am doing wrong? I googled it but didn't got any satisfactory result...

    Thanks...

  6. #6
    Addicted Member
    Join Date
    May 2009
    Location
    New Zealand
    Posts
    241

    Re: Displaying selected item from listbox into textbox.

    Code:
    Dim strTable as string
    Dim strSelected as string
    Dim i as integer
    
    Set cmd = New ADODB.Command
    Set rs = New ADODB.Recordset
    
    strTable = (This is where you put the selected table)
    i = list1.listindex
    strSelected = list1.list(i)
    
    With cmd
        .ActiveConnection = con
        .CommandType = adCmdText
        .CommandText = "SELECT * FROM " & strTable & " WHERE Product_Name = ?" 'Leave this as a question mark. It will reference to the parameter below. 
        .Parameters.Append .CreateParameter(, adVarChar, adParamInput, 50, strSelected)
        .Prepared = True
        Set rs = .Execute
    End With
    
    Text1.Text = rs.Fields("Model_Number").Value
    Text2.Text = rs.Fields("Platform").Value
    Text3.Text = rs.Fields("Type").Value

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2010
    Posts
    8

    Re: Displaying selected item from listbox into textbox.

    i tried your new code but still i'm getting the same error......

Tags for this Thread

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