Results 1 to 19 of 19

Thread: Access style multi-column listbox

  1. #1

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Access style multi-column listbox

    I'm in the process of converting an Access 2000 front-end to VB6. I've got quite a bit of VBA experience, but am only just starting out with VB6. Anyway, I've already hit a bit of a stumbling block as I notice that VB6 listboxes do not behave the same way as Access listboxes. The Access front-end I need to convert uses them extensively as listboxes tend to work faster than continuous forms (in Access). Is there an accepted way I should be converting the Access listboxes to VB6? I've done a lot of searching, but to no avail. I need to have multi-column listboxes that also have a hidden column (key field). For example, I have a list of clients in a listbox, and when the user double clicks on a client, the hidden column (key field) is used to populate the rest of the form. Any help much appreciated.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Access style multi-column listbox

    Welcome to the forums.

    There are no multi column Listboxes in VB6. This is something I've always felt was sorely lacking and yet another cause for me to believe that often the development teams never talked to each other.

    Anyway, in VB6, use the ListView control. It will do what you need.

  3. #3

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Re: Access style multi-column listbox

    Thanks, the ListView control seems exactly what I was looking for. And thanks for the welcome!

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Access style multi-column listbox

    You are welcome.

    The ListView syntax differs somewhat from the ListBox, but if you run into any issues, just post a question.

    If you consider this resolved, you could help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.

    Thank you.

  5. #5

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Re: Access style multi-column listbox

    I've managed to populate the ListView with items from a recordset, however, how can I obtain the Key value of the selected item (for example, when the user double clicks an item)? Also, I noticed that when assigning a Key, it has to contain an alpha character, it wouldn't accept a numerical value. I tried converting the numerical value to a string but it still wouldn't let me use it unless I put an alpha-prefix before the number.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access style multi-column listbox

    You can check for the items selected by looping through the ListItems collection checking for the .Selected property to be true.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Re: Access style multi-column listbox

    Is there a simpler method if the user can only select one item at a time?

  8. #8
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Access style multi-column listbox

    Well a combo box control is the simpliest but only single column'ed. A listview is really not that difficult to work with.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Access style multi-column listbox

    Quote Originally Posted by bcass
    Is there a simpler method if the user can only select one item at a time?
    From ListView Properties window, uncheck [ ] Multi Select box.
    or by code:
    Code:
    ListView1.MultiSelect = False

  10. #10

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Re: Access style multi-column listbox

    Sorry, I just meant how could I get the Key value of the item the user selected, here's how I ended up doing it:

    Code:
    lvwViewList.SelectedItem.Key
    However, now I'm stuck again - how can I change the width of the column (I only have 1 column in this particular ViewList)? Also, I am not showing ColumnHeaders, if that makes any difference?

  11. #11
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Access style multi-column listbox

    ListView1.ColumnHeaders(1).Width = 3000

    I've managed to populate the ListView with items from a recordset, however, how can I obtain the Key value of the selected item (for example, when the user double clicks an item)?
    You could use the ListItem.Tag property. The Tag property is a Variant and can contain any data type required by an application.

    Code:
    Private Sub Form_Load()
        Dim oItem As ListItem
        Set oItem = ListView1.ListItems.Add(, , "Some Text")
        oItem.Tag = 12345
    End Sub
    
    Private Sub ListView1_DblClick()
        MsgBox ListView1.SelectedItem.Tag
    End Sub
    Last edited by brucevde; Dec 20th, 2007 at 05:33 PM.

  12. #12

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Re: Access style multi-column listbox

    Quote Originally Posted by brucevde
    ListView1.ColumnHeaders(1).Width = 3000
    I actually tried that before posting, but I get the error "Index out of bounds". I'm using the lvwReport View of the ListView if that might be affecting anything?

  13. #13
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Access style multi-column listbox

    You must have at least 1 Column when using lvwReport view.
    ColumnHeaders is a collection and collections are 1 based.

  14. #14

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Re: Access style multi-column listbox

    I have 1 column (the data is showing) but I'm not showing ColumnHeader. Are you saying it can only be done if you show the ColumnHeader?

  15. #15
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: Access style multi-column listbox

    With or without showing column headers, you always can set ColumnHeaders(1).Width = ...

  16. #16

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Re: Access style multi-column listbox

    Any idea why I'm getting the "Index out of bounds" error message then?

  17. #17
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Access style multi-column listbox

    .Text is column 1
    .Subitems(1) or .ListSubItems(1) is column 2
    .Subitems(2) or .ListSubItems(2) is column 3

    and so on and so forth... you can check with ListView1.ColumnHeaders.Count adjusted accordingly (less 1 in order to correspond with indices of Subitems).

    As to the error with ColumnHeaders collection, how did you create the column? In the IDE at design time or programmatically at run-time... if at run-time, was the column already created before you tried changing the width? Are you referencing the correct listview in the GUI?

    As to the listitem key, you can easily get rid of the prepended character with Mid(Key, 2) to get the numeric ID rather than iterating through the listview.
    Last edited by leinad31; Dec 21st, 2007 at 06:15 AM.

  18. #18

    Thread Starter
    Lively Member bcass's Avatar
    Join Date
    Dec 2007
    Location
    Island of Dots
    Posts
    108

    Re: Access style multi-column listbox

    Quote Originally Posted by leinad31
    As to the listitem key, you can easily get rid of the prepended character with Mid(Key, 2) to get the numeric ID rather than iterating through the listview.
    Yep, that's what I'd already ended up doing. Just seemed a bit odd that I couldn't use only a numerical value, even when it was assigned to a string variable.

    Anyway, I found out what the problem was with setting the ColumnHeaders - I was trying to define the column width before adding any items to the ListView... I tried it after adding items, and it works perfectly. Thanks for all the help.
    Last edited by bcass; Dec 21st, 2007 at 06:32 AM.

  19. #19
    Frenzied Member aikidokid's Avatar
    Join Date
    Aug 2002
    Location
    Bristol, UK
    Posts
    1,968

    Re: Access style multi-column listbox

    Just something I came across, so if somebody is searching this thread at a later date here is a multicolumn (and more) listbox.
    If somebody helps you, take time to RATE the post. I do.

    "FAILURE IS NOT AN OPTION. It comes bundled with the software."

    Below are some of the threads that have helped me along the way:

    CodeBank submission:
    Listview Backcolor (without subclassing)

    Loading Treeview Nodes From A Database, Creating Registry Keys, Count Number of Lines in TextBox , Excellent RichTextBox Tricks & Tips
    Ideas & Screen Shots For A Code Library App
    How to do Data validation in Excel, Conditional Formating in Excel

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