Results 1 to 12 of 12
  1. #1
    Join Date
    Feb 2004
    Posts
    59

    Red face Unanswered: ListBox Properties

    Okay - so here's the newbie at it again!

    When I wrote my program in Access... it was very easy to link my ListBox to the table/query in my database, because everything was relative to the database already open.

    So, I've got issues trying to get this to work with just VB.

    I have the form setting DB as DAO.Database, and RS as DAO.Recordset. Then I set DB = "C:\databasepath\database.mdb". But, I cannot for the life of me, figure out how to tell the listbox to open, say,
    tblAccountTypes (the account types table). or, how to pull up a query that is in the database.

    I'm assuming I'm going to have to use something like:
    ListboxName.(something) = DB.tblAccountTypes
    (to specify, open the TABLE named "tblAccountTypes" in the DataBase specified above).

    Does anyone know how I can correctly do this?
    -Matt H-
    www.comperfection.com

  2. #2
    Join Date
    Feb 2004
    Posts
    59

    partial solution

    ok - well I havce a partial solution

    I made a for loop that goes through the recordset of a table one by one and adds it to the list...

    but, I know I will need something better. The better question (than above) would be,
    is there a way I can reference a query that is already in the database (mdb) and make that the source for the combo/list box?

    I have already setup a connection to the db as a DAO database and set the path...

    would it be as easy as setting the datasource of the combobox to something like
    db.openquery(qryTest) (<-basically saying, using the db specified, open the query named "qryTest")..

    or perhaps
    db.qryTest

    Obviously, I don't know the syntax, but I'm assuming I should be able to declare something during the page load to get my combo box to populate from a query in the database.
    -Matt H-
    www.comperfection.com

  3. #3
    Join Date
    Jul 2004
    Posts
    69
    assuming you have your recordset(in this example is rs) connected to a table/query and its alreay open and you're trying to populate a combobox with a specific field in your recordset(in this example, fieldname)

    'code it wherever it is appropriate, ex. in form_load

    rs.MoveFirst
    Do Until rs.EOF = True
    Combo1.AddItem rs!fieldname
    rs.MoveNext
    Loop

  4. #4
    Join Date
    Feb 2004
    Posts
    59

    thanks and update

    thank you very much! That is working for me!

    I do have one problem though...
    I am using a listbox to display information from 6 fields in my table:
    UserName, FirstName, LastName, PhoneNumber, AcountType and AccoutStatus...

    I've modified the loop so the AddItem reads:
    lstbox.AddItem rs5!UserName & rs5!FirstName & .....
    and this gets all the information into the listbox, but its real ugly.

    Is there anyway that I can have it put the items into columns (can I label the header too?)...

    and, is there anyway for it to "autoformat" the width of the columns to view decently?

    Thanks so much everyone!
    -Matt H-
    www.comperfection.com

  5. #5
    Join Date
    May 2004
    Posts
    97
    Instead of a listbox control you might want to consider an MSFlexGrid control. Then your code will look like...

    rs.MoveFirst
    Do Until rs.EOF = True
    mfFlexGrid.AddItem rs5!UserName & vbtab & rs5!FirstName & vbtab & .....
    rs.MoveNext
    Loop

    Then you have columns, headers, a good amount of control over colors, styles, fonts, etc.

  6. #6
    Join Date
    Jul 2004
    Posts
    69
    hi there! instead of using listbox, try using ListView. In this way, you can add headers, etc. to suit your own taste of a good gui. anyways, i use listview. add it as a component, check Microsoft Windows Common Control 6.0. using the "loop" that i wrote last time, (and assuming you add 6 columns in the listview control by setting it in it's property...right-click the control -> property)...Assuming you have your listview control name as ListView1, your recordset as rs, your fields (fields1...fields6). this example assumes (again) that your rs is already connected to a table/query and its already open...


    Private Sub Form_Load()
    Dim items As ListItem

    rs.MoveFirst
    Do Until rs.EOF = True
    set items = ListView1.Add (, , rs!Field1)
    items.SubItems(1) = rs!Field2
    items.SubItems(2) = rs!Field3
    items.SubItems(3) = rs!Field4
    items.SubItems(4) = rs!Field5
    items.SubItems(5) = rs!Field6
    rs.MoveNext
    Loop

    hope this example can help you. good luck
    Last edited by Daniel_Johns; 11-09-04 at 07:16.

  7. #7
    Join Date
    Feb 2004
    Posts
    59

    almost there!

    thanks so much daniel and everyone for posting... i think i'm going to go with the list view at this point, I just like the look better... and from what I've seen it's pretty good.

    When I try to compile the code you gave me... I can tell that my listview isn't going to work right, because the ListView1. (Add), and the "Add" isn't on the list of choices (dropdown)...

    similarly, I get a "method not found" when i try to run the progr. and thats where I'm gettingthe debugger line...

    any thoughts on this? I'm using the correct control, it says "listView" and it gives me options for columns and whatnot under properties...

    I've even removed and recreated the listview, same problem...

    :-/
    -Matt H-
    www.comperfection.com

  8. #8
    Join Date
    Jul 2004
    Posts
    69
    hi mateo. sorry i got it wrong. it should be:

    set items = ListView1.ListItems.Add( , , rs!Field1)

    sorry 'bout this carelessness... :-) good luck with it.

  9. #9
    Join Date
    Feb 2004
    Posts
    59
    i've used your code, but for some reason its coming up wrong... it pulls the info, but seems to put just the usernames one in each column.

    I'd rather have it so that it looks liek this
    Username1 (column1) LastName1 (column2) FirstName1 (column3...)
    Username2 (column1) LastName2 (column2) FirstName2 (column3...)

    here's the code that I used... rs5 is my recordset
    rs5.MoveFirst
    Do Until rs5.EOF = True
    set items = ListView1.ListItems.Add(, , rs5!UserName)
    items.Subitems(1) = rs5!LastName
    items.SubItems(2) = rs5!FirstName
    .. etc...
    rs5.MoveNext
    Loop
    rs5.Close

    any ideas what went wrong? should i be using something like
    set items = ListView1.ListItems.Add(, , rs5!UserName & rs5!LastName &...)?
    -Matt H-
    www.comperfection.com

  10. #10
    Join Date
    Jul 2004
    Posts
    69
    did you properly set-up your listview to accomodate the columns that you want to display? i looked at your code and nothing's wrong with it. maybe its on the listview that you're working at. better check it carefully and post anything back here.

  11. #11
    Join Date
    Feb 2004
    Posts
    59

    Thumbs up

    For anyone else who might run into this predicament... the resolution was as follows (oh boy, do I feel dumb)...

    since I was not showing anything with any "icons" (as is the default for ListView), my data was not being shown correctly.

    The easiest way I found to fix this, was to change the properties for the listbox, under "View" to number 3 - LvwReport, and tada... it works like a champ!

    My only remaining question is...in the last column, I have data being pulled from a Yes/No Checkbox. In ListView it shows as a "True/False" which is fine, but ultimately, could it be a checkbox?

    Turning on the "checkbox" function is not correct.

    Thanks again for everyone's wonderful help!
    -Matt H-
    www.comperfection.com

  12. #12
    Join Date
    Jul 2004
    Posts
    69
    hi mateo! as far as my experience on using listview, only the first column can have the "checkbox" state that you are looking. if you like to have that kind of listview, you can either use other controls (3rd party controls) or you can re-create a custom listview that will suit your need (but this is hard...)

Posting Permissions

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