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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,213,506
Messages
6,114,025
Members
448,543
Latest member
MartinLarkin

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