Problem with SQL in VBA

czl103

New Member
Joined
Sep 27, 2018
Messages
19
After below code runs, the result is 310, however the result of VBA code should be 321.3, all the digits after the decimal points were ignored in calculation. why does this happens?
Pls download the 2 files from below site, and run the xlsm file after downloading....
Code:
[URL="https://www.mrexcel.com/forum/redirect-to/?redirect=https%3A%2F%2Fjmp.sh%2F71yC8Ti"]https://jmp.sh/71yC8Ti[/URL]

Thanks in advance.

PHP:
Dim cnn As Object, SQL As String, s As String, p As String, f As String
Set cnn = CreateObject("ADODB.Connection")
p = ThisWorkbook.Path & "\"f = Dir(p & "*.CSV")
cnn.Open "Provider=Microsoft.ace.OLEDB.16.0;Extended Properties='text;FMT=Delimited(,);hdr=YES';Data Source=" & p
Do While f <> ""   SQL = SQL & " SELECT int(Date) AS T, Amount 
FROM [" & f & "] 
UNION ALL "    f = Dir()LoopSQL = Left(SQL, Len(SQL) - 11)
SQL = "SELECT T,SUM(Amount) FROM (" & SQL & ") 
GROUP BY T"Range("a2:e" & Rows.Count) = ""[a2].CopyFromRecordset 
cnn.Execute(SQL)
cnn.CloseSet 
cnn = Nothing
Dim R%R = Cells(Rows.Count, "a").End(xlUp).Row[a1].Offset(R, 0) = "Sum"[b1].Offset(R, 0).Resize(1, 4) = "=sum(b2:b" & R & ")"
 
Last edited by a moderator:

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Now I found one method to solve this problem, change the format of the first row number to "0.00", then the result is ok.
But this is not clever way, is there any good idea?
 
Upvote 0
I got an error on the line
Code:
[a2].CopyFromRecordset cnn.Execute(SQL)
undefined function "subtotal" in expression


Where does subtotal(Amount) refer to?
The column Amount in your csv file is empty when I import the csv file. Is this correct?
When I import your csv data I get this. Is this the correct data?

[Table="width:, class:grid"][tr][td]Row\Col[/td][td]
A​
[/td][td]
B​
[/td][td]
C​
[/td][td]
D​
[/td][td]
E​
[/td][td]
F​
[/td][td]
G​
[/td][/tr][tr][td]
1​
[/td][td]Order No[/td][td]Date[/td][td]Customer 1 No[/td][td]Customer 1 Name[/td][td]Customer 2 No[/td][td]Customer 2 Name[/td][td] Amount [/td][/tr]
[tr][td]
2​
[/td][td]
1,11E+02​
[/td][td]
11-1-2017 21:00​
[/td][td]
2,22E+02​
[/td][td]aaa[/td][td]
3,33E+02​
[/td][td]bbb[/td][td] - [/td][/tr]
[tr][td]
3​
[/td][td]
1,11E+02​
[/td][td]
11-1-2017 20:00​
[/td][td]
2,22E+02​
[/td][td]aaa[/td][td]
3,33E+02​
[/td][td]bbb[/td][td] - [/td][/tr]
[tr][td]
4​
[/td][td]
1,11E+02​
[/td][td]
11-1-2017 20:00​
[/td][td]
2,22E+02​
[/td][td]aaa[/td][td]
3,33E+02​
[/td][td]bbb[/td][td] - [/td][/tr]
[tr][td]
5​
[/td][td]
1,11E+02​
[/td][td]
11-1-2017 20:00​
[/td][td]
2,22E+02​
[/td][td]aaa[/td][td]
3,33E+02​
[/td][td]bbb[/td][td] - [/td][/tr]
[tr][td]
6​
[/td][td]
1,11E+02​
[/td][td]
11-1-2017 20:00​
[/td][td]
2,22E+02​
[/td][td]aaa[/td][td]
3,33E+02​
[/td][td]bbb[/td][td] - [/td][/tr]
[tr][td]
7​
[/td][td]
1,11E+02​
[/td][td]
11-1-2017 20:00​
[/td][td]
2,22E+02​
[/td][td]aaa[/td][td]
3,33E+02​
[/td][td]bbb[/td][td] - [/td][/tr]
[tr][td]
8​
[/td][td]
1,11E+02​
[/td][td]
11-1-2017 20:00​
[/td][td]
2,22E+02​
[/td][td]aaa[/td][td]
3,33E+02​
[/td][td]bbb[/td][td] - [/td][/tr]
[tr][td]
9​
[/td][td]
1,11E+02​
[/td][td]
11-1-2017 20:00​
[/td][td]
2,22E+02​
[/td][td]aaa[/td][td]
3,33E+02​
[/td][td]bbb[/td][td] - [/td][/tr]
[/table]
 
Upvote 0
You can use a Schema.ini file to specify data types for the columns, like this:

Code:
Sub Sum()
    Dim cnn As Object, SQL$, s$, p$, f$
    Set cnn = CreateObject("ADODB.Connection")
    p = ThisWorkbook.Path & "\"
    CreateSchemaFile p
    f = Dir(p & "*.CSV")
    cnn.Open "Provider=Microsoft.ace.OLEDB.12.0;Extended Properties='text;';Data Source=" & p

    Do While f <> ""
        SQL = SQL & " SELECT int(Date) AS T, Amount FROM [" & f & "] UNION ALL "
        f = Dir()
    Loop
    SQL = Left(SQL, Len(SQL) - 11)
    SQL = "SELECT T,sum(Amount) FROM (" & SQL & ") GROUP BY T"
    [A2:E1048576] = ""
    [a2].CopyFromRecordset cnn.Execute(SQL)
    cnn.Close
    Set cnn = Nothing
    Dim R%
    R = [A1048576].End(3).Row
    [a1].Offset(R, 0) = "Sum"
    [b1].Offset(R, 0).Resize(1, 4) = "=sum(b2:b" & R & ")"
End Sub
Sub CreateSchemaFile(FilePath As String)
    If Right$(FilePath, 1) <> "\" Then FilePath = FilePath & "\"

    Dim IntFileHandleLog As Integer
    IntFileHandleLog = FreeFile
    
    On Error Resume Next
    Kill FilePath
    On Error GoTo 0
    
    Open FilePath & "schema.ini" For Output As #IntFileHandleLog
    
    Dim FileName As String
    FileName = Dir(FilePath & "*.csv")
    
    If FileName <> vbNullString Then
        Do
            Print #IntFileHandleLog, "[" & FileName & "]"
            Print #IntFileHandleLog, "ColNameHeader=True"
            Print #IntFileHandleLog, "MaxScanRows = 0"
            Print #IntFileHandleLog, "Col1=" & """Order No"" Text"
            Print #IntFileHandleLog, "Col2=" & """Date"" DateTime"
            Print #IntFileHandleLog, "Col3=" & """Customer 1 No"" Text"
            Print #IntFileHandleLog, "Col4=" & """Customer 1 Name"" Text"
            Print #IntFileHandleLog, "Col5=" & """Customer 2 No"" Text"
            Print #IntFileHandleLog, "Col6=" & """Customer 2 Name"" Text"
            Print #IntFileHandleLog, "Col7=" & """Amount"" Currency"
            FileName = Dir
        Loop While FileName <> vbNullString
    End If
    Close #IntFileHandleLog
End Sub
 
Last edited:
Upvote 0
Pls refer to response with red text
Where does subtotal(Amount) refer to?
the G column

The column Amount in your csv file is empty when I import the csv file. Is this correct?
Yes,correct, some non-zero number do exist below these rows

When I import your csv data I get this. Is this the correct data?
yes correct data

Thanks

 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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