+ Reply to Thread
Results 1 to 19 of 19

SQL - How to return all records if parameter is blank

  1. #1
    Forum Contributor
    Join Date
    02-14-2012
    Location
    Warrington
    MS-Off Ver
    Excel 365
    Posts
    377

    SQL - How to return all records if parameter is blank

    I want TMSWR.EMPREF = ? (Which is linked to a cell in Excel) parameter to be ignored if the cell is left blank. Is this possible?

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    01-25-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    434

    Re: SQL - How to return all records if parameter is blank

    Quote Originally Posted by benoj2005 View Post
    I want TMSWR.EMPREF = ? (Which is linked to a cell in Excel) parameter to be ignored if the cell is left blank. Is this possible?

    Please Login or Register  to view this content.
    I suggest putting that condition into a string, if it's blank it won't add anything to your string that holding the main SQL query, but if it's not blank it will add it to the end of your query, something like this

    Please Login or Register  to view this content.

  3. #3
    Forum Contributor
    Join Date
    02-14-2012
    Location
    Warrington
    MS-Off Ver
    Excel 365
    Posts
    377

    Re: SQL - How to return all records if parameter is blank

    That makes sense, I've tried this:

    Please Login or Register  to view this content.
    but I get these two error messages, any ideas?

    Errors.jpg

  4. #4
    Forum Contributor
    Join Date
    01-25-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    434

    Re: SQL - How to return all records if parameter is blank

    is your SQL query in a string in VBA ?

  5. #5
    Forum Contributor
    Join Date
    02-14-2012
    Location
    Warrington
    MS-Off Ver
    Excel 365
    Posts
    377

    Re: SQL - How to return all records if parameter is blank

    Oh no, it's in the connection properties command text. Using Microsoft query via ODBC. If that's the right terminology

  6. #6
    Forum Contributor
    Join Date
    01-25-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    434

    Re: SQL - How to return all records if parameter is blank

    what about creating two procedure, one for when you want the additional condition, and one without ?

    Please Login or Register  to view this content.
    I'm not that familiar with the addon reference library you're using

  7. #7
    Forum Contributor
    Join Date
    01-25-2014
    Location
    UK
    MS-Off Ver
    Excel 2010
    Posts
    434

    Re: SQL - How to return all records if parameter is blank

    so you're just replacing your current sql query with the above IF statement, and creating the two new subs() somewhere else in the module

  8. #8
    Forum Contributor
    Join Date
    02-14-2012
    Location
    Warrington
    MS-Off Ver
    Excel 365
    Posts
    377

    Re: SQL - How to return all records if parameter is blank

    The code makes sense, apart from
    Please Login or Register  to view this content.
    needs to be
    Please Login or Register  to view this content.
    because ? is a parameter in an excel sheet, if they don't specify an EMPREF than it needs to return all EMPREF's.

    Still getting the same errors using this:

    connection properties.jpg


    and code is here for reference
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    02-14-2012
    Location
    Warrington
    MS-Off Ver
    Excel 365
    Posts
    377

    Re: SQL - How to return all records if parameter is blank

    Surprised no one knows how to do this

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

    Re: SQL - How to return all records if parameter is blank

    Try using something like:

    AND ((TMSWR.EMPREF = ?) OR (' '=? & ' '))

    and make sure the additional parameter is linked to the same cell as the prior one.
    Remember what the dormouse said
    Feed your head

  11. #11
    Forum Contributor
    Join Date
    02-14-2012
    Location
    Warrington
    MS-Off Ver
    Excel 365
    Posts
    377

    Re: SQL - How to return all records if parameter is blank

    Quote Originally Posted by romperstomper View Post
    Try using something like:

    AND ((TMSWR.EMPREF = ?) OR (' '=? & ' '))

    and make sure the additional parameter is linked to the same cell as the prior one.
    I like your thinking! So I've added that and linked both paramerter 3 and 4 to the same cell, but, now I get an error message about "string data, right truncation". See this screen shot > error.jpg

    Please Login or Register  to view this content.

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

    Re: SQL - How to return all records if parameter is blank

    No idea I'm afraid. I don't have a lot of experience with SQL Server. (it works with Access)

  13. #13
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: SQL - How to return all records if parameter is blank

    Does this work?
    PHP Code: 
    SELECT
        TMSWR
    .EMPREF,
        
    TMSWR.PROCDATE,
        
    TMSWR.REASON,
        
    OD.DEPARTMENT

    FROM    Mfdata
    .Mfuser.OD OD,
            
    Mfdata.Mfuser.TMSWR TMSWR

    WHERE 
        OD
    .EMPREF TMSWR.EMPREF 
        
    AND ((OD.DEPARTMENT IN (05101114)) AND (TMSWR.PROCDATE BETWEEN ? AND ?) AND (TMSWR.REASON ''))
        AND ((
    TMSWR.EMPREF COALESCE(?,TMSWR.EMPREF)) ) 

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

    Re: SQL - How to return all records if parameter is blank

    I like your thinking! (but can't rep you again yet)

  15. #15
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: SQL - How to return all records if parameter is blank

    Thanks, but I've no idea if it will work in Excel

    Also the performance might be a bit pants since the query isn't sargable, but I don't know an easier way of doing it

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

    Re: SQL - How to return all records if parameter is blank

    Nor do I, but it's still a good thought.

  17. #17
    Forum Contributor
    Join Date
    02-14-2012
    Location
    Warrington
    MS-Off Ver
    Excel 365
    Posts
    377

    Re: SQL - How to return all records if parameter is blank

    Quote Originally Posted by Kyle123 View Post
    Does this work?
    PHP Code: 
    SELECT
        TMSWR
    .EMPREF,
        
    TMSWR.PROCDATE,
        
    TMSWR.REASON,
        
    OD.DEPARTMENT

    FROM    Mfdata
    .Mfuser.OD OD,
            
    Mfdata.Mfuser.TMSWR TMSWR

    WHERE 
        OD
    .EMPREF TMSWR.EMPREF 
        
    AND ((OD.DEPARTMENT IN (05101114)) AND (TMSWR.PROCDATE BETWEEN ? AND ?) AND (TMSWR.REASON ''))
        AND ((
    TMSWR.EMPREF COALESCE(?,TMSWR.EMPREF)) ) 
    No error messages is the good news, works if I specify a value in the linked cell to the parameter for this
    Please Login or Register  to view this content.
    , however, if no value is specified in the cell then no results are returned. It needs to ignore this parameter and return all results in this case. I feel like we're getting closer, but not quite there!

  18. #18
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,238

    Re: SQL - How to return all records if parameter is blank

    What happens if you type null into the cell?

  19. #19
    Forum Contributor
    Join Date
    02-14-2012
    Location
    Warrington
    MS-Off Ver
    Excel 365
    Posts
    377

    Re: SQL - How to return all records if parameter is blank

    Quote Originally Posted by Kyle123 View Post
    What happens if you type null into the cell?
    Still returns no records.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. return all value unless a parameter is enter
    By zafirah in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-25-2013, 10:55 PM
  2. CopyFromRecordset does return only 1000 records while recordset have 4000 records
    By KRUSHNAT in forum Access Programming / VBA / Macros
    Replies: 1
    Last Post: 10-05-2012, 05:47 PM
  3. [SOLVED] Need to return value of cell based on parameter.
    By odoc in forum Excel General
    Replies: 2
    Last Post: 04-09-2012, 05:02 PM
  4. Replies: 3
    Last Post: 01-06-2012, 11:16 PM
  5. Return message if parameter not exists
    By krish T in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-08-2010, 11:01 AM

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