Error adding item to ListView [Resolved]-VBForums
Results 1 to 6 of 6

Thread: Error adding item to ListView [Resolved]

  1. #1

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828

    Error adding item to ListView [Resolved]

    Hi guys, i'm using a listview for the first time...

    this is my code to add to a listview (2 of them)

    VB Code:
    1. Private Sub ShowRecordLists()
    2. On Error GoTo ErrorHandler
    3.  
    4.     'goes through each record and adds it to the correct listview
    5.    
    6.     rstCalls.MoveFirst
    7.     lvwCompleted.ListItems.Clear
    8.     lvwOutstanding.ListItems.Clear
    9.    
    10.     Do Until rstCalls.EOF = True
    11.         If rstCalls!booCompleted Then
    12.             'ERROR ON LINE BELOW
    13.             lvwCompleted.ListItems.Add , rstCalls!txtID, rstCalls!dtmDateLogged & vbTab _
    14.                 & rstCalls!txtCallerName & vbTab & rstCalls!txtReason
    15.         Else
    16.             lvwOutstanding.ListItems.Add , rstCalls!txtID, rstCalls!dtmDateLogged & vbTab _
    17.                 & rstCalls!txtCallerName & vbTab & rstCalls!txtReason
    18.         End If
    19.         rstCalls.MoveNext
    20.     Loop
    21.    
    22.     Exit Sub
    23.  
    24. ErrorHandler:
    25.     MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ShowRecordLists of Form frmCallLog"
    26. End Sub

    the error is 35603 Invalid Key.

    the value of rstCalls!txtID at this point is "000010"

    the Key parameter is expecting a string expression and rstCalls!txtID is a text field, so i dont see the problem...
    Last edited by darre1; May 1st, 2002 at 08:19 AM.
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

  2. #2
    Fanatic Member Stevie's Avatar
    Join Date
    Mar 2000
    Location
    London, UK
    Posts
    565
    As far as I can remember treeview and listviews still see it as a number even after what you've done. The way I got round this was to add the same 2 chars in front of the number.

    Why are you using tabs to spaces out your columns. You should be using subitems.

    VB Code:
    1. listview.ListItems.Add intIndex, "SomeKey", "SomeText1"
    2.     listview.ListItems(intIndex).SubItems(1) = "SomeText2"
    3.     listview.ListItems(intIndex).SubItems(2) = "SomeText3"

    Hope this helps
    VB6 sp5, SQL Server 2000, C#

    There are no stupid questions. Only stupid people.

  3. #3
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    If I remember correctly, the listview needs an alpha character as the first character of the Key. Can't remember why!
    Try appending a letter and see if that works...
    VB Code:
    1. lvwCompleted.ListItems.Add , [color=red]"x" & [/color]rstCalls!txtID, rstCalls!dtmDateLogged & vbTab _
    2.                 & rstCalls!txtCallerName & vbTab & rstCalls!txtReason

  4. #4
    PowerPoster
    Join Date
    Mar 2002
    Location
    UK
    Posts
    4,630
    The reason you cannot use a number as a key is as follows.

    When you call a collection, you can call it either of 2 ways, using the key, or the index number.

    i.e

    Collection.Item("MYKEY")

    or

    Collection.Item(23)

    VB needs to know which one you are calling, either the index number or the key, hence the key cannot be numeric.

    In answer to your question, a string "0010101", when used as a variable, will think that is is really then number 10101.

    Another example of this problem is as follows, a simple calulation

    Msgbox 35 x 1000

    Will cause an error, as 35 (not declared) is automatticaly created as an interger, and 35 x 1000 = 35000, which is greater then the size of an integer.

    Hope this gives you an idea.

  5. #5
    Super Moderator Wokawidget's Avatar
    Join Date
    Nov 2001
    Location
    Headingly Occupation: Classified
    Posts
    9,633
    The Key for a list Item has to be a string. I have redone your code, just cut and paste
    VB Code:
    1. Private Sub Form_Load()
    2.     CreateColumns
    3. End Sub
    4.  
    5. Private Sub CreateColumns()
    6.     With lvwCompleted
    7.         .View = lvwReport
    8.         .LabelEdit = lvwManual
    9.         .FullRowSelect = True
    10.     End With
    11.     With lvwCompleted.ColumnHeaders
    12.         .Add , "DATE", "Date Logged", 1000
    13.         .Add , "CALLER", "Caller", 2000
    14.         .Add , "REASON", "Reason", 3000
    15.     End With
    16.     With lvwOutstanding
    17.         .View = lvwReport
    18.         .LabelEdit = lvwManual
    19.         .FullRowSelect = True
    20.     End With
    21.     With lvwOutstanding.ColumnHeaders
    22.         .Add , "DATE", "Date Logged", 1000
    23.         .Add , "CALLER", "Caller", 2000
    24.         .Add , "REASON", "Reason", 3000
    25.     End With
    26. End Sub
    27.  
    28. Private Sub Populate()
    29. Dim rstCalls        As Recordset
    30. Dim lvwItem         As ListItem
    31.     'Your code here to get records
    32.     With rstCalls
    33.         Do While Not .EOF
    34.             If .Fields("booCompleted").Value Then
    35.                 Set lvwItem = lvwCompleted.ListItems.Add(, .Fields("txtID").Value & "K", .Fields("dtmDateLogged").Value)
    36.                 lvwItem.SubItems(1) = .Fields("txtCallerName").Value
    37.                 lvwItem.SubItems(2) = .Fields("txtReason").Value
    38.             Else
    39.                 Set lvwItem = lvwOutstanding.ListItems.Add(, .Fields("txtID").Value & "K", .Fields("dtmDateLogged").Value)
    40.                 lvwItem.SubItems(1) = .Fields("txtCallerName").Value
    41.                 lvwItem.SubItems(2) = .Fields("txtReason").Value
    42.             End If
    43.             .MoveNext
    44.         Loop
    45.         .Close
    46.     End With
    47.     Set rstCalls = Nothing
    48. End Sub
    Well, almost a cut and paste
    To get the ID of an item use the VAL statment, VAL("23K") = 23
    The code can be neatend up a bit but it'll do for starters...

  6. #6

    Thread Starter
    Bouncy Member darre1's Avatar
    Join Date
    May 2001
    Location
    Peterborough, UK
    Posts
    3,828

    Thumbs up

    thank you boys!

    so basically even though txtID is a text field, it made it into a number anyway!!! thats crap!

    and by adding a letter to it it forces it to be a string

    its working now anyway. cheers
    Confucious say, "Man standing naked in biscuit barrel not necessarily ****ing crackers."

    Don't forget to format your code in your posts

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.