Results 1 to 11 of 11
  1. #1
    roberto21 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    89

    Pass through query question

    Experimenting with access and sql server, I ran into a problem. I have a query that works ok in access

    Code:
    SELECT PrimaNota.Ricevuta, PrimaNota.[Conto N], Pianodeiconti2.Descrizione, PrimaNota.Data, PrimaNota.Entrate, PrimaNota.Uscite, PrimaNota.Note, Pianodeiconti2.LIVELLO_1, Pianodeiconti2.LIVELLO_2, Pianodeiconti2.INDICE, PrimaNota.MESE, Pianodeiconti2.CODICE_REGIONE_AVERE, Pianodeiconti2.CODICE_REGIONE_DARE, Pianodeiconti2.RAGGRUPPAMENTO1FROM Pianodeiconti2 INNER JOIN PrimaNota ON Pianodeiconti2.[Conto N] = PrimaNota.[Conto N]
    WHERE (((PrimaNota.Data) BETWEEN #31/12/1023# AND #01/01/2025#))
    ORDER BY PrimaNota.Data, PrimaNota.[Conto N];
    For the sake of testing and learning something, I created an identical query using the create->passThrough in access, deliberately leaving the query in access syntax (BETWEEN, dates wrapped in #) .



    Click image for larger version. 

Name:	Screenshot 2025-03-27 095211.png 
Views:	27 
Size:	33.4 KB 
ID:	52881
    So, I was expecting the query to fail if executed on the server. But the query worked fine also in this case. What does this mean? That the query is executed in access anyway? And if so, what am I missing in order to have a real pass through query?

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,551
    I would have honestly expected the data to be in mm/dd/yyyy format ?
    Though I do not use SQL server BE I now use
    Code:
    'Public Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Needed for dates in queries as Access expects USA format.
    Public Const strcJetDate = "\#yyyy-mm-dd\#"  'Needed for dates in queries as Access expects USA but will accept ISO format.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    roberto21 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    89
    Handling of dates is always painful. Anyhow, I am in Italy, so I use the european standard dd/mm/yyyy, In any case, this is not the problem: the query works fine in both cases, but it should fail whan handled by the server which, as far as I know, expects T-SQL syntax: Between is not recognized, dates should be enclosed in quotes and in yyyy/mm/dd format. How come does it work? I am missing something here...

  4. #4
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,551
    I am in the UK and use the same format, except for when I am using Access.
    No idea, as I said, I have not used SQL BE.

    According to ChatGPT, Between is allowed. Dates are in quotes as you stated though. Perhaps Access translates it on sending?
    Code:
    SELECT 
        PrimaNota.Ricevuta, 
        PrimaNota.[Conto N], 
        Pianodeiconti2.Descrizione, 
        PrimaNota.Data, 
        PrimaNota.Entrate, 
        PrimaNota.Uscite, 
        PrimaNota.Note, 
        Pianodeiconti2.LIVELLO_1, 
        Pianodeiconti2.LIVELLO_2, 
        Pianodeiconti2.INDICE, 
        PrimaNota.MESE, 
        Pianodeiconti2.CODICE_REGIONE_AVERE, 
        Pianodeiconti2.CODICE_REGIONE_DARE, 
        Pianodeiconti2.RAGGRUPPAMENTO1
    FROM 
        Pianodeiconti2 
    INNER JOIN 
        PrimaNota 
    ON 
        Pianodeiconti2.[Conto N] = PrimaNota.[Conto N]
    WHERE 
        PrimaNota.Data BETWEEN '1023-12-31' AND '2025-01-01'
    ORDER BY 
        PrimaNota.Data, PrimaNota.[Conto N];
    Changes:

    1. Date Format:

      • Access uses #MM/DD/YYYY#, whereas SQL Server uses 'YYYY-MM-DD' for date literals.

    2. Aliasing Fix:

      • Fixed RAGGRUPPAMENTO1FROM, which appears to have a typo (RAGGRUPPAMENTO1 FROM).

    3. BETWEEN Clause:

      • The dates are kept as strings in 'YYYY-MM-DD' format for T-SQL compatibility.

    This should work in SQL Server. Let me know if you need any refinements! ��
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  5. #5
    Minty is offline VIP
    Windows 11 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Most of my work these days is with Azure SQL backends and Access FE 's.
    This query should fail SQL Server,
    Code:
    SELECT PrimaNota.Ricevuta, PrimaNota.[Conto N], Pianodeiconti2.Descrizione, PrimaNota.Data, PrimaNota.Entrate, PrimaNota.Uscite, PrimaNota.Note, Pianodeiconti2.LIVELLO_1, Pianodeiconti2.LIVELLO_2, Pianodeiconti2.INDICE, PrimaNota.MESE, Pianodeiconti2.CODICE_REGIONE_AVERE, Pianodeiconti2.CODICE_REGIONE_DARE, Pianodeiconti2.RAGGRUPPAMENTO1FROM Pianodeiconti2 INNER JOIN PrimaNota ON Pianodeiconti2.[Conto N] = PrimaNota.[Conto N]
    WHERE (((PrimaNota.Data) BETWEEN #31/12/1023# AND #01/01/2025#)) ORDER BY PrimaNota.Data, PrimaNota.[Conto N];
    as it won't understand the Date delimiters being # and won't understand the dd/mm/yyyy date format either.

    I always use 'yyyy-MM-dd' format for Sql Server.
    I use the function below to take normal access dates or dates as string
    Code:
    Function ServerDate(varDate As Variant) As String
        'Purpose:    Return a delimited string in the date format used natively by SQL Server and Azure SQL.
    
        'Argument:   A date/time value.
        'Note:       Returns just the date format if the argument has no time component,
        '                or a date/time format if it does.
        'Author:     Adjusted from a Allen Browne function for user with SQL server. allen@allenbrowne.com, June 2006.
        If IsDate(varDate) Then
            If DateValue(varDate) = varDate Then
                ServerDate = Format$(varDate, "'yyyy-mm-dd'")
            Else
                ServerDate = Format$(varDate, "'yyyy-mm-dd hh:nn:ss'")
            End If
        End If
    End Function
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  6. #6
    roberto21 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    89
    Thanks a lot for your kind interest, but maybe I was not clear. I know that the query (as amended to reflect T-SQL syntax) works fine in sql server; I can run it with SSMS without any trouble.
    What I do not understand is this: I define the query in access as pass-through query, meaning (correct me if I am wrong) that the query is passed to the server "as is". BUT,
    if I leave the date in the format #..#, I expect he server to complain about invalid syntax. So one might conclude that the query, in spite of the fact of being passthru, gets executed in access. Why? Or, is the conclusion wrong? That's why I think I am missing something here...

  7. #7
    Minty is offline VIP
    Windows 11 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    It shouldn't work. It doesn't get past the SQL Driver
    See the below
    Click image for larger version. 

Name:	SQL_DATE_Error_In_Pass_Thru.png 
Views:	23 
Size:	14.5 KB 
ID:	52882

    Changing the format doesn't work either:
    Code:
    SELECT * FROM tb_Calendar 
    
    WHere FullDate > #2024-12-31#
    This does work
    Code:
    SELECT * FROM tb_Calendar 
    
    WHere FullDate >'2024-12-31'
    So to answer your question - I think there is something not right in how you are running or using the pass through?
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    roberto21 is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Apr 2019
    Posts
    89
    Frankly, I don't know what to say. If you look at my first post, you can see that I have a pass-through query in access, clearly identified (I circled it in red). This query when executed should go straight to the server that, because of the #..# syntax of the dates, should complain of invalid syntax. Instead the query is executed without errors. It seems very simple, and in any case I followed the steps outlined here
    https://support.microsoft.com/en-us/...2-6dac62532a42
    So it appears that the query is executed in access (I don't believe access modifies the query before forwarding it), but the situation is so simple that, again, I must be missing something trivial.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 11 Access 2021
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    What is the connection string in the query ODBC Connect Str property?

    I have SQLServerExpress backend on my laptop. Connection string:
    ODBC;DRIVER=SQL Server;SERVER=localhost\SQLEXPRESS01;DATABASE=Umpi resSSE;Trusted_Connection=True

    This works for me: WHERE GameDate BETWEEN '2023-10-1' AND '2023-12-1'

    # and / fail
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    jojowhite's Avatar
    jojowhite is online now Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    432
    what you just created is a query from your Linked table.
    If you ever need to create a pass through query, you
    must supply the ODBC connection in it's Property first.
    Click image for larger version. 

Name:	proper.png 
Views:	18 
Size:	16.0 KB 
ID:	52887

  11. #11
    Minty is offline VIP
    Windows 11 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,157
    Stupid question No1.

    Did you save the query before running it?
    If not, it will remain as whatever type of query it was originally, unlike Access queries they aren't updated until they are saved.

    So, if your workflow was, create normal Access Query, view it, change to a pass through, Run it - nothing will have changed at that point.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 01-24-2022, 12:47 PM
  2. Pass a variable to a Pass Through Query
    By violeta_barajas in forum Access
    Replies: 2
    Last Post: 01-26-2017, 07:59 PM
  3. Replies: 5
    Last Post: 04-27-2015, 02:40 PM
  4. Replies: 7
    Last Post: 03-11-2015, 12:48 PM
  5. Replies: 0
    Last Post: 03-19-2006, 11:52 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums