vba help - how to replace string with variable in a query.

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
935
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

how to pass variable in a sql Query string.
I want to replace ""USD"" with variable str_currency

working.....
Sql = "Select Sum(Weekly) from [Sheet1$] Where (Currency = ""USD"")" ' This code works

Not working
Sql = "Select Sum(Weekly) from [Sheet1$] Where (Currency = " & str_currency & ")" 'This is not working how to pass variable


Below code which I was trying.
VBA Code:
Sub CopyData()
    Dim Conn As New ADODB.Connection
    Dim Rst As New ADODB.Recordset
    
    FilePath = ThisWorkbook.FullName
    connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & FilePath & _
                    ";Extended Properties=""Excel 12.0 Macro;HDR=YES"";"
    
    Conn.Open connstr
   ' Sql = "Select * from [Sheet1$] order by State Desc"
   'Sql = "Select State,city from [Sheet1$] "
  'Sql = "SELECT Sum(city)FROM [Sheet1$] where (State = ""Gujarat"")"



Dim str_currency As String
str_currency = USD

Sql = "Select Sum(Weekly) from [Sheet1$] Where (Currency = ""USD"")"  ' This code works
Sql = "Select Sum(Weekly) from [Sheet1$] Where (Currency = " & str_currency & " )" 'This is not working how to pass variable

    
    Rst.Open Sql, Conn
    Dim cnt As Double
    
   cnt = Rst.Fields(0).Value
   MsgBox cnt
   
End Sub

Thanks
mg
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
37,146
Office Version
  1. 365
  2. 2019
  3. 2016
  4. 2010
Platform
  1. Windows
  2. MacOS
You're missing the quotes:

Code:
Sql = "Select Sum(Weekly) from [Sheet1$] Where (Currency = """ & str_currency & """)"

though I think most languages would allow a single quote:

Code:
Sql = "Select Sum(Weekly) from [Sheet1$] Where (Currency = '" & str_currency & "')"
 

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
935
Office Version
  1. 2010
Platform
  1. Windows
Hi Rory,

Thanks a lot both the solution worked. 🕺 (y)


Thanks
mg
 

Forum statistics

Threads
1,136,954
Messages
5,678,761
Members
419,782
Latest member
gc75150

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