VBA SQL time

vics_roo

Board Regular
Joined
Apr 3, 2015
Messages
75
Hi, i need to do samples from different worksheets by time. User input the start time and end time in format hh.hh.
I'm thinking do that by SQL request. So I do the following code

Code:
    ...
    Dim startLT As Double
    Dim endLT As Double
    startLT = ThisWorkbook.Worksheets(1).Range("D7").Value
    endLT = ThisWorkbook.Worksheets(1).Range("E7").Value
   
    'converting to hh:mm:ss
    Dim startTime As Date
    Dim endTime As Date
    startTime = CDate(startLT / 24)
    endTime = CDate(endLT / 24)

    Dim CN As New ADODB.connection         
    CN.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
                            "Data Source=" & fName & _
                            ";Extended Properties=""Excel 12.0;HDR=YES"""
    CN.Open
    
    'names of worksheets to processing
    Dim names
    names = Array("000", "001", "010", "011", "100", "101", "110")
    Dim i As Long
    Dim sSQL As String
    Dim tableName As String
    Dim sqlTime As String
    sqlTime = "SQLTime"            'column header
    For i = 1 To UBound(names, 1)
        
        ActiveWorkbook.Worksheets("Summary").Activate
        
        'clean active worksheet for testing
        ActiveSheet.Cells.Select
        Selection.Delete xlUp
        Selection.Clear
        
        Set RS = Nothing
        Set RS = CreateObject("ADODB.Recordset")
        
        tableName = "[" & CStr(names(i)) & "$]"
        sSQL = "SELECT * FROM " & tableName & " WHERE " & sqlTime & " BETWEEN '" & startTime & "' AND '" & endTime & "'"
       
        'locals window show me the following 
        'sSQL ="SELECT * FROM [001$] WHERE SQLTime BETWEEN '3:00:00' AND '8:00:00'"
        'startTime=#3:00:00#        '<- type is Date
        'endtime=#8:00:00#          '<- type is Date


        MsgBox sSQL
        RS.Open sSQL, CN              ' <<<< ERROR IS HERE
        ActiveWorkbook.Worksheets("Summary").Activate
        Range("A1").CopyFromRecordset RS
    Next i
...

I've got error Run-time error '-2147217913 (80040e07). Type missmatch...'

I guess that it hapenned becouse in SQL expression I used time.

If SQL expression will be
sSQL = "SELECT * FROM " & tableName
my code is working almost good and time column will be holds invalid values.

How to fix that ?

Thanks
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

Forum statistics

Threads
1,214,601
Messages
6,120,467
Members
448,965
Latest member
grijken

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