recordset type mismatch

xsnd47

New Member
Joined
Oct 1, 2004
Messages
11
I have code:

Private Sub RemoveVialsButton_Click()

Dim frm As Form, ctl As Control
Dim varItm As Variant, sel As Variant
Dim id As Variant, rec As Recordset
Dim db As Database, recnum

Set frm = Forms!Remove
Set ctl = frm!VialSearchResultListBox

'For Each varItm In ctl.ItemsSelected
'ctl.ItemData(varItm))
'Next varItm

If Not IsNull(ctl) Then
sel = ctl.ItemsSelected(0)
Else
MsgBox ("No vials selected.")
End If

id = ctl.ItemData(sel)

Set db = CurrentDb
Set rec = db.OpenRecordset("SELECT * FROM Inventory WHERE Inventory.ID = " + id)
MsgBox (rec.RecordCount)

rec.Close

Set rec = Nothing
Set db = Nothing

End Sub

and I get a "Run-time error:'13' Type mismatch" on this line:
Set rec = db.OpenRecordset("SELECT * FROM Inventory WHERE Inventory.ID = " + id)

I'm not sure where I've gone wrong. Any suggestions?
Thanks for the help.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
hmmm

It looks like you're doing DAO not ADO.
Check your references under Tools-References and either make sure that DAO 3.6 (or whichever version of Access you have) is first OR explictly declare your objects as DAO.

Code:
Dim db = DAO.Database
Dim rec = DAO.Recordset

Is your ListBox a Multi-Select Listbox or do you have a single item?

If the above isn't it - is your form open when you excecute this code?
If it isn't open, Inventory.ID isn't populated.

Your syntax for concatenating the SQL is correct, but Access may want the & character instead of +. It shouldn't, but it might.

my edit-
Just noticed you must only have a single item selected in your listbox since you're only passing a single value to the ID WHERE Parameter. There's easier syntax to reference all this.

Code:
Private Sub RemoveVialsButton_Click() 
Dim db As DAO.Database 
Dim rec As DAO.Recordset 

Set frm = Forms!Remove 

If len(Me.SearchResultListBox.Value) >0 Then
   Set db = CurrentDb()
   Set rec = db.OpenRecordset("SELECT * FROM Inventory WHERE Inventory.ID = " & Me.SearchResultListBox.Value) 
Else
   MsgBox ("No vials selected.") 
End if

MsgBox (rec.RecordCount) 

rec.Close 

Set rec = Nothing 
Set db = Nothing 

End Sub

Hopefully I didn't typo anything or introduce a syntax error.

Mike
 
Upvote 0

Forum statistics

Threads
1,221,854
Messages
6,162,450
Members
451,765
Latest member
craigvan888

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