runtime error '13': type mismatch

Pookiemeister

Well-known Member
Joined
Jan 6, 2012
Messages
563
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
On my form, I have a listbox that returns a list of selected items from a listbox. strReturns equals the selected items position in the listbox, and strReturn equals the selected .itemdata(). The error occurs on the line of code below. Thank you.

VBA Code:
strReturn = strReturns & ", " & lstBx.ItemData(strReturns)

VBA Code:
Function lbxRPItems() As String
   'Returns a list of items in the listbox
   Dim lstBx As ListBox
   Dim lngItems, lngItem As Long
   Dim strReturns, strReturn As String
   Dim varItem As Variant
  
   strReturn = ""
   strReturns = ""
   Set lstBx = Me!lstResponsiblePerson
   lngItems = lstBx.ItemsSelected.Count
   If lngItems > 0 Then
      For lngItem = 0 To lngItems - 1
         If lngItem = 0 Then
            strReturns = CStr(lstBx.ItemsSelected(lngItem))
            strReturn = lstBx.ItemData(strReturns)
         Else
            strReturns = strReturns & ", " & CStr(lstBx.ItemsSelected(lngItem))
           strReturn = strReturns & ", " & lstBx.ItemData(strReturns)
         End If
      Next
      MsgBox strReturn 'Test purposes to view output
   End If
   Set lstBx = Nothing
  
   lbxRPItems = strReturn
End Function
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
I'm not familiar with Access, but the basic principles will be the same as Excel, strReturns is a text string (although it has been declared as a variant), the part in brackets at the end of the line should be a valid index position (number).
 
Upvote 0
This might not be it but anything that you don't explicitly declare is a variant - i.e. strReturns. Sometimes that results in unexpected values or data types.
Secondly, you're trying to get the item data from row n in a listbox but IIRC, you have to pass a number to ItemData as lstMyList.ItemData(5) yet you're passing a string (well, in some cases, actually a variant because of not being declared). You need to use
Dim lngItems As Long, lngItem As Long
not
Dim lngItems, lngItem As Long

in all cases, plus don't pass a string to ItemData
 
Upvote 0
in all cases, plus don't pass a string to ItemData
So how can I retrieve the name of the selected item, so I can put that in my table? Thank you.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top