-
May 1st, 2002, 04:27 AM
#1
Thread Starter
Bouncy Member
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:
Private Sub ShowRecordLists()
On Error GoTo ErrorHandler
'goes through each record and adds it to the correct listview
rstCalls.MoveFirst
lvwCompleted.ListItems.Clear
lvwOutstanding.ListItems.Clear
Do Until rstCalls.EOF = True
If rstCalls!booCompleted Then
'ERROR ON LINE BELOW
lvwCompleted.ListItems.Add , rstCalls!txtID, rstCalls!dtmDateLogged & vbTab _
& rstCalls!txtCallerName & vbTab & rstCalls!txtReason
Else
lvwOutstanding.ListItems.Add , rstCalls!txtID, rstCalls!dtmDateLogged & vbTab _
& rstCalls!txtCallerName & vbTab & rstCalls!txtReason
End If
rstCalls.MoveNext
Loop
Exit Sub
ErrorHandler:
MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure ShowRecordLists of Form frmCallLog"
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.
-
May 1st, 2002, 05:19 AM
#2
Fanatic Member
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:
listview.ListItems.Add intIndex, "SomeKey", "SomeText1"
listview.ListItems(intIndex).SubItems(1) = "SomeText2"
listview.ListItems(intIndex).SubItems(2) = "SomeText3"
Hope this helps
VB6 sp5, SQL Server 2000, C#
There are no stupid questions. Only stupid people.
-
May 1st, 2002, 05:21 AM
#3
Frenzied Member
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:
lvwCompleted.ListItems.Add , [color=red]"x" & [/color]rstCalls!txtID, rstCalls!dtmDateLogged & vbTab _
& rstCalls!txtCallerName & vbTab & rstCalls!txtReason
-
May 1st, 2002, 07:30 AM
#4
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.
-
May 1st, 2002, 07:49 AM
#5
The Key for a list Item has to be a string. I have redone your code, just cut and paste
VB Code:
Private Sub Form_Load()
CreateColumns
End Sub
Private Sub CreateColumns()
With lvwCompleted
.View = lvwReport
.LabelEdit = lvwManual
.FullRowSelect = True
End With
With lvwCompleted.ColumnHeaders
.Add , "DATE", "Date Logged", 1000
.Add , "CALLER", "Caller", 2000
.Add , "REASON", "Reason", 3000
End With
With lvwOutstanding
.View = lvwReport
.LabelEdit = lvwManual
.FullRowSelect = True
End With
With lvwOutstanding.ColumnHeaders
.Add , "DATE", "Date Logged", 1000
.Add , "CALLER", "Caller", 2000
.Add , "REASON", "Reason", 3000
End With
End Sub
Private Sub Populate()
Dim rstCalls As Recordset
Dim lvwItem As ListItem
'Your code here to get records
With rstCalls
Do While Not .EOF
If .Fields("booCompleted").Value Then
Set lvwItem = lvwCompleted.ListItems.Add(, .Fields("txtID").Value & "K", .Fields("dtmDateLogged").Value)
lvwItem.SubItems(1) = .Fields("txtCallerName").Value
lvwItem.SubItems(2) = .Fields("txtReason").Value
Else
Set lvwItem = lvwOutstanding.ListItems.Add(, .Fields("txtID").Value & "K", .Fields("dtmDateLogged").Value)
lvwItem.SubItems(1) = .Fields("txtCallerName").Value
lvwItem.SubItems(2) = .Fields("txtReason").Value
End If
.MoveNext
Loop
.Close
End With
Set rstCalls = Nothing
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...
-
May 1st, 2002, 08:18 AM
#6
Thread Starter
Bouncy Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|