Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31

    How to handle null in SQL statement

    I have an SQL statement in VBA and I am concatenating into it criteria from a combo box. The row source of the Combo Box is:

    Code:
    SELECT Categories.ID, Categories.Category FROM Categories UNION Select Null as AllChoice , "(All)" as Bogus From Categories
    ORDER BY Categories.Category;
    The purpose of All is to be able to choose All Categories as a criteria for the query. This works when I run the query in Access.

    For my next application, I am forced to convert this query into VBA code with an SQL statement. The chunk of the VB statement giving me issues is:

    Code:
    WHERE (((Categories.ID) = " & [Forms]![frmCreateTriviaNight]![cboCategory1] & ")) Or (((" & [Forms]![frmCreateTriviaNight]![cboCategory1] & ") Is Null))
    The VBA version of this query works fine when I have a specific Category chosen. For example if Category 22 is chosen then the statement becomes:



    Code:
    WHERE (((Categories.ID) = 22)) Or (((22) Is Null))
    which works. But if the Category combo box has (All) selected, then the statement becomes:

    Code:
    WHERE (((Categories.ID) = )) Or ((() Is Null))

    Which throws an error for invalid syntax. How can I deal with this null value in my VBA code?

  2. #2
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    WHERE tblTableTitle.FieldTitle Is Null

    also you can use parenthesis

    so ((tblTableTitle.FieldTitle) is null)

  3. #3
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by ultimateguy View Post
    I have an SQL statement in VBA and I am concatenating into it criteria from a combo box. The row source of the Combo Box is:

    Code:
    WHERE (((Categories.ID) = )) Or ((() Is Null))

    Which throws an error for invalid syntax. How can I deal with this null value in my VBA code?
    well here you are saying catagories table field ID is equal to well... nothing

    then

    or nothing is null.

    Usually just reading it for what it is helps determine the problem.

    so you have to handle a NULL result.

    Since you are receiving null results from the combo box you have to accommodate for it.

    One way is using the nz() function. So normally nz(forms!frmName!combo, something happen here if it is null)

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Is this code behind frmCreateTriviaNight? Use Me alias to shorten code.

    The criteria following the Or is nonsense.

    ID field is number type so LIKE operator and wildcard won't work.

    No alternative value with Nz() will return all records. So try an IIf().

    WHERE Categories.ID " & IIf(IsNull(Me.cboCategory1), ">0", "=" & Me.cboCategory1) & ";"


    What are you doing with this SQL - opening a recordset object?
    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.

  5. #5
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    Thanks, the IIf worked. I am using this to change the SQL statement in an existing query.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Why do you need to change query? Why not just apply filter to form or report?
    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.

  7. #7
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    I need to generate multiple sets of data based on the same query. But the user can specify different criteria for each set of data. I have comboboxes for each set. So rather than create tons of pre-made queries, I am using VBA code to cycle through the sets and change the criteria accordingly for each new set of data.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Users really should not interact with tables and queries.

    So again - why not apply filter to form or report? Form or report uses the query (without filter) as RecordSource. Code applies filter to form or report.

    DoCmd.OpenReport "report name", , , "ID" & IIf(IsNull(Me.cboCategory1), ">0", "=" & Me.cboCategory1)
    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.

  9. #9
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    I didn't know I could do it that way. I need to use cboCategory1, cboSubCategory1, cboType1, and cboDifficulty1 as criteria. cboCategory1 and cboSubCategory1 are combo boxes with column(0) as the ID, and cboType and cboDifficulty have a text string for column(0). I also need to do a SELECT TOP with number of values specified by the user in txtQuestionsPerRound, and those questions have to be randomly sorted.

    Is it possible to do all of this with a filter in this fashion?

    I am doing this to generate one round of trivia. My end goal is to generate a report containing multiple rounds of trivia. Would the best way to do this be to have a report with a subreport for each round? Can I filter the subreports in this fashion?

  10. #10
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    Only the main report can have the filter passed with the DoCmd.OpenReport. Here is example of code to construct filter criteria http://allenbrowne.com/ser-62code.html

    Subreports are synchronized with the main report by the Master/Child Links properties. Subreport RecordSource can be an SQL statement with dynamic parameters. http://www.datapigtechnologies.com/f...mtoreport.html
    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.

  11. #11
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    Thanks. Is there any way to swap out a single record in a report, leaving the others the same?

  12. #12
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,626
    I don't know what that means. Records are retrieved by query. What does 'swap out' mean?
    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.

  13. #13
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    confused myself...

    swap?

    hide?

  14. #14
    ultimateguy is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2014
    Posts
    31
    Sorry, should have been more clear. Currently the report has a record source equal to an SQL statement with dynamic parameters based on the user's combo box selections. The statement produces random Questions and Answers.

    So for example, the user generates one round of ten questions and is then shown the report displaying this information. Suppose that the user decides one of those questions is not suitable. In this situation, the user needs to have one of these options:

    1) Replace this single question with another random question based on the same criteria used to generate the other questions (while keeping the others unchanged)
    2) Replace this single question with another question from the Questions table that is specifically chosen by the user
    3) Replace this single question with a new question that is not currently in the database. The user inputs a new question into the Questions table, and then this question takes the place of the unsuitable question in the report.

    I suspect that these actions are not possible given the structure of the report?

  15. #15
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 8 Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    So the user doesn't select the questions, the questions are selected for them based on the combo boxes they have updated.

    i.e. group1 gives you 20 random group 1 questions? group 2 gives you 20 random group 2 questions? and if you don't like one of those questions you want the user to be able to remove it....?

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 11-19-2012, 05:42 PM
  2. Access VBA statement to check for Null entries
    By tim_tims33 in forum Access
    Replies: 4
    Last Post: 01-24-2012, 12:19 PM
  3. Replies: 12
    Last Post: 12-11-2011, 05:04 PM
  4. Best way to handle this Web Database
    By tucker1003 in forum Database Design
    Replies: 8
    Last Post: 03-18-2011, 12:14 PM
  5. What is the best way to handle photos?
    By TundraMonkey in forum Database Design
    Replies: 2
    Last Post: 08-12-2009, 10: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