Custom populate ListView from mySQL DB

Posted on 2005-05-13
Last Modified: 2008-02-01

I am trying to do a custom Listview Population code.

I am using a setting.ini file to set the names of the columnheaders and I need to also set in the ini file what fieldnames to go with those
Column headers is very easy:

        For ggr = 1 To ReadINI("Datagrid", "headers", App.Path + "\settings.ini")
            lvRecs.ColumnHeaders.Add ggr, , ReadINI("Datagrid", CStr(ggr), App.Path + "\settings.ini")

this populates the colimn headers on the ListView.

I want to now :

How do I automate the:
lvRecs.ListItems.Add , , Str(rsClients.Fields!UserId)  <-

so when adding another field in the database, all I have to do is alter the settings.ini with the new field.

Using a datagrid would solve the problem but adodc and datagrid are way to buggy for me,
they work and crash now and then so they are not reliable at all.

Alternatively, do you know another listview/datagrid/something that works better than adodc+datagrid.

Question by:Berhan Karagoez
    LVL 12

    Accepted Solution

    You might consider using an MSFlexGrid control.  Here is some sample code to setup and populate a flexgrid:

    grdParts.cols = 4
        grdParts.ColAlignment(1) = flexAlignLeftCenter
        grdParts.ColAlignment(2) = flexAlignLeftCenter
        'grdParts.ColAlignment(2) = flexAlignLeftCenter
        grdParts.ColAlignment(3) = flexAlignRightCenter
        grdParts.ColWidth(0) = 0
        grdParts.ColWidth(1) = 1000
        grdParts.ColWidth(2) = 4500
        grdParts.ColWidth(3) = 1000
        grdParts.Row = 0
        grdParts.Col = 1
        grdParts.Text = "Part Num"
        grdParts.Col = 2
        grdParts.Text = "Description"
        grdParts.Col = 3
        grdParts.Text = "MSRP"
        ' populate default first row
        grdParts.Row = 1
        grdParts.Col = 1
        grdParts.Text = "2871217"
        grdParts.Col = 2
        grdParts.Text = "Gen III Mirror"
        grdParts.Col = 3
        grdParts.Text = "$34.99"

        ' add new rows
        grdParts.AddItem vbTab & "2872702" & vbTab & "ROCK GUARD,GEN III" & vbTab & "$24.99"
        grdParts.AddItem vbTab & "2872323" & vbTab & "1999-2004 Scrambler 500 electronic speedometer" & vbTab & "$229.99"
        grdParts.AddItem vbTab & "2872699" & vbTab & "Alum A-Arm guards" & vbTab & "$119.99"
        grdParts.AddItem vbTab & "2872698" & vbTab & "Alum full chassis skid plate" & vbTab & "$99.99"
        grdParts.AddItem vbTab & "2872701" & vbTab & "Alum swing arm guard" & vbTab & "$99.99"
        grdParts.AddItem vbTab & "2871792" & vbTab & "Shock covers - front" & vbTab & "$29.99"
        grdParts.AddItem vbTab & "2873052" & vbTab & "Shock covers - rear" & vbTab & "$14.99"
        grdParts.AddItem vbTab & "2873946" & vbTab & "Gen III alum front bumper" & vbTab & "$199.99"
        grdParts.AddItem vbTab & "2872043" & vbTab & "Gen III front rack" & vbTab & "$79.99"
        grdParts.AddItem vbTab & "2872044" & vbTab & "Gen III rear rack" & vbTab & "$119.99"
        grdParts.AddItem vbTab & "2872342" & vbTab & "Gen III Black Front Rack Bag" & vbTab & "$49.99"
        grdParts.AddItem vbTab & "2872343" & vbTab & "Gen III Black Rear Rack Bag" & vbTab & "$49.99"
        grdParts.AddItem vbTab & "2873559" & vbTab & "Gen III ATV cover" & vbTab & "$69.99"
        grdParts.AddItem vbTab & "2873341" & vbTab & "Gen III Cooler / gas rack bag" & vbTab & "$44.99"
        grdParts.AddItem vbTab & "2872045" & vbTab & "Gen III Gas can rack" & vbTab & "$74.99"
        grdParts.AddItem vbTab & "2873559" & vbTab & "Gen III Cover Black/Red" & vbTab & "$69.99"
        grdParts.AddItem vbTab & "2872339" & vbTab & "Gen III Black Tank Saddle Bags" & vbTab & "$49.99"
        grdParts.AddItem vbTab & "2873555" & vbTab & "Gen III Screen Headlight Cover" & vbTab & "$24.99"

    Now, in this example, I've hard-coded the values, but you can easily use a recorset instead.
    LVL 12

    Expert Comment

    Notice I've set the first "header" column to a width of 0:

    grdParts.ColWidth(0) = 0

    And I'm setting the header row values, which you can do with your calls to the ini

    Regarding to the part of populating data, you could build a string, such as:

    sData =  vbTab & rsClients.Fields!UserId & vbTab & rsClients.Fields!UserName & vbTab & rsClients.Fields!UserPhone

    Then use it to add to the grid:
    grdParts.AddItem sData


    Author Comment

    by:Berhan Karagoez
    Preece, thanx for the answer but this is not what I am looking for.

    The code should be in a ro loop to read all fields from the settings ini. the whole point with this question is not to what you show.


    Author Comment

    by:Berhan Karagoez
    Also the data is retrieved from a database, this is the "tricky part".
    How do I from what I have in the settings file (field names) populate the drid/LV with right field data.

    LVL 12

    Expert Comment

    Yeah, the code was intended to give you an idea of how to use a FlexGrid.  It is up to you to adapt it to your getting of a recordset, using the for loop, etc.  Because I'm not there, an I can only provide guidance based upon the little bit of info you've provided.  I'm not grilling you, I'm just making a point!

    But to your point of getting the field names to synch up with the grid, here are some things to ponder:

    Read in your ini file, put the field names into a collection or an array, then use that collection / array to:

    - setup up your grid
    - get your rs
    - populate your grid

    This is assuming that the order of your field names in the ini is the order in which you want the data to be displayed in the grid...

    I like the idea of adding a field to ini and then the code dynamically adding a new column and the respective data to the grid.  Saves adding code and recompiling...


    Author Comment

    by:Berhan Karagoez
    Yes, the whole idea is to do just that.

    Let' me test your suggestion, and get back to you!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Introduction This article makes the case for using two modules in your VBA/VB6 applications to provide both case-sensitive and case-insensitive text comparison operations.  Recently, I solved an EE question using the LIKE function.  In order for th…
    This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    761 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now