Solved

Listview with SubItems Problem

Posted on 2001-06-20
14
463 Views
Last Modified: 2011-09-20
I am trying to populate a listview control with data. For some reason when I try to set the 'NewLine'
varible I get a type mismatch error. See the following code;

Function FillList(Domain As String, LV As Object) As Boolean
'==================================================================
     '  Purpose:   to fill a ListView control with data from a table or
     '             query
     '  Arguments: a Domain which is the name of the table or query, and
     '             a ListView control object
     '  Returns:   A Boolean value to indicate if the function was
     '             successful
     '==================================================================

     Dim db As database, rs As Recordset
     Dim intTotCount As Integer
     Dim intCount1 As Integer, intCount2 As Integer
     Dim colNew As ColumnHeader
     Dim NewLine As ListItem

     On Error GoTo Err_Man

     ' Clear the ListView control.
     lstBudgetLoadType.ListItems.Clear
     lstBudgetLoadType.ColumnHeaders.Clear

     ' Set Variables.
     Set db = CurrentDb
     Set rs = db.OpenRecordset(Domain)

     ' Set Column Headers.
     For intCount1 = 0 To rs.Fields.Count - 1
        lstBudgetLoadType.ColumnHeaders.Add , , rs(intCount1).Name
     Next intCount1
     
     lstBudgetLoadType.View = 3    ' Set View property to 'Report'.

     ' Set Total Records Counter.
     rs.MoveLast
     intTotCount = rs.RecordCount
     rs.MoveFirst

     ' Loop through recordset and add Items to the control.
     For intCount1 = 1 To intTotCount
        If IsNumeric(rs(0).Value) Then
            Set NewLine = lstBudgetLoadType.ListItems.Add(, , str(rs(0).Value))
        Else
            Set NewLine = lstBudgetLoadType.ListItems.Add(, , rs(0).Value)
        End If
        For intCount2 = 1 To rs.Fields.Count - 1
            NewLine.SubItems(intCount2) = rs(intCount2).Value
        Next intCount2
        rs.MoveNext
     Next intCount1

     Exit Function

Err_Man:
       Resume Next
        ' Ignore Error 94 which indicates you passed a NULL value.
        If Err = 94 Then
           Resume Next
        Else
        ' Otherwise display the error message.
           MsgBox "Error: " & Err.Number & Chr(13) & _
              Chr(10) & Err.Description
        End If
End Function

Does anyone have experience with this and tell me why I cannot set the lineitem so that I can populate
my subitem data?

Lyners

0
Comment
Question by:lyners
  • 6
  • 4
  • 2
  • +2
14 Comments
 
LVL 4

Expert Comment

by:nutwiss
ID: 6210750
try

For intCount2 = 1 To rs.Fields.Count
    NewLine.SubItems(intCount2) = rs.fields(intCount2 - 1).Value
next
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6210753
PS the field index and subitem index will be 1 out, as the subitems are 1-based and fields collection is 0-based
0
 
LVL 15

Expert Comment

by:ameba
ID: 6210758
Last 10 Grades Given:  B A B A B A C A B  
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6210784
and?

you're so point horny, ameba...... :)
0
 
LVL 1

Author Comment

by:lyners
ID: 6210785
nutwiss,
Right before that I have a Set Newline = list.....

I get an error right there of a type mismatch. It doesn't appear to like the listitem. Am I doing something wrong with the Set statement?

Lyners

0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6210816
instead of:

    Set NewLine = lstBudgetLoadType.ListItems.Add(, , str(rs(0).Value))

try

    Set NewLine = lstBudgetLoadType.ListItems.Add(, , str(rs.fields(0).Value))



YOU KEEP LEAVING OUT THE 'FIELDS' COLLECTION
0
 
LVL 1

Author Comment

by:lyners
ID: 6210854
Tried adding the fields. Thanks, but i am still getting the error; 13 TYPE MISMATCH on the line;

Set NewLine = lstBudgetLoadType.ListItems.Add(, , str(rs.fields(0).Value))

I've tried to do this several different ways. I have even tried the way Microsoft shows in there examples, and it doesn't work. Do I have a DLL problem?





0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Expert Comment

by:mcbeth
ID: 6210858
For intCount1 = 1 To intTotCount
       If IsNumeric(rs(0).Value) Then
           Set NewLine = lstBudgetLoadType.ListItems.Add()

newline.text = cstr(rs(0).value)

For intCount2 = 1 To rs.Fields.Count - 1
           NewLine.SubItems(intCount2) = rs(intCount2).Value
       Next intCount2
       rs.MoveNext
    Next intCount1

 
notice that first row in listview must have a value
0
 
LVL 15

Expert Comment

by:ameba
ID: 6210878
nutwiss,
>you're so point horny, ameba...... :)
Yes, I know ;-)

But, honestly, this was for lyners' good in his future questions.
With better grading history, he would have the right answer ~25 minutes ago.
0
 
LVL 1

Author Comment

by:lyners
ID: 6210936
McBeth - I tried your way and still got a 13 type mismatch error on:

Set NewLine = lstBudgetLoadType.ListItems.Add()

Any suggestions?




0
 
LVL 4

Accepted Solution

by:
nutwiss earned 100 total points
ID: 6210947
I assume you've got the LATEST common controls control referenced??

if not, then you are using the old incompatible version of the listview control..

Actually, come to think of it, your error sounds like you've got both versions referenced, and your

Dim NewLine As ListItem

is ambiguous
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6211016
Yeah, you may be passing LV in from a different ocx - try being super-explicit with your parameters and variables, eg Dim NewLine As MSComctlLib.ListItem
or be super implicit (ugh!) and use
dim NewLine as object

If the "as object" doesn't work, then you've probably got some other problem.

0
 
LVL 1

Author Comment

by:lyners
ID: 6211243
Nutwiss,
That is what the problem was. I had COMCTL32.OCX and MSCOMCTL.OCX referenced. I removed the COMCTL32.OCX reference and all is good now. Thanks for the info!

Lyners
0
 
LVL 4

Expert Comment

by:nutwiss
ID: 6211260
my pleasure - BTW it's a common problem.... :)

try using DAO and ADO in the same project!!

....Dim myAmbiguousRecordset As Recordset....
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…

705 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

14 Experts available now in Live!

Get 1:1 Help Now