using Query Database Query in Excel 2003

vranjit138

Board Regular
Joined
Dec 18, 2006
Messages
146
Office Version
  1. 365
Platform
  1. Windows
Hi,

Iam trying to use a variable, so that the user can select a particular text and use it in the database query

But i find that string b is not being taken up by the code -- (((data1.Gender=", "'b'))")

Can anyone tell me how to pass a variable to this code of Database query in excel

Dim a As String
Dim b As String
a = Sheets("Sheet2").Range("A4").Text
b = Sheets("Sheet2").Range("b4").Text
With ActiveSheet.QueryTables.Add(Connection:= _
"ODBC;DSN=Excel Files;DBQ=C:\REPORTS\Team1.xls;DefaultDir=C:\REPORTS;DriverId=790;MaxBufferSize=2048;PageTimeout=5;" _
, Destination:=Range("F4"))
.CommandText = Array( _
"SELECT data1.Name, data1.Gender, Lotus.`Lotus Notes Long Name`" & Chr(13) & "" & Chr(10) & "FROM `C:\REPORTS\Team1`.data1 data1, `C:\REPORTS\Team2`.Lotus Lotus" & Chr(13) & "" & Chr(10) & "WHERE data1.`Employee ID` = Lotus.`Employee ID` AND ((data1.Gender=", "'b'))")
.Name = "Query from Excel Files"
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Perhaps you mean:

Code:
"SELECT data1.Name, data1.Gender, Lotus.`Lotus Notes Long Name` FROM `C:\REPORTS\Team1`.data1 data1, `C:\REPORTS\Team2`.Lotus Lotus WHERE data1.`Employee ID` = Lotus.`Employee ID` AND data1.Gender = '" & b & "'"

we assume Gender field is of String Type.
 
Upvote 0

Forum statistics

Threads
1,224,505
Messages
6,179,152
Members
452,891
Latest member
JUSTOUTOFMYREACH

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