?
Solved

How to use a entered parameter in other places

Posted on 2011-05-04
12
Medium Priority
?
345 Views
Last Modified: 2012-05-11
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?
0
Comment
Question by:stlinae
  • 4
  • 4
  • 3
  • +1
12 Comments
 
LVL 75
ID: 35694732
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
 
LVL 11

Author Comment

by:stlinae
ID: 35694754
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
 
LVL 75

Accepted Solution

by:
DatabaseMX (Joe Anderson - Microsoft MVP, Access and Data Platform) earned 1000 total points
ID: 35694777
Sure... Here is an example:


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

mx
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 49

Assisted Solution

by:Dale Fye
Dale Fye earned 1000 total points
ID: 35694804
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
 
LVL 75
ID: 35694815
I was sort of going on "Knowing nothing about how to incorporate VB into Access, "

mx
0
 
LVL 49

Expert Comment

by:Dale Fye
ID: 35694892
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
 
LVL 75
ID: 35694905
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
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 35698802
<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
 
LVL 11

Author Comment

by:stlinae
ID: 35699527
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
 
LVL 49

Expert Comment

by:Dale Fye
ID: 35699697
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
 
LVL 11

Author Closing Comment

by:stlinae
ID: 35720981
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
 
LVL 49

Expert Comment

by:Dale Fye
ID: 35721061
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question