How to use a entered parameter in other places

Access 2003,
Have a parameter set up for dates in a query, >=[DateFrom] & <=[DateTo]. Knowing nothing about how to incorporate VB into Access, is there a way to 'store' the entered date and use them in a report? If so, how?
LVL 11
stlinaeAsked:
Who is Participating?
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Connect With a Mentor Database ArchitectCommented:
Sure... Here is an example:


SELECT Table1.*
FROM Table1
WHERE (((Table1.YourDate) Between [Forms]![YourFormName]![txtDateForm] And [Forms]![YourFormName]![txtDateTo]));

mx
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Not directly from a Query, but ... you can use a Form with two text boxes to hold that From/To date, then reference those text boxes from where ever,  including a Query and/or Report.

mx
0
 
stlinaeAuthor Commented:
Okay, so just create a form with the 2 text boxes, that part I can do. Then how do I get those fields for use in the query and the report?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Dale FyeConnect With a Mentor Commented:
I also use functions for this purpose, which allows me to use them in a variety of ways in forms, reports, and code modules.  The nice thing about this is I can update the function value at any time, usually in the AfterUpdate event of a combo box, but also in a query.  To update it in the AfterUpdate event of a combo box, I'd simply do:

Private Sub cbo_StartDate_AfterUpdate

    Call fnStartDate(me.cbo_StartDate)

End Sub

But you don't have to set it beforehand.  You can force the function to ask you for the start date by setting ForceNew to True.  So in your query, you might write:

SELECT * FROM yourTable
WHERE [DateField] >= fnStartDate(NULL, True)
AND [DateField] < DateAdd("d", 1, fnEndDate(NULL, True))

I've included the fnStartDate function, you can figure out fnEndDate on your own.

Public Function fnStartDate(Optional StartDate As Variant = Null, Optional ForceNew As Boolean = False) As Date

    Static myStartDate As Variant

    If IsNull(StartDate) = False Then
        'If the user passes a StartDate, set the value of myStartDate
        myStartDate = StartDate
    ElseIf IsEmpty(myStartDate) Or ForceNew Then
        'If myStartDate is not already set (it's static, so it keeps its value between function calls)
        'or the user sets ForceNew to True, then the function asks for a new value, and continues
        'asking until a valid value is passed
        Do
            myStartDate = InputBox("Start date?")
            If IsDate(myStartDate) Then Exit Do
        Loop
    End If
   
    fnStartDate = myStartDate
   
End Function
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
I was sort of going on "Knowing nothing about how to incorporate VB into Access, "

mx
0
 
Dale FyeCommented:
Joe,

You gotta start somewhere.  If I had picked up on that part, I probably would have added instructions on how to create a code module.

stlinae,

If you want to try my route, and don't know how to create a public code module, just post back and I'll provide those instructions.
0
 
DatabaseMX (Joe Anderson - Microsoft Access MVP)Database ArchitectCommented:
Dale ... well, your route is certainly what I use also.  But, I was just to keep it simple.  And ... good to start with the basics also ... and then move on the the code approach.

It's all good ...

mx
0
 
Jeffrey CoachmanMIS LiasonCommented:
<No Points wanted>

You can also delete the parameters from the query and use the form values to open the report directly, filtered for these values...

Private Sub btnOpenReport_Click()
    'Validation for start and end Dates
    If Me.txtStart = "" Or IsNull(Me.txtStart) Then
        MsgBox "You must enter an Start Date."
        Me.txtStart.SetFocus
        Exit Sub
    End If

    If Me.txtEnd = "" Or IsNull(Me.txtEnd) Then
        MsgBox "You must enter an End Date."
        Me.txtEnd.SetFocus
        Exit Sub
    End If
   
    'Open the report filtered for the textbox date values
    DoCmd.OpenReport "rptOrders", acViewPreview, , "OrderDate Between " & "#" & Me.txtStart & "#" & " AND " & "#" & Me.txtEnd & "#"

End Sub



sample attached.

What we have presented here are the 3 popular ways of doing this.

There is no right or wrong way to do this, no one technique is "better" than another.

It all depends on what you feel comfortable with now, as you may be just starting out.
I, like the previous experts, will provide you with any help you may need in implementing the approach...
All we ask is that you give it a try on your own first, then report back with any issues you may be having...

;-)

JeffCoachman

Database171.mdb
0
 
stlinaeAuthor Commented:
Thank you all for your help. I'm going to try all the suggestions to see what works best for me at this time. That being said, I'm sure I'm going to need help with the code module as I'm not even sure where to start.

0
 
Dale FyeCommented:
Createing a code module is really quite simple.

1.  Open Access.
2.  Press CTRL-G, this will open up the Visual Basic for Applications Editor.  It should look something like Image #1(VBE.jpg).  If not, click View, then select the Project Explorer and Properties Window.

3.  Right click in the upper left quad and select Insert -> Module.  This will create a new module which will probably show up in the bottom left quad with a name of Module1.  That is fine, leave it like that for now, but eventually you will want to start naming modules and putting subroutines and functions in them based on their functionality.
4.  Copy the code I provided, including the lines that start with "Public Function" and end with "End Function"
5.  Go back to the VB Editor window and paste the code you just copied into the upper right quadrant
6.  Click the Save icon on the toolbar.  You have just created your first code module.

Since you are new to this, I'll provide the code for fnEndDate() as well:

Public Function fnEndDate(Optional EndDate As Variant = Null, Optional ForceNew As Boolean = False) As Date

    Static myEndDate As Variant

    If IsNull(EndDate) = False Then
        'If the user passes a EndDate, set the value of myEndDate
        myEndDate = EndDate
    ElseIf IsEmpty(myEndDate) Or ForceNew Then
        'If myEndDate is not already set (it's static, so it keeps its value between function calls)
        'or the user sets ForceNew to True, then the function asks for a new value, and continues
        'asking until a valid value is passed
        Do
            myEndDate = InputBox("End date?")
            If IsDate(myEndDate) Then Exit Do
        Loop
    End If
   
    fnEndDate = myEndDate
   
End Function
0
 
stlinaeAuthor Commented:
Thanks to all of you for helping. Jeff, I only kept you out of the points per your statement.
Using a Form to hold the dates was certainly easier for me, but I did manage to get all methods to work.

Thanks again, ~S
0
 
Dale FyeCommented:
Glad I could help.

The reason I like prefer using the functions over the forms is that I can set the value of the functions in the immediate window, and then write and test queries without having the form open.  

I have to admit, I started out using the controls on a form method, so if that is what you are most comfortable with, go with it.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.