473,394 Members | 2,052 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Date Range on a report??

smithj14
I have a form to enter start and end dates then select a worker name to filter a report. This all works fine and when the report is open in preview mode it shows the date range in the txtboxes on the report header, but when I print the report the value for these txtboxes are #Name?

Here is the code
Date Range Form:

Private Sub cmdViewErrorReport_Click()
Dim stDocName As String
Dim stDocName2 As String
Dim stWhere As String
Dim stLinkCriteria As String
Dim lngLen As Long

stDocName = "Error Report"
stDocName2 = "Error Report - Date Range"
stLinkCriteria = " [ReviewDate] Between " & CLng(Nz(Me.txtBeginDate, txtEndDate)) & " And " & CLng(Me.txtEndDate)

Me.Filter = stLinkCriteria
Me.FilterOn = Len(stLinkCriteria)

If Not IsNull(Me.cboWorker) Then
stWhere = stWhere & "([Worker] = """ & Me.cboWorker & """)"
DoCmd.OpenReport stDocName, acViewPreview, stLinkCriteria, stWhere
End If

DoCmd.Close acForm, stDocName2
DoCmd.RunMacro "PrintErrorReport"
DoCmd.Close acReport, "Error Report"
End Sub


The controlsource property of the txtboxes on my report:

txtBeginDate:
=Forms.[Error Report - Date Range].txtBeginDate

txtEndDate:
=Forms.[Error Report - Date Range].txtEndDate

I know the code isn't pretty, I am still testing and have not added error checking yet but all is working except this part.

The only thing I haven't tried is changing the name of the report. Maybe the spaces and dash need to removed?

Help
Oct 6 '08 #1
12 3237
nico5038
3,080 Expert 2GB
Normally I use a form to enter the date(s) and/or additional fields and pass these as the filter parameter when starting the report.
Another option is to have the parameters in the WHERE clause like e.g.
[Begin date: ]
as this will ask for a value when the report is started.

Finally when you have the fields on a report (which effectively should be "output only"), the you would have to refer to:
=Reports.[Error Report - Date Range].txtEndDate

But I wouldn't use input textfields on a report, but on a form...

Nic;o)
Oct 6 '08 #2
Sorry, I might have not been clear enough. The code I posted was from the click event from a seperate unbound form that has txtboxes to enter dates and a combobox to select the worker then filter the report based on these criteria.

Then the report auto prints and closes displaying all the correctly filtered data on the printed report.

I just need the entered date values from the form to print out on the report.
The txtboxes on the report are not used for input just to display the date values.

If you need more info let me know.
Oct 7 '08 #3
nico5038
3,080 Expert 2GB
Strange, as it should work as you code the fields.
Perhaps the formname with the embedded "-" sign causes trouble as some Access versions aren't "happy" with special characters in form and/or field names.

Can you retry this with a form just named like "frmReportStartEnd".

Nic;o)
Oct 7 '08 #4
youmike
69
Nico's advice regarding embedding "-" is good. Access just doesn't like that sort of thing and you get most reliable (and fastest) performance if all object names are restricted to using A-Z (upper and lower case) and 1-9. Using a capital at the start of each word in the name as Nico suggests makes names easier to read.

I'd also be inclined to redesign stWhere so that it doesn't have successive double quotes. I tend to favour passing values of controls to global constants, which in this case would be set via the after update event of the combo box. In this case, you'd declare a string constant, say gcstrWorkerName. I use the prefix gcstr for a global string, because it is helpful when debugging. I also tend to put all such global declarations in a VBA module which I call modConstants.

If you adopt this approach, your code to build stWhere (which would need to be declared as global) would look something like
If Not IsNull(Me.cboWorker) Then
gcstWhere = stWhere & " AND Worker = " & gcstrWorkerName
End if.

Hope this makes sense
Oct 7 '08 #5
Ok, thank both of you for the point in the right direction.
I now have it working printing the date range on the report. I created a new form and used a different approach to the coding. The code I was using would only work if both dates were entered. Now I can enter a start date, an end date or both, and all works fine. I think my problem was with the selection of the worker. When I added that part of code it did not work again, so I removed it.

Here is the code I used:

Private Sub cmdErrorReport_Click()
Dim stReport As String
Dim stForm As String
Dim stMacro As String
Dim stDateField As String
Dim stWhere As String
Dim lngView As Long
Const stDateFormat = "\#mm\/dd\/yyyy\#"

stReport = "ErrorReport"
stForm = "ErrorDateRange"
stMacro = "PrintErrorReport"
stDateField = "[ReviewDate]"
lngView = acViewPreview

If IsDate(Me.txtStartDate) Then
stWhere = "(" & stDateField & " >= " & Format(Me.txtStartDate, stDateFormat) & ")"
End If

If IsDate(Me.txtEndDate) Then
If stWhere <> vbNullString Then
stWhere = stWhere & " AND "
End If
stWhere = stWhere & "(" & stDateField & " < " & Format(Me.txtEndDate + 1, stDateFormat) & ")"
End If

If CurrentProject.AllReports(stReport).IsLoaded Then
DoCmd.Close acReport, stReport
End If

DoCmd.OpenReport stReport, lngView, , stWhere

DoCmd.RunMacro stMacro
DoCmd.Close acForm, stForm
DoCmd.Close acReport, stReport

End Sub

However i still need to select a worker name from a combo box and have the report filter by the entered dates and the worker name. This part is a must as we view the report based on the workers errors and needs to be specific to each worker.

Can you guys help with this or should I start another post with that part of problem?
Oct 7 '08 #6
nico5038
3,080 Expert 2GB
You'll need to code all combinations of dates and worker filled and/or empty.
Would look something like:
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdErrorReport_Click()
  2.  
  3. Dim stWhere As String
  4.  
  5. stWhere = ""
  6.  
  7. If IsDate(Me.txtStartDate) and IsDate(Me.txtEndDate) Then
  8.    stWhere = "([ReviewDate] between #" & Format(Me.txtStartDate, "mm-dd-yyyy") & "and & Format(Me.txtEndDate, "mm-dd-yyyy") & ")"
  9. elseif IsDate(Me.txtStartDate) then
  10.    stWhere = "([ReviewDate] >=" & Format(Me.txtStartDate, "mm-dd-yyyy") & ")"
  11. elseif IsDate(Me.txtEndDate) then
  12.    stWhere = "([ReviewDate] <=" & Format(Me.txtEndDate, "mm-dd-yyyy") & ")"
  13. End If
  14.  
  15. ' stWhere filled?
  16. IF len(stWhere) > 0 then
  17.    if len(nz(Me.cmbWorker)) > 0 then
  18.       ' cmbWorker filled and stWhere
  19.       stWhere = stWhere & " AND ([Worker] =" & Me.cmbWorker & ")"
  20.    endif
  21. else
  22.    if len(nz(Me.cmbWorker)) > 0 then
  23.       ' cmbWorker filled and stWhere empty
  24.       stWhere = "([Worker] =" & Me.cmbWorker & ")"
  25.    endif
  26. endif
  27.  
  28. If stWhere <> "" Then
  29.    DoCmd.OpenReport "ErrorReport", acViewPreview, , stWhere
  30. else
  31.    DoCmd.OpenReport "ErrorReport", acViewPreview
  32. endif
  33.  
  34. End Sub
  35.  
Just keep the form opened, thus an erroneous selection can be corrected easily by the user.

Nic;o)
Oct 7 '08 #7
I am getting there, slowly but surely.

I used some of the code you suggested and added some of my variables.

It filters the report by the worker now. When I enter a date range and select a worker I get the following runtime error:

Run Time Error '3075':
Syntax error (Missing Operator) in query expression '((stDateField between ##09/01/2008# and #09/30/2008#) AND [Worker] = 'Last, First')'

When I enter just a date range and no worker I get the following runtime error:

Run Time Error '3075':
Syntax error (Missing Operator) in query expression '((stDateField between ##09/01/2008# and #09/30/2008#))'

I have the worker names stored in this format:
Column - WorkerName
Date - LastName, FirstName


Here is my code:

Private Sub cmdErrorReport_Click()
Dim stReport As String
Dim stForm As String
Dim stMacro As String
Dim stDateField As String
Dim stWhere As String
Dim stLinkCriteria As String
Dim lngView As Long
Const stDateFormat = "\#mm\/dd\/yyyy\#"

stReport = "ErrorReport"
stForm = "ErrorDateRange"
stMacro = "PrintErrorReport"
stDateField = "[ReviewDate]"
lngView = acViewPreview



If CurrentProject.AllReports(stReport).IsLoaded Then
DoCmd.Close acReport, stReport
End If

stWhere = ""

If IsDate(Me.txtStartDate) And IsDate(Me.txtEndDate) Then
stWhere = "(stDateField between #" & Format(Me.txtStartDate, stDateFormat) & " and " & Format(Me.txtEndDate, stDateFormat) & ")"
ElseIf IsDate(Me.txtStartDate) Then
stWhere = "(stDateField >=" & Format(Me.txtStartDate, stDateFormat) & ")"
ElseIf IsDate(Me.txtEndDate) Then
stWhere = "(stDateField <=" & Format(Me.txtEndDate, stDateFormat) & ")"
End If

If Len(stWhere) > 0 Then
If Len(Nz(Me.cboWorkerName)) > 0 Then
stWhere = stWhere & " AND [WORKER] =" & "'" & Me.cboWorkerName & "'"
End If
Else
If Len(Nz(Me.cboWorkerName)) > 0 Then
stWhere = "[Worker]=" & "'" & Me.cboWorkerName & "'"
End If
End If

If stWhere <> "" Then
DoCmd.OpenReport stReport, lngView, , stWhere
'DoCmd.RunMacro stMacro
'DoCmd.Close acForm, stForm
'DoCmd.Close acReport, stReport
Else
MsgBox "You must enter a worker and date range for this report!", vbOKOnly, "Not enough information!"
End If
End Sub


Thank you in advance. Especially if it is a dumb mistake on my part.
Oct 8 '08 #8
nico5038
3,080 Expert 2GB
There's an additional "#" before the first date, remove it from the stWhere filling.

Nic;o)
Oct 8 '08 #9
Ok I just about have it.

If I enter just start date, I get reports from then on.
If I enter just end date, I get reports from then back.
If I select just worker name, I get reports for that worker.

Only problem is they don't all work together. If I enter a worker and the wrong date range the report still opens with all fields saying error.

Here is the latest code:

Private Sub cmdErrorReport_Click()





Dim stReport As String
Dim stForm As String
Dim stMacro As String
Dim stDateField As String
Dim stWhere As String
Dim stLinkCriteria As String
Dim lngView As Long
Const stDateFormat = "\#mm\/dd\/yyyy\#"

stReport = "ErrorReport"
stForm = "ErrorDateRange"
stMacro = "PrintErrorReport"
stDateField = "[tblQALog].[ReviewDate]"
lngView = acViewPreview



If CurrentProject.AllReports(stReport).IsLoaded Then
DoCmd.Close acReport, stReport
End If

stWhere = ""

If IsDate(Me.txtStartDate) Then
stWhere = "([tblQALog].[ReviewDate] >=" & Format(Me.txtStartDate, stDateFormat) & ")"
ElseIf IsDate(Me.txtEndDate) Then
stWhere = "([tblQALog].[ReviewDate] <=" & Format(Me.txtEndDate, stDateFormat) & ")"
End If

If Len(stWhere) > 0 Then
If Len(Nz(Me.cboWorkerName)) > 0 Then
stWhere = stWhere & " AND [WORKER] =" & "'" & Me.cboWorkerName & "'"
End If
Else
If Len(Nz(Me.cboWorkerName)) > 0 Then
stWhere = "[Worker]=" & "'" & Me.cboWorkerName & "'"
End If
End If

If stWhere <> "" Then
DoCmd.OpenReport stReport, lngView, , stWhere
'DoCmd.RunMacro stMacro
'DoCmd.Close acForm, stForm
'DoCmd.Close acReport, stReport
Else
MsgBox "You must enter a worker and date range for this report!", vbOKOnly, "Not enough information!"
End If

End Sub
Oct 9 '08 #10
nico5038
3,080 Expert 2GB
You found the reason why I normally use a "dynamic" combo box.
When dates are selected the available workers can differ, by selecting the available workers first this error can't occur.

Nic;o)
Oct 9 '08 #11
I ams sorry but I do not know anything about using dynamic combobox with a date range form and reports. Is this something you can assist with?
Oct 9 '08 #12
nico5038
3,080 Expert 2GB
To start with you can create a new combobox and use a "SELECT DISTINCT ReviewDate from tblQALog Order by 1 DESC".
This can be used for the startdate.

Best probably to start with a combo for the employees. This will allow to select all ReviewDate values that are available for an employee.

This will change the above "SELECT" into a "SELECT DISTINCT ReviewDate from tblQALog WHERE employeeID = " & Me.cmbEmployee & " Order by 1 DESC"

Filling this Startdate combobox will be needed when the employee combobox has been selected, thus we set the rowsource of the combobox in the cmbEmployee's AfterUpdate event and first we test or there has been a selection of an employee like:
Expand|Select|Wrap|Line Numbers
  1. IF Len(nz(Me.cmbEmployee)) > 0 then
  2.    Me.cmbStartdate.rowsource = "SELECT" into a "SELECT DISTINCT ReviewDate from tblQALog WHERE employeeID = " & Me.cmbEmployee & " Order by 1 DESC"
  3. else
  4.    msgbox "Please select an employee"
  5. endif
  6.  
Personally I often make the "dependent" comboboxes visible after a selection has been made of the "master". Thus the user won't see the Start/End combo's when no employee has been selected and I don't have to test or the first fill the employee combo :-)

Getting the idea ?

Nic;o)
Oct 9 '08 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Garyrek | last post by:
Hi I have a requirement where I need to make a report for each auto dealer how many hits they got on specified date range, so for each car detail page I increment the counter and total it while...
5
by: Michelle | last post by:
Hi all I have a report which displays staff absences. An absence record has a start and end date and the type of absence, ie sickness, annual leave. I have created a form which allows the...
3
by: manning_news | last post by:
Using A2K. I've been asked to modify a report currently requiring only one date parameter to now accept a date range. The main report has 2 subreports and is not bound to a table or query. The...
2
by: Sara | last post by:
I have followed instructions on the http://allenbrowne.com/tips.html for limiting a report to a date range. At the bottom there is a note that says You will end up using this form for all sorts...
1
by: irfanali | last post by:
Hallo All, This is a Tool i m tryin to develop at work. I will explain how it works and then the Q I download a report from my ERP Tool on a daily basis and upload it into the Access Tool....
19
by: ali3n8 | last post by:
Hello I have attempted to create a date range report from a query called qrycustomerinformation. The field that contains the value of my date is called Followup. When i run a report on this it is...
7
by: dozingquinn | last post by:
Hello, Is there any way to auto populate the user defined date range into a report? I currently have the criteria "Between And " for a date range field. This prompts the user to enter a date...
5
by: jambonjamasb | last post by:
I am wanting to create a report that summarises the number of items within a date range. For example I have a FIELD called System_Change. This is a drop down COMBOBOX that uses words like unix,...
9
by: Mo | last post by:
After a little PHP education, my first project (to get my feet wet) is making an employee time-tracking program for our small business. *** ANY SUGGESTION, THOUGHTS, OR ADVICE WOULD BE WARMLY...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.