+ Reply to Thread
Results 1 to 9 of 9

Excel VBA - problems with subquery

  1. #1
    Registered User
    Join Date
    03-12-2011
    Location
    Leiden, Netherlands
    MS-Off Ver
    Office 365
    Posts
    8

    Excel VBA - problems with subquery

    Hi,

    I am using ADO to retrieve records from an MS ACCESS DB in Excel. All my queries work fine but I am having problems with subqueries. My subqueries work fine in ACCESS but when I execute them via ADO I get the following error message:'"The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect".

    I hope someone can help me with the correct syntax. Alternatively I was thinking of creating a view in ACCESS but that seems not possible.

    Help would much appreciated!


    PHP Code: 
    Sub retrieve_loan_details_test()

    Dim Provider As String
    Dim Source 
    As String
    Dim sConn 
    As String
    Dim sSql 
    As String
    Dim rep_per 
    As Date
    Dim LB
    UB As Long

    Dim cnPubs 
    As ADODB.Connection
    Dim rsPubs 
    As ADODB.Recordset
    Set cnPubs 
    = New ADODB.Connection
    Set rsPubs 
    = New ADODB.Recordset

    Provider 
    Range("Provider")
    Source Range("Source")
    rep_per Range("rep_per")
    LB "12"
    UB "18"

    sConn "PROVIDER=" Provider ";"
    sConn sConn "DATA SOURCE=" Source ";Persist Security Info=False;"
    cnPubs.Open sConn

    sSql 
    "select c.bank, a.appID, a.month, b.min, sum(a.Mdeopn)"
    sSql sSql " from Monthly_balances a, (SELECT appID, min(prepdate) as min FROM Prep_input where fullpartial = 'Full' group by appID) b, Loan_details c"
    sSql sSql " where a.appID = b.appID"
    sSql sSql " and a.appID = c.appID"
    sSql sSql " and a.rep_per = #" rep_per "#"
    sSql sSql " and a.month between " LB " and " UB ""
    'sSql = sSql & " and DateDiff("m", #" & rep_per & "#", b.min) > 6"
    sSql = sSql & " Group by c.bank, a.appID, a.month,b.min"


    With rsPubs
        .ActiveConnection = cnPubs
        .Open sSql
        Range("tst").CopyFromRecordset rsPubs
        .Close
    End With

    cnPubs.Close
    Set rsPubs = Nothing
    Set cnPubs = Nothing

    End Sub 

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Excel VBA - problems with subquery

    I don't think the VBA function Datediff will be evaluated correctly in a SQL-string.

    Please Login or Register  to view this content.
    You are allowed to use the correct code tags ( using the # icon in the edit window).



  3. #3
    Registered User
    Join Date
    03-12-2011
    Location
    Leiden, Netherlands
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Excel VBA - problems with subquery

    Thanks for your swift response.

    The Datediff function does indeed not work but I think I will be able to find an SQL alternative.

    My main problem is the subquery; I have spend most of the day on searching for alternatives but didnt manage to find one.

    Do I need to change the syntax for the subquery ((SELECT appID, min(prepdate) as min FROM Prep_input where fullpartial = 'Full' group by appID) b) or is there some other way to do this?

    Thanks again for your help!

  4. #4
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Excel VBA - problems with subquery

    I would start by changing this:
    Please Login or Register  to view this content.
    to this:
    Please Login or Register  to view this content.
    since min is a function name.
    Remember what the dormouse said
    Feed your head

  5. #5
    Registered User
    Join Date
    03-12-2011
    Location
    Leiden, Netherlands
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Excel VBA - problems with subquery

    Thanks a lot, referring to a function was indeed the problem, it works now!

  6. #6
    Forum Guru romperstomper's Avatar
    Join Date
    11-04-2008
    Location
    A1
    MS-Off Ver
    Most
    Posts
    12,302

    Re: Excel VBA - problems with subquery

    Glad to help.

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save

  7. #7
    Registered User
    Join Date
    07-17-2012
    Location
    iran
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Excel VBA - problems with subquery

    Hi , I am using subquery for Retrieve Next record date . My subqueries work fine in ACCESS and takes so long for excel in line .Range("A2").CopyFromRecordset rst
    please help

    Dim cnn As ADODB.Connection
    Dim rst As ADODB.Recordset
    Dim WBCurrent As Workbook
    Dim WS As Worksheet
    'Change ScreenUpdating, Calculation, EnableEvents, ....
    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    .ScreenUpdating = False
    .EnableEvents = False
    End With
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    ActiveSheet.DisplayPageBreaks = False

    Set cnn = New ADODB.Connection

    With cnn
    .Provider = "Microsoft.ACE.OLEDB.12.0"
    .ConnectionString = "Data Source=" & ActiveWorkbook.FullName & ";" & _
    "Extended Properties=""Excel 12.0;HDR=Yes;IMEX=0;Readonly=False"";"
    .Open
    End With

    Set WBCurrent = ActiveWorkbook
    WBCurrent.Worksheets("SAP1").Rows("2:100000").Delete

    'mold_code = "002073-00"
    strSQL = "SELECT [All$A1:L].[mold code], [All$A1:L].row, [All$A1:L].date2, [All$A1:L].[machine code], " _
    & "(SELECT top 1 temp.[date2] FROM [All$A1:L] AS temp " _
    & "WHERE temp.[machine code] = [All$A1:L].[machine code] AND temp.row > [All$A1:L].row " _
    & "ORDER BY temp.row ) AS PreviousValue " _
    & "FROM [All$A1:L]; "



    Set rst = New ADODB.Recordset
    rst.Open strSQL, cnn, adOpenStatic, adLockOptimistic
    Set WS = WBCurrent.Worksheets("SAP1")
    With WS
    .Cells(1, 1) = "Machine Code"
    .Cells(1, 2) = "Last Plan"
    .Cells(1, 3) = "Mold Code"
    .Range("A2").CopyFromRecordset rst
    End With

    ActiveWindow.View = ViewMode
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlAutomatic
    End With

  8. #8
    Registered User
    Join Date
    07-17-2012
    Location
    iran
    MS-Off Ver
    Excel 2010
    Posts
    6

    Re: Excel VBA - problems with subquery

    referring up problem help me. thanks
    Last edited by vahiddastitash; 04-03-2016 at 09:39 AM.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Excel VBA - problems with subquery

    vahiddastitash

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    If posting code please use code tags, see here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1