Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old August 13th, 2010, 03:45 PM
Registered User
 
Join Date: Aug 2010
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Parameter query by Month/Year

I was thinking there has to be a way to prompt for monthly data without
using [Start Date] and [End Date].

I thought it would be a simple parameter query
that prompts the user with [Please enter Reporting Month/Year as mm/yy].

I've tried using different variation of DatePart and DateSerial as the criteria for
my date field ExpDate which is formated as mm/dd/yy.
and these Expressions: Month: Month([ExpDate]), Year: Year([ExpDate])

None are working as I had hoped.

I would be grateful for any and all suggestions.

Gil
Reply With Quote
  #2 (permalink)  
Old August 27th, 2010, 04:03 PM
Friend of Wrox
Points: 513, Level: 8
Points: 513, Level: 8 Points: 513, Level: 8 Points: 513, Level: 8
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Feb 2007
Location: Davenport, IA, USA.
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Is this for a form filter to be set when you first load a form? Instead of using a query for this I'd just set form parameters on load. To make it static (not removable by turning off or clearing a filter) you'd want to set the RecordSource instead of the filter.

Just put this code in as your Form_Load event:
Code:
Private Sub Form_Load()

'Sets Date From and Date To as filter criteria for form. Default is set to be 1 week including today.
  Dim dFromDate As Date, dToDate As Date, sTemp As String, bGood As Boolean
  
  Do While Not bGood
    sTemp = InputBox("Please Input Month/Year From (in DATE Format)", "Input Date From", _
      Format(Now() - 6, "mm/dd/yyyy") & "")
    If Not IsDate(sTemp) Then
      MsgBox "Improper Entry, needs to be In Valid DATE Format (example: 01/10/2010 for January 10, 2010)" _
        , , "Improper Format"
    Else
      bGood = True
    End If
  Loop
  dFromDate = CDate(sTemp)

bGood = False
  Do While Not bGood
    sTemp = InputBox("Please Input Month/Year From (in DATE Format)", "Input Date To", _
      Format(Now(), "mm/dd/yyyy") & "")
    If Not IsDate(sTemp) Then
      MsgBox "Improper Entry, needs to be In Valid DATE Format (example: 01/10/2010 for January 10, 2010)" _
        , , "Improper Format"
    Else
      bGood = True
    End If
  Loop
  dToDate = CDate(sTemp)

'You can make this a part of the actual RecordSource for the Form instead if desired.
'me.RecordSource = "Select * From YourTableName Where Date_Modified >= #" _
  & Format(dFromDate, "mm/dd/yyyy") & "# And Date_Modified <= #" _
  & Format(dToDate, "mm/dd/yyyy") & "#"

'But for this example I'll just set the filter property of the form and turn the filter on
  Me.Filter = "Date_Modified >= #" & Format(dFromDate, "mm/dd/yyyy") & "#" _
    & " And Date_Modified <= #" & Format(dToDate, "mm/dd/yyyy") & "#"
  Me.FilterOn = True

End Sub
Modify it to meet your specific needs. If you prefer you can spawn a child form that asks for date entry complete with calendar pop-up to 'spiff' it up instead of using 'mundane' inputboxes.
Reply With Quote
  #3 (permalink)  
Old August 27th, 2010, 04:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

I think he is asking how to allow JUST entering MM/YY *INSTEAD* of insisting on MM/DD/YYYY.

And that shouldn't be hard.
Code:
bGood = False
Do Until bGood
    dTemp = "OOPS"
    sTemp = InputBox("Please Input FROM Month/Year ", "Input Date From", _
       Format(Now(), "mm/yy") & "")
    aTemp = Split( sTemp, "/" )
    If UBound(aTemp) = 1 Then
        dTemp = DateSerial( 2000+CINT(aTemp(1)), CINT(aTemp(0)), 1 )
    End If
    If Not IsDate(dTemp) Then
      MsgBox "Improper Entry, needs to be MM/DD (e.g., 1/10 fr January 2010)" _
        , , "Improper Format"
    Else
      bGood = True
    End If
Loop
dFromDate = dTemp
 
bGood = False
Do Until bGood
    dTemp = "OOPS"
    sTemp = InputBox("Please Input TO Month/Year From ", "Input Date To", _
       Format(Now(), "mm/yy") & "")
    aTemp = Split( sTemp, "/" )
    If UBound(aTemp) = 1 Then
        ' trick to get last day of given month:
        dTemp = DateSerial( 2000+CINT(aTemp(1)), CINT(aTemp(0)) + 1, 0 )
    End If
    If Not IsDate(dTemp) Then
      MsgBox "Improper Entry, needs to be MM/DD (e.g., 1/10 fr January 2010)" _
        , , "Improper Format"
    Else
      bGood = True
    End If
Loop
dToDate = dTemp
No?

The trick with DateSerial is documented by MS. Add 1 to the month number and use 0 as the day number and it gives you the last day of the given month.
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using month & year variable to get date in range rsearing ASP.NET 2.0 Professional 2 July 5th, 2008 08:30 AM
Exact year month and days mankoti_mankoti2000 Access 1 July 3rd, 2008 10:05 AM
Month and Year to Date on a Report Mitch Access 2 February 20th, 2007 09:23 AM
get year,month,day,hour... kobystud C# 1 July 16th, 2004 04:45 PM
Month() Year() questions eon ASP.NET 1.0 and 1.1 Basics 1 October 4th, 2003 04:12 AM



All times are GMT -4. The time now is 03:41 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.