vba help - wild card in SQL Query

Mallesh23

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

I am fetching data using in sql query
Can i use Wildcard for header. headers name might change little.


Can i use Week* for Weekly
Can i use Month* for Monthly
Can i use State* for States

how to use wild card in below query.
Sql = "Select Sum([" & hdr & "]) from [" & sht.Name & "$A1:G9] Where (Currency = ""IND"" OR Currency = ""EUR"")AND(State = """ & st & """ and City = """ & ct & """)"

Dim arr As Variant
arr = Array("Weekly", "Monthly", "1Yr", "O/W")
st = "Maharashtra"
ct = "Pune"


Sub CopyData()
Dim Conn As New ADODB.Connection
Dim Rst As 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
Dim hdr As String
'hdr = "State"
Dim cnt As Long
Dim st As String
Dim ct As String
Dim ShtName As String

Dim sh As Worksheet
Set sht = ThisWorkbook.Worksheets("Sheet1")

ShtName = sht.Name

Dim arr As Variant
arr = Array("Weekly", "Monthly", "1Yr", "O/W")
st = "Maharashtra"
ct = "Pune"

For I = LBound(arr) To UBound(arr)

hdr = arr(I)
MsgBox hdr

Sql = "Select Sum([" & hdr & "]) from [" & sht.Name & "$A1:G9] Where (Currency = ""IND"" OR Currency = ""EUR"")AND(State = """ & st & """ and City = """ & ct & """)"

Set Rst = New ADODB.Recordset
Rst.Open Sql, Conn
cnt = Rst.Fields(0).Value
MsgBox cnt

On Error Resume Next
Rst.Close
Set Rst = Nothing
On Error GoTo 0
Next I
End Sub

Below is a sample Table.

StatesCityWeeklyMonthly1Yro/wCurrency
MaharashtraPune100200400800USD
MaharashtraPune200400400200EUR
MaharashtraNagpur300600600600IND
MaharashtraPune2345IND
GujaratSurat500100010001000IND
KarnatakaBangalore600120012001200IND
DelhiNew Delhi800160016001600IND



Thanks
mg
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Hi mrshl9898,

It worked ,Thanks for your help. (y)



Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,213,546
Messages
6,114,256
Members
448,558
Latest member
aivin

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