vba help - sql query not working for headers which begin with numbers.

Mallesh23

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

SQL Query not working for headers which starts with Numbers and backward slashes.
arr = Array("Weekly", "Monthly", "1Yr", "O/W")

VBA Code:
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 & "$] Where (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


StateCityWeeklyMonthly1Yro/w
MaharashtraPune100200200200
MaharashtraPune200400400400
MaharashtraNagpur300600600600
GujaratAhmedabad400800800800
GujaratSurat500100010001000
KarnatakaBangalore600120012001200
DelhiNew Delhi800160016001600




Thanks
mg
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Hi Mallesh23,

maybe this codeline will work for you
Code:
   SQL = "Select Sum([" & hdr & "]) from [" & sht.Name & "$] Where (State = """ & st & """ and City = """ & ct & """)"
(additional square brackets around header).

Ciao,
Holger
 
Upvote 0
Hi, HaHoBe,

it worked ! millions of thanks. ? (y)

Can I add Range also after sht.Name . I tried below code still its working.

Sql = "Select Sum([" & hdr & "]) from [" & sht.Name & "$A1:F8] Where (State = """ & st & """ and City = """ & ct & """)"



Thanks
mg
 
Upvote 0
Hi Mallesh23,

it works, I think we got that sorted out in another thread.

I just got dizzy with clicking on all the MsgBoxes popping up. Maybe consider to change the code a bit and look at the results in the Immediate Window
VBA Code:
    Dim strMsg As String
    For i = LBound(arr) To UBound(arr)
      hdr = arr(i)
      strMsg = "State:  " & st & vbCrLf & "City:   " & ct & vbCrLf & "Header: " & hdr
      SQL = "Select Sum([" & hdr & "]) from [" & sht.Name & "$A1:F9] Where (State = """ & st & """ and City = """ & ct & """)"
      Set Rst = New ADODB.Recordset
      Rst.Open SQL, Conn
      cnt = Rst.Fields(0).Value
      Debug.Print strMsg & vbCrLf & "Sum:    " & Format(CDbl(cnt), "#,#00")
      Debug.Print vbNullString
         
      On Error Resume Next
      Rst.Close
      Set Rst = Nothing
      On Error GoTo 0
    Next i
Ciao,
Holger
 
Upvote 0
HaHoBe,

Excellent ! thanks once again understood. (y) ?


Thanks
mg
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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