Results 1 to 6 of 6
  1. #1
    Alsail77 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    17

    Criteria based on user input

    I am very new to access so please forgive me if this is an obvious fix.



    I have a query which searches the database for records and outputs all matches to a continuous form. On Form "Main" there are 2 text boxes where the user can enter the name and the address and run the search. This works.

    I added 2 more boxes to limit the search to a date range, DateMin and DateMax. This checks the "NextMaturity" field which is a calculated field which is equal to the earlier of two date fields. I got this to work with the below text as a criteria of the "NextMaturity" field in the query:

    Between [Forms]![Main].[DateMin] And [Forms]![Main].[DateMAx]

    The problem is the dates are not, and can not, be required and can have null values. Since the user will not always need to limit the search by a date I added a check box on the Form "Main", "Check40". When checked I want the query to include the date as a criteria, when not checked I do not want the date considered. I tried writing and IIF statment along the lines of IIF([Forms]![Main].[Check40]=true, Between [Forms]![Main].[DateMin] And [Forms]![Main].[DateMAx]) (as well as several variations) but this caused no records to ever be returned.

    If this is possible and anyone can help it would be appreciated

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664

  3. #3
    Alsail77 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    17
    Ssanfu - I looked at the link you provided but am at a loss as to how to carry that over to what I need to do. Essentially, if the checkbox at [Forms]![Main]![check40] = true (is checked), I want Between [Forms]![Main].[DateMin] And [Forms]![Main].[DateMAx] to be the criteria. If [Forms]![Main]![check40] = false (not checked), I do not want there to be any criteria filter for this field.

  4. #4
    Alsail77 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    17
    I think I fixed my own problem but am confused. I went into the SQL view and added the code in red below. This appears to work but I know nothing about SQL. In the design view a new column was created with the red text as the Field and <>False as a Criteria. Does this make sense? Or am I asking to break something down the road?

    WHERE (((PropData.Property) Like "*" & [Forms]![Main].[PropSearch] & "*") AND ((PropData.Address) Like "*" & [Forms]![Main].[AddressSearch] & "*") AND (IIf([Forms]![Main]![Check40]=-1, ((PropData.NextMaturity) Between [Forms]![Main].[DateMin] And [Forms]![Main].[DateMAx]),"")))

  5. #5
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    OK, here is one way:
    Create two more text boxes on the form "Main":

    Name: DateMin1
    Visible = No
    Default = #1/1/1800#

    Name: DateMax1
    Visible = No
    Default = #12/31/3000#


    Add code:
    Code:
    Option Compare Database
    Option Explicit
    
    Private Sub Check40_Click()
       If IsDate(Me.DateMin) And IsDate(Me.DateMax) Then
          If Me.Check40 = True Then
             Me.DateMin1 = Me.DateMin
             Me.DateMax1 = Me.DateMax
          Else
             Me.DateMin1 = #1/1/1800#
             Me.DateMax1 = #12/31/3000#
          End If
       End If
       Me.Requery
    End Sub
    
    Private Sub Form_Load()
    Me.Requery
    End Sub
    Change the criteria in the query to:

    Between [forms]![main].[datemin1] And [forms]![main].[datemax1]


    If the dates in Me.DateMin and Me.DateMax are valid dates, put them in Me.DateMin1 and Me.DateMax1. Otherwise use the default dates of #1/1/1800# and #12/31/3000#.
    Both Me.DateMin and Me.DateMax must be valid dates..

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by Alsail77 View Post
    I think I fixed my own problem but am confused. I went into the SQL view and added the code in red below. This appears to work but I know nothing about SQL. In the design view a new column was created with the red text as the Field and <>False as a Criteria. Does this make sense? Or am I asking to break something down the road?

    WHERE (((PropData.Property) Like "*" & [Forms]![Main].[PropSearch] & "*") AND ((PropData.Address) Like "*" & [Forms]![Main].[AddressSearch] & "*") AND (IIf([Forms]![Main]![Check40]=-1, ((PropData.NextMaturity) Between [Forms]![Main].[DateMin] And [Forms]![Main].[DateMAx]),"")))
    That is another way. But I would use

    (IIf([Forms]![Main]![Check40]=-1, ((PropData.NextMaturity) Between [Forms]![Main].[DateMin] And [Forms]![Main].[DateMAx]),TRUE)))


    So if Check40 is FALSE, the Where clause would evaluate to:

    WHERE (((PropData.Property) Like "*" & [Forms]![Main].[PropSearch] & "*") AND ((PropData.Address) Like "*" & [Forms]![Main].[AddressSearch] & "*") AND TRUE


    See if that makes any difference.

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

Similar Threads

  1. How do you select a field based on user input?
    By technesurge in forum Queries
    Replies: 5
    Last Post: 06-20-2012, 02:04 PM
  2. Selecting records based on criteria from user input
    By shabbaranks in forum Programming
    Replies: 1
    Last Post: 01-04-2012, 09:06 AM
  3. Criteria issue when using user input and > < ect
    By scotty562 in forum Queries
    Replies: 3
    Last Post: 11-11-2010, 11:08 AM
  4. Replies: 3
    Last Post: 08-25-2010, 09:03 AM
  5. Replies: 6
    Last Post: 07-22-2010, 05:53 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