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

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,887
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,353
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,176,315
Messages
5,902,432
Members
434,975
Latest member
snoski

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