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

Mallesh23

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

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

HaHoBe

Well-known Member
Joined
Jan 24, 2003
Messages
513
Office Version
  1. 2013
Platform
  1. Windows
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
 

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
961
Office Version
  1. 2010
Platform
  1. Windows
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
 

HaHoBe

Well-known Member
Joined
Jan 24, 2003
Messages
513
Office Version
  1. 2013
Platform
  1. Windows
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
 

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
961
Office Version
  1. 2010
Platform
  1. Windows
HaHoBe,

Excellent ! thanks once again understood. (y) 🕺


Thanks
mg
 

Forum statistics

Threads
1,148,108
Messages
5,744,874
Members
423,907
Latest member
zerocool88

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