Delete checked item (one or more) from listview I write this code.

  Dim x As Long
                For x = LVEmp.ListItems.Count To 1 Step -1
            If LVEmp.ListItems(x).Checked Then
                con.Execute "DELETE FROM stock WHERE [SrNo] = '" & LVEmp.ListItems(x).SubItems(1) & "'"

                LVEmp.ListItems.Remove x
            End If
        Next

        con.Close
        Set con = Nothing

but it show error massage:
run time error -2147217913 (80040e07)
data type mismatch in criteria expression.

Dani AI

Generated

The error means the DELETE is comparing the SrNo column (numeric) to a string value. As suggested, you must pass a numeric value to the SQL (or use a typed parameter) — wrapping a number in single quotes makes SQL treat it as text and produces the data type mismatch.

A clarification about the ListView loop: in classic VB6 the ListItems collection is 1-based and Remove accepts an index, so iterating from Count down to 1 is the correct pattern for safe removal. appears to be mixing VB.NET semantics (zero-based Items and RemoveAt). If you are on VB.NET, use zero-based indexing and Items.RemoveAt; if you are on VB6, your reverse loop is fine.

Practical, safer approach (VB6 + ADODB): validate the SubItem value, use a parameterized Command so the driver enforces numeric type, and only remove the ListItem after the DB delete succeeds. Example pattern:

Dim cmd As ADODB.Command
Dim p As ADODB.Parameter
Set cmd = New ADODB.Command
cmd.ActiveConnection = con
cmd.CommandText = "DELETE FROM stock WHERE [SrNo] = ?"
cmd.CommandType = adCmdText
Set p = cmd.CreateParameter("pSrNo", adInteger, adParamInput)
cmd.Parameters.Append p

For idx = LVEmp.ListItems.Count To 1 Step -1
  raw = Trim(LVEmp.ListItems(idx).SubItems(1))
  If IsNumeric(raw) Then
    cmd.Parameters(0).Value = CLng(raw)
    cmd.Execute
    LVEmp.ListItems.Remove idx
  End If
Next

Quick checklist:

  • Verify the SubItem really contains the SrNo (use Debug.Print or MsgBox).
  • Use IsNumeric before CLng/CInt to avoid runtime conversion errors.
  • Prefer parameterized commands to avoid type issues and injection.
  • Wrap multiple deletes in a transaction for atomicity if needed.
  • Use the correct indexing rules for VB6 (1-based) vs VB.NET (0-based).

This addresses the type mismatch and also explains the indexing confusion flagged by while following ’s correct advice to use a numeric value.

Recommended Answers

All 4 Replies

It looks to me that the query is loking to compare numbers but you're giving it a string. Try converting the subitem value to a number and calling the query with that.

LVEmp.ListItems.Remove x
here x should be an object not a numerical value which is ListItem or Item.

For x = LVEmp.ListItems.Count To 1 Step -1
and also the loop should be

    For x = (LVEmp.ListItems.Count-1) To 0 Step -1

In database it was numeric. so what problem?

In database it was numeric. so what problem?

How would you call an object by a numerical value ?
Whats ever it would be in your database, to remove an selected item from listview you must have to pick up the selected listitem object not the listitem index value.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.