Using SQL SUM/GROUP query in VBA code

baha17

Board Regular
Joined
May 12, 2010
Messages
183
I wrote a code in excel VBA which was supposed to extract data from the SQL server. There is "tbl.id" has a relation in two tables. In my third column I get the "cash-drop" and on the forth column I need total marker for that "tbl.id" from another table.I tried this SUM/GROUP in my code but I am getting out of luck. Thanks in advance for the hand.Maybe you can understand more if you have a look my excel vba codes:
Code:
    Sub GetingItFromSnd()
    Dim DBFullName, TableName As String
    Dim TargetRange As Range
    Dim Conn As ADODB.Connection, intColIndex As Integer
    Dim cel As Range
    Dim TD As Long
    Dim qdate1 As Double
    Dim qdate2 As Double
    Dim LastRow As Long
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    On Error Resume Next
    qdate1 = Range("trddate1").Value
    qdate2 = Range("trddate2").Value
    Sheets("TblData").Range("A2:J20000").ClearContents
    Sheets("TblData").Select
    Columns("A:J").AutoFilter
    Range("A2").Select
    Selection.Activate
    Set TargetRange = Range("A2")
    Set Conn = New ADODB.Connection
     Conn.Open "driver={SQL Server};" & _
    "server=XXsql;database=Csn;"
        Set RecSet = New Recordset
    RecSet.Open "SELECT   sht_f.tbl_id, sht_f.s_openclose, sht_f.s_cashdrop,  " & _
    "sht_f.s_current-sht_f.s_total+sht_f.s_cashdrop, SUM(hist_markers_per_tbl.TotalMarkers) ,  " & _
    "replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(sht_f.tbl_id,'0',''),'1',''),'2',''),'3',''),'4',''),'5',''),'6',''),'7',''),'8',''),'9',''), " & _
    "sht_f.pt_name, sht_f.s_cashdrop/2.1, " & _
    "(sht_f.s_current-sht_f.s_total+sht_f.s_cashdrop)/2.1 FROM sht_f sht_f, Csn.dbo.hist_markers_per_tbl hist_markers_per_tbl " & _
    "WHERE hist_markers_per_tbl.game_date>='" & qdate1 & "' AND hist_markers_per_tbl.game_date<='" & qdate2 & "' sht_f.game_date>='" & qdate1 & "' " & _
    "And sht_f.game_date<='" & qdate2 & "' And sht_f.pt_id<>'" & 99 & "'GROUP BY hist_markers_per_tbl.tbl_id ORDER BY sht_f.pt_name", Conn, , , adCmdText
        TargetRange.CopyFromRecordset RecSet
    RecSet.Close
    Set RecSet = Nothing
    Conn.Close
    Set Conn = Nothing
    LastRow = Sheets("TblData").Range("A" & Sheets("TblData").Rows.Count).End(xlUp).Row
    Columns("A:J").AutoFilter
    Sheets("WPU").Select
    End Sub
 
Last edited:

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"

Forum statistics

Threads
1,223,268
Messages
6,171,099
Members
452,379
Latest member
IainTru

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