Pass Variable in SQL string

gdspeare

Board Regular
Joined
Oct 8, 2002
Messages
198
Hello, I am trying to pass a variable called rep into a sql string and i keep getting an error that my parameter does not have a value.

Anyone ever dealt with this?

rep = ComboBox1.Value
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
' connect to the Access database
Set cn = New ADODB.Connection
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; " & _
"Data Source=C:\Documents and Settings\speared\My Documents\db1.mdb"
' open a recordset
Set rs = New ADODB.Recordset
rs.Open "(SELECT name FROM [David] where salesrep=rep)", cn, adOpenKeyset, adLockOptimistic, adCmdTable
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
Hi gdspeare

You are in fact passing the text "rep" instead of the value of the variable rep.

Please try:

rs.Open "(SELECT name FROM [David] where salesrep=" & rep & ")", cn ...

Hope this helps
PGC
 
Upvote 0
gdspeare

If salesrep is a text field you'll probably need to wrap it in single quotes too.
Code:
rs.Open "(SELECT name FROM [David] where salesrep='" & rep & "')", cn ...
 
Upvote 0

Forum statistics

Threads
1,217,451
Messages
6,136,714
Members
450,025
Latest member
Beginner52

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