Results 1 to 17 of 17

Thread: Writing SQL Queries Against Virtual Tables in Excel VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #12
    VBAX Regular NateO's Avatar
    Joined
    Jun 2004
    Location
    Minneapolis, MN
    Posts
    90
    Location
    It should work if:

    1) Your name is valid.
    2) You are connecting to the correct workbook. The named range must be in the workbook that the code lies in if you're going to connect to ThisWorkbook.

    The following, like a fox in the hen house, has chicken for supper every time:

    Sub testeroo()
    Dim cn As Object, rs As Object
    Dim clcMde As Long
    Let clcMde = Application.Calculation
    With Application
        .ScreenUpdating = False
        .Calculation = xlCalculationManual
    End With
    Set cn = CreateObject("ADODB.Connection")
    cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source='" & _
    ThisWorkbook.FullName & "';Extended Properties=Excel 8.0;"
    Set rs = CreateObject("ADODB.Recordset")
    With rs
        Set .ActiveConnection = cn
        .Source = "Select TimeStamp, Value, Last6MinAvg  From " & _
        "Query_from_MS_Access_Database Where Last6MinAvg >= 10"
        .Open , , 3, 3
        Sheets(2).Cells.ClearContents
        Sheets(2).[a1].CopyFromRecordset rs
        .Close
    End With
    cn.Close
    Set rs = Nothing: Set cn = Nothing
    With Application
        .ScreenUpdating = True
        .Calculation = clcMde
    End With
    End Sub
    I have attached an example. So, I'm not quite sure what you have done at this point.
    Last edited by Aussiebear; 04-22-2023 at 12:08 AM. Reason: Adjusted the code tags
    Regards,
    Nate Oliver

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •