CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 1999
    Location
    CA,USA
    Posts
    2

    Add Fieldnames to a Listbox/Combo

    Project : A Query Web Client
    Problem1: How can I add the fieldnames of a
    table directly into a listbox/combo control on a webpage
    Problem2: A field within the table say has 5 possible values as of now. I want a combobox to
    be populated with these values without me entering them manually or assigning them to the combo box as selection or option.
    Example
    fieldname status contains values such as complete, incomplete, deferred, reported, needs verification.
    Assume if their are 4000 records in the table the status field would carry a value which is one of these.
    I want the combo to get populated against a command button captioned "possible values" which, when hit by the user the combo has the possible values added. I hope whoever reads this understood what I am hinting at. Otherwise please get back to me so that

    Thank You for your time in reading and understanding my problems.


  2. #2
    Join Date
    Feb 2000
    Posts
    2

    Re: Add Fieldnames to a Listbox/Combo

    I have the exact same problem as you (problem 1).

    I'd really like to know the answer as well.


  3. #3
    Join Date
    Dec 1999
    Posts
    128

    Re: Add Fieldnames to a Listbox/Combo

    One of the Recordset object's properties is a collection named Fields.

    Here is a sample:


    Dim Rs as Recordset
    Dim i as Integer

    for i = 0 to Rs.Fields.Count - 1
    MyList.AddItem Rs.Fields(i).Name
    next




    -------------------------
    Nick A.

  4. #4
    Join Date
    Jan 2000
    Location
    MO, USA
    Posts
    1,506

    Re: Add Fieldnames to a Listbox/Combo

    If you are running an activex control that you built for use on the web, then you can use a standard combo box, but if you want VB to return some HTML to the client, you'll have to use the option and select tags to build that list. You can do it with something like this:

    you will have to figure out the best place for this code - whether an ASP page or inside a custom dll, etc.

    You can use this code to get all the field names into a dropdown box on the web page. Then when they choose one and click the "possible values" (which in HTML terms would an input type=submit) that will post this information to another page that will use the second chunk of code below to generate the list of possible values.


    dim str as string
    dim i as integer
    dim rs as recordset

    'get an empty recordset
    'SELECT * FROM Table WHERE 1=2
    'open the recorset
    ...
    str = "<select name=fieldname>" & vbcrlf
    for i = to rs.fields.count - 1
    'build the html stream to build the drop down box
    str = str & "<option>" & rs.fields(i).name & "</option>"
    next x
    str = str & "</select>"

    'send this html to the client
    response.write str




    Here is the code to fill the drop down box with the values in the field that the user selects, the field name would be passed to this page in the form collection from the previous code.


    dim rs as recordset
    dim str as string
    dim sql as string

    'open the recorset with the distinct values from that field that was chosen.
    'ie
    sql = "select distinct " & request.form("fieldname") & " from table "
    ...

    'build the list...

    str = "<select>" & vbcrlf
    While Not rs.EOF
    'i included some code formatting so if you wanted to do a view source on the resulting html page it would be readable the vbcrlf's and vtTab's
    str = str & "<option>" & rs!fieldname & "</option>" & vbcrlf & vbtab
    rs.movenext
    Wend
    str = str & "</select>"

    response.write str




    Hope this helps and makes a little sense,
    John


    John Pirkey
    MCSD
    www.ShallowWaterSystems.com
    John Pirkey
    MCSD (VB6)
    http://www.stlvbug.org

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

Featured