Results 1 to 3 of 3
  1. #1
    project2501 is offline Novice
    Windows 10 Access 2016
    Join Date
    Sep 2017
    Posts
    1

    Where [field]=variable

    I am trying to create a query with the WHERE criteria equaling a variable defined in vba. The variable in vba is set to equal the .value of a combo box. However, if the combo box value is null, then it gives me an error saying it's an invalid use of null. How do I code the variable so if the combo box value is null, then the variables value is one that will return all records for the related field in the query? Or how do I set the combo box to include an option for all records (which would be set as it's default value)?



    I know how to achieve this outcome by defining it all in the query, but I am trying to reduce the query size by turning WHERE criteria into vba variables to escape "too complex" errors

    Thank you for any advice/tips offered

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,524
    you need 2 queries:
    1 without the null combo, where the sql doesnt use it,
    qsDataAllDepts
    select * from table

    1 with the combo as criteria:
    qsData1Dept
    select * from table where [dept] = forms!myForm!cboDept

    then to open the query:
    Code:
    sub btnOpen_click()
    if IsNull(cboDept) then
       docmd.openquery "qsAllDepts"
    else
       docmd.openquery "qs1Dept"
    endif
    end sub

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    You tried a dynamic parameterized query? Why would you get 'too complex' error?

    Is the field a text type? Try LIKE and wildcard in query dynamic parameter.

    WHERE fieldname LIKE Forms!formname!comboboxname & "*"

    Or use the query as RecordSource for form or report and apply filter to the form or report.

    DoCmd.OpenReport "report name", , acViewPreview, "fieldname LIKE '" & Me.combobox & "*'"
    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.

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

Similar Threads

  1. form with variable field number
    By FRSPA in forum Access
    Replies: 10
    Last Post: 05-04-2011, 05:29 AM
  2. Pass Variable to Table Field with VBA
    By dgj32784 in forum Programming
    Replies: 3
    Last Post: 03-28-2011, 09:36 PM
  3. VBA DCount() with variable field name
    By drh in forum Programming
    Replies: 2
    Last Post: 07-13-2010, 12:36 AM
  4. Using a field as a variable in a query
    By Shadow2K9 in forum Queries
    Replies: 1
    Last Post: 06-10-2010, 07:38 PM
  5. binding report field to variable
    By frente in forum Reports
    Replies: 1
    Last Post: 10-19-2009, 02:48 AM

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