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

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
983
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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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 & "')"
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

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