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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
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
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
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 ...
 

Forum statistics

Threads
1,136,268
Messages
5,674,732
Members
419,523
Latest member
Urnovio

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
Top