Library code snippets
Use unique numeric keys in the ListView control
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.
Related articles
Related discussion
-
Run-time error '91'
by converter2009 (1 replies)
-
VB6 Runtime error 381 subsript out of range Error
by Uncle (2 replies)
-
passing and reading parameters from using Shell
by jigartoliya (0 replies)
-
Convert C++ code to VB6
by mawcot (4 replies)
-
listbox scrollbar
by Dennijr (10 replies)
Related podcasts
-
Christian Beauclair
14 mai 2008 (�mission #0074) ::.Christian Beauclair: Stratégies de migration VB6 vers .NET Nous discutons avec Christian Beauclair des stratégies de migration VB6 vers .NET. Entre autres, nous discutons comment utiliser le "VB 6 Code Advisor" et le "Interop Forms Toolkit" pour ajouter la puiss...
This thread is for discussions of Use unique numeric keys in the ListView control.