Often when you store records in a database, you may create a unique
ID number to go along with that record. In Visual Basic, you can use
the ListView or TreeView controls to display these records. It's
only natural that you might want to use each records numeric ID
value as each item's Key value in the control. However, if you've
ever tried to add a number key to either of these controls, then
you know that Visual Basic generates an invalid key error.
This error occurs because the ListView control won't accept all-
numeric keys-even if they're technically strings. As a work-around,
if you want to add all-numeric Ids to the control, you'll need to
append an alpha character to the end of it. The following code
snippet illustrates how you might go about doing so. This code
assumes you're using a ListView in report mode with two columns
total, and that you've created a recordset object.
If Not Rst.EOF Then
To retrieve the true Id value from the Key property, simply use the
With Rst
.MoveFirst
Do
Set itm =
ListView1.ListItems.Add(Key:=.Fields _
("txtID")
& "K", Text:=.Fields("sintRank"))
itm.SubItems(1) = .Fields("txtCounty")
.MoveNext
Loop Until .EOF
End With
End If
Val() function to strip the character back out, as in
MsgBox "Key: " & itm.Key & vbNewLine _
& "ID value: " & Val(itm.Key)
If you were to put an alpha character at the beginning of the ID value,
the Val() function would return 0.
Use unique numeric keys in the ListView control
By ElementK Journals, published on 14 Jul 2001
| Filed in
You might also like...
VB 6 forum discussion
-
CorelDRAW VBA: cdrTraceLineDrawing FAILS, producing single linear path instead of Centerline trace?
by dancemanj (0 replies)
-
client/server application using activex
by beautifulheart (0 replies)
-
System Error &H8007007E. The specifed module could not be found.
by swiftsafe (5 replies)
-
Invitation to take part in an academic research study
by researchlab (0 replies)
-
Send SMS with SMPP
by mmahmoud (0 replies)
VB 6 podcasts
-
Stack Overflow Podcast: Podcast #45 – Keeping it Sharp
Published 7 years ago, running time 0h54m
Our guest this week is Eric Lippert – language architect extraordinaire and famous for all his work at Microsoft in developing their languages Eric joined Microsoft right out of college and was originally working on VB It’s time for everyone’s favorite game: Name the Worst Feature of that Microso.
Comments