Results 1 to 13 of 13
  1. #1
    Gee is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Posts
    31

    Open Report with Date Parameters

    I need the following report to open with date parameters.
    I have the following code, but it doesn't quite work.
    When an item is chosen from Modl (a list box) a box pops up asking for LowPop, then another for Start Year and then another for End Year.


    Those last two aren't doing what they should. They should restrice the [Date] field to between the years entered as start and end.
    I would like to put it in the "OpenReport" line, but don't think that's going to work.
    Any help would be appreciated.
    Code:
    Private Sub Command27_Click()
     Dim varItm As Variant
        Dim ModelWhere As String
        Dim strQuery
        Dim LowPop As String
        Dim SDate As Date
        Dim EDate As Date
        Dim qdf As DAO.QueryDef
       
        If SysCmd(acSysCmdGetObjectState, acQuery, "CompactSUMQry") = acObjStateOpen Then
        DoCmd.Close acQuery, "CompactSUMQry"
        End If
            
        Set qdf = CurrentDb.QueryDefs("CompactSUMQry")
            
        LowPop = InputBox("Please Enter Minimum Population", "Population")
        SDate = InputBox("Enter Start Year")
        EDate = InputBox("Enter End Year")
        
        strQuery = "SELECT * FROM dbo_BAMtbl WHERE ((dbo_BAMtbl.Pop)>=" & LowPop & " AND "
       
        For Each varItm In Me.Modl.ItemsSelected
            If ModelWhere = "" Then
               ModelWhere = "[dbo_BAMtbl].[Model]=" & _
               Chr(34) & Me.Modl.Column(0, varItm) & Chr(34)
            Else
               ModelWhere = ModelWhere & " OR [dbo_BAMtbl].[Model]=" & _
               Chr(34) & Me.Modl.Column(0, varItm) & Chr(34)
            End If
        Next varItm
        
        strQuery = strQuery & "(" & ModelWhere & "));"
        
        qdf.SQL = strQuery
          
    DoCmd.OpenReport "SUMrptCmpct", acViewPreview, , WhereCondition:="(" & [Date] & "  >=" & SDate & " And " & "<= EDate&"")"
        Set qdf = Nothing
    End Sub

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    What is the data type of the date field (bad name by the way, as it can get confused with the Date() function)? It looks like you're comparing a date to just a year. If not, you have to repeat the field name:

    FieldName >= Whatever And FieldName <= Whatever
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    Gee is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Posts
    31
    Yes, I agree with the [Date] field, I would have chosen something else, but I didn't build the datasource.
    The dbo_BAMtbl.Date is a date field, mm/dd/yyyy, I need the input in the inputboxes to be yyyy.
    I understand that I need to say "FieldName >= Whatever And FieldName <= Whatever", I just have no clue on how to write it in the WhereCondition of the OpenReport Command.
    The FieldName is dbo_BAMtbl.Date, but I need to steer it to the SDate and EDate.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    If you just want to compare the year try:


    DoCmd.OpenReport "SUMrptCmpct", acViewPreview, , "Year([Date]) >=" & SDate & " And Year([Date]) <= " & EDate
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    Gee is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Posts
    31
    It did not work..It gave me Run-time Error '3075'
    Syntax error (missing operator) in query expression 'Year([Date]) >=12:00:00AM And Year([Date])<=12:00:00Am

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    It doesn't look like the user entered a year for either variable.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Oh, and change your variable declarations to Integer, since you don't really want a date input.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  8. #8
    Gee is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Posts
    31
    I entered it myself...2011 and 2011.

  9. #9
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Did you see:

    Quote Originally Posted by pbaldy View Post
    Oh, and change your variable declarations to Integer, since you don't really want a date input.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  10. #10
    Gee is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Posts
    31
    I don't know how to do that.
    Can you tell me?

  11. #11
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Change these to Integer:


    Dim SDate As Date
    Dim EDate As Date
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  12. #12
    Gee is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Feb 2013
    Posts
    31
    YES!!
    Thank you SO much!

  13. #13
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,641
    Excellent, glad it worked!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 5
    Last Post: 12-04-2012, 02:46 AM
  2. Adding date parameters into report
    By djfuego in forum Reports
    Replies: 4
    Last Post: 05-31-2012, 07:18 AM
  3. Open Report filtered by Date in ListBox
    By TinaCa in forum Programming
    Replies: 1
    Last Post: 03-06-2012, 02:29 PM
  4. Open form from parameters
    By funkygoorilla in forum Forms
    Replies: 5
    Last Post: 01-01-2012, 09:17 PM
  5. Open a report with a date condition
    By Grooz13 in forum Reports
    Replies: 3
    Last Post: 08-02-2010, 02:35 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