How to get/edit selected rows in listbox?

Hello,

After searching i have found the following test codes to selecting multiple rows in a listbox :

Dim ids() As String
Dim sqL As String
Dim combined As String

for rowIndex as Integer = 0 to PurchasesList.ListCount - 1
  ids.Append Str(PurchasesList.RowTag(rowIndex))
next

combined = Join(ids, ",")

sql = "DELETE FROM addressbook WHERE NRef IN" + combined

MsgBox(sql)

The problem is within combined variable, somehow in my part it’s always empty. Also in my database the rowid is starting with “1” instead of “0”.

How to approach my condition?, to selecting a/couple rows and delete/edit it in database?, from above code, deleting the database records seems easy enough but how to edit multiple records in the database at once? Need looping?.

I am also have put the code above in the event CellClick, is it “CellClick” the appropriate listbox event for multiple selecting row?

You need to use the debugger and step through your code. Perhaps the content can’t be coerced to a string.

if you had a list box and the rowtags were integers

That code would assemble a string that looks like this:

DELETE FROM addressbook WHERE NRef IN3,7,8,35,76

Which is invalid syntax.

1/ No space after the IN
2/ no brackets or quotes around the list (quotes may not be needed, but casting is usually automatic)

So, if combined is empty, you dont have any rowtags, or purchaseslist is empty
Fx that, and then you need this:

[code]Dim combined As String
combined = " ("

for rowIndex as Integer = 0 to PurchasesList.ListCount - 1
combined = combined + “’” + Str(PurchasesList.RowTag(rowIndex)) + “’,”
ids.Append Str(PurchasesList.RowTag(rowIndex))
next
combined = combined + “’’)”[/code]

[code]Public Function ToString(extends anIntegerArray() as Integer, withBetweenChar as String = “,”) as String
dim i as integer
dim ch as string
dim lines() as String

ch=""
if UBound( anIntegerArray)>=0 then
for i=0 to UBound(anIntegerArray)
lines.Append str(anIntegerArray(i))
next
end if
Return Join( lines, withBetweenChar)

End Function
[/code]

then you can use

sql = "DELETE FROM addressbook WHERE NRef IN (" + ids.toString+")"

Thanks for all peoples who is replying, but coming from background of non-native english speaker, can someone explained the real/descriptive purposes of listbox.rowtag?

Tried @Jeff Tullin code but combined still empty :
ids :

combined :

listbox displayed at runtime :

You put things in to take them back out. If you didn’t put anything into the RowTag, there’s nothing there for the code to take back out.

Is ‘Purchase ID’ the ID you are seeking?

If so, instead of rowtag, use cell(row, column)

[code]Dim combined As String
combined = " ("

for rowIndex as Integer = 0 to PurchasesList.ListCount - 1
combined = combined + “’” + PurchasesList.cell(rowIndex,1) + “’,”
next
combined = combined + “’’)”[/code]

RowTag is a place where you can put some information that will not be displayed on your listbox cells, like a secret box where you can put something and then your code can ask for it.

Sometimes with databases your primary key ID is a value that increase as each record is added and maybe is not used for other things. I have a database with primary key ID, Date and Currency, my listbox shows the Date and the Value but not the ID, I store the ID within RowTag to know what record to edit when I change the Currency.

If you don’t write anything to RowTag, then the RowTag will be empty.

If your database primary key ID is ProductID, then you don’t need RowTag and just use ProductID.

@Alberto DePoo & @Tim Parnell finally it’s clear about rowtag

@Jeff Tullin
Thanks for the sampe code, it works! :slight_smile: