473,399 Members | 3,888 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,399 software developers and data experts.

When I open a report using vba and apply a filter I lose the ability to sort.

I have a report that has three components: department, shift, and a calculated field. I am grouping by department and shift and sorting the calculated field in descending order. When I just open the report it works great. When I use a form with checkboxes that allows the user to choose which departments to include in the report it stops sorting by the calculated field. It will not even let me manually sort by that field. What have I messed up?
Dec 1 '15 #1

✓ answered by zmbd

I've tried to replicate this in my test database and have not been able to do so with my data.

You do have to have the report closed before calling the report from the code... that's snagged me once or twice during development. Have it open in design view and then run the VBA to open the report... and the report ignores the WhereCondition when it opens in print preview or is sent to the printer (opps, sorry tree :( ).

Are there any VBA/Macro codes running in the On_Current, On_Load, On_Open, On_Activate, On_Filter,.... Or for that matter any of the events in the report?
Open your form in design view.
Show the property sheet for the report
Double check that "Report" is shown in the PS dropdown
Events tab
The attached is from data based on a MS tutorial.
I've set the where condition as:
[City_Name]='Jakarta' or [City_Name]='Paris'

In the report itself, the grouping is by [City_Name] sorted on [RetailSales] as descending.
[RetailSales] is a calculated field in the underlying query.

[City_Name] moved to the [City_Name] group header
Sub totals are calculated textbox in the [City_Name] group footer

The grand totals are in the report footer

The form use the DoCmd.OpenReport passing the wherecondition in as the above string.

Once the report was opened, used the DoCmd.OutPutTo command to send to PDF.

The formatting is generally default; thus, kindof ugly :)

9 1514
zmbd
5,501 Expert Mod 4TB
sounds like could be something in the calling code; however, there can be other causes one such being if the value of the text box is calculated in the form/report instead of in the underlying recordset. Another is if there are conflicting names between the form and the report controls... scope should limit this; however, stranger things have happened.

Please post the code that loads your report from the form (cut and paste), please use the [CODE/] format by selecting the posted script and clicking on the [CODE/] format tool.

-z
Dec 2 '15 #2
Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdOpenReport_Click()
  2.     Dim strFilter As String
  3.     Dim lngLen As Long
  4.  
  5.     'Identify which departments are selected for the filter string
  6.     If Me.chkCFAA = -1 Then
  7.         strFilter = strFilter & "(DeptName = """ & "CFAA" & """) Or "
  8.     End If
  9.  
  10.     If Me.chkCFMP = -1 Then
  11.         strFilter = strFilter & "(DeptName = """ & "CFMP" & """) Or "
  12.     End If
  13.  
  14.     If Me.chkPlating = -1 Then
  15.         strFilter = strFilter & "(DeptName = """ & "Plating" & """) Or "
  16.     End If
  17.  
  18.     If Me.chkSPEER = -1 Then
  19.         strFilter = strFilter & "(DeptName = """ & "SPEER" & """) Or "
  20.     End If
  21.  
  22.     If Me.chkPA = -1 Then
  23.         strFilter = strFilter & "(DeptName = """ & "PA" & """) Or "
  24.     End If
  25.  
  26.     If Me.chkPMP = -1 Then
  27.         strFilter = strFilter & "(DeptName = """ & "PMP" & """) Or "
  28.     End If
  29.  
  30.     If Me.chkChemistry = -1 Then
  31.         strFilter = strFilter & "(DeptName = """ & "Chemistry" & """) Or "
  32.     End If
  33.  
  34.     If Me.chkRFAA = -1 Then
  35.         strFilter = strFilter & "(DeptName = """ & "RFAA" & """) Or "
  36.     End If
  37.  
  38.     If Me.chkRFMP = -1 Then
  39.         strFilter = strFilter & "(DeptName = """ & "RFMP" & """) Or "
  40.     End If
  41.  
  42.     If Me.chkRFP = -1 Then
  43.         strFilter = strFilter & "(DeptName = """ & "RFP" & """) Or "
  44.     End If
  45.  
  46.     If Me.chkAccounting = -1 Then
  47.         strFilter = strFilter & "(DeptName = """ & "Accounting" & """) Or "
  48.     End If
  49.  
  50.     If Me.chkCI = -1 Then
  51.         strFilter = strFilter & "(DeptName = """ & "CI" & """) Or "
  52.     End If
  53.  
  54.     If Me.chkEngineering = -1 Then
  55.         strFilter = strFilter & "(DeptName = """ & "Engineering" & """) Or "
  56.     End If
  57.  
  58.     If Me.chkEnvironmental = -1 Then
  59.         strFilter = strFilter & "(DeptName = """ & "Environmental" & """) Or "
  60.     End If
  61.  
  62.     If Me.chkHR = -1 Then
  63.         strFilter = strFilter & "(DeptName = """ & "HR" & """) Or "
  64.     End If
  65.  
  66.     If Me.chkIT = -1 Then
  67.         strFilter = strFilter & "(DeptName = """ & "IT" & """) Or "
  68.     End If
  69.  
  70.     If Me.chkMachineShop = -1 Then
  71.         strFilter = strFilter & "(DeptName = """ & "Machine Shop" & """) Or "
  72.     End If
  73.  
  74.     If Me.chkMaintenance = -1 Then
  75.         strFilter = strFilter & "(DeptName = """ & "Maintenance" & """) Or "
  76.     End If
  77.  
  78.     If Me.chkPurchasing = -1 Then
  79.         strFilter = strFilter & "(DeptName = """ & "Purchasing" & """) Or "
  80.     End If
  81.  
  82.     If Me.chkQA = -1 Then
  83.         strFilter = strFilter & "(DeptName = """ & "QA" & """) Or "
  84.     End If
  85.  
  86.     If Me.chkRandD = -1 Then
  87.         strFilter = strFilter & "(DeptName = """ & "R&D" & """) Or "
  88.     End If
  89.  
  90.     If Me.chkSafety = -1 Then
  91.         strFilter = strFilter & "(DeptName = """ & "Safety" & """) Or "
  92.     End If
  93.  
  94.     If Me.chkSales = -1 Then
  95.         strFilter = strFilter & "(DeptName = """ & "Sales" & """) Or "
  96.     End If
  97.  
  98.     If Me.chkShipping = -1 Then
  99.         strFilter = strFilter & "(DeptName = """ & "Shipping" & """) Or "
  100.     End If
  101.  
  102.     'Trim any trailing " AND " from the filter string
  103.     'See if the string has more than 5 characters (a trailng " AND ") to remove.
  104.     lngLen = Len(strFilter) - 4
  105.     If lngLen <= 0 Then     'Nah: there was nothing in the string.
  106.         MsgBox "Please select a department"
  107.     Else                    'Yep: there is something there, so remove the " AND " at the end.
  108.         strFilter = Left$(strFilter, lngLen)
  109.         'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
  110.         'Debug.Print strWhere
  111.  
  112.         'Finally, apply the string as the form's Filter.
  113.         'Me.Filter = strFilter
  114.         'Me.FilterOn = True
  115.     End If
  116.  
  117.     DoCmd.OpenReport "Completed Kaizen List", acViewPreview, , strFilter
  118.  
  119. End Sub
  120.  
Dec 2 '15 #3
zmbd
5,501 Expert Mod 4TB
Expand|Select|Wrap|Line Numbers
  1.  'See if the string has more than 5 characters (a trailng " AND ") to remove.
  2. 104.     lngLen = Len(strFilter) - 4
Sticks out...

Expand|Select|Wrap|Line Numbers
  1.         'For debugging, remove the leading quote on the next line. Prints to Immediate Window (Ctrl+G).
  2. 110.         'Debug.Print strWhere
  3.  
Uncomment line 110 and take a look again at the resolved string here. This might not be the issue; however, it's not going to hurt anything to verify that things are actually being formed correctly before we start in on other steps.

You might consider posting this string for us to look at too. :) ([CODE/] please :)
Dec 2 '15 #4
strWhere comes up empty
Dec 2 '15 #5
jforbes
1,107 Expert 1GB
How are you expecting your report order to be Defined?

I don't typically use the OrderBy Property on a Report, but set the OrderBy in the Query that the report is based on. You may want to check the OrderBy and OrderByOnLoad properties of your Report to see if they are causing you this trouble.
Dec 2 '15 #6
Tried using the OrderBy properties and it didn't make a difference.
Dec 2 '15 #7
zmbd
5,501 Expert Mod 4TB
strWhere comes up empty
Sorry, didn't catch that you have a different string in the filter...
either insert the debug.print strFilter below line110 and comment out the original line or replace line110. I would like to see what I being passed to the report.

Also, if you are using a query as the recordsource for the report the SQL for that query might be something to look at too.

Finally, is the calculated value being done in the recordsource or the report/form?
Dec 2 '15 #8
I had two departments selected with check boxes and this is the filter string "strFilter" = (DeptName = "RFP") Or (DeptName = "Engineering")

And I think I may have mispoke. It's not a calculated field as such, it's a count of records, but it is happening in the query which is a combination of two queries. The first is a list of all employees being pulled from a master HR database. The second is a count of the number of times the employee has entered a record in the local database.

Here is the sql code for the query:
Expand|Select|Wrap|Line Numbers
  1. SELECT qEEList.[Value Stream]
  2.    , qEEList.DeptName, qEEList.Shift
  3.    , qEEList.EmpID, qEEList.Name
  4.    , Nz([CountOfEmpID],"0") 
  5.       AS CountofCompleted
  6. FROM qEEList 
  7.    LEFT JOIN qParticipationCount 
  8.       ON qEEList.EmpID 
  9.          = qParticipationCount.EmpID
  10. ORDER BY Nz([CountOfEmpID],"0") DESC;
Dec 3 '15 #9
zmbd
5,501 Expert Mod 4TB
I've tried to replicate this in my test database and have not been able to do so with my data.

You do have to have the report closed before calling the report from the code... that's snagged me once or twice during development. Have it open in design view and then run the VBA to open the report... and the report ignores the WhereCondition when it opens in print preview or is sent to the printer (opps, sorry tree :( ).

Are there any VBA/Macro codes running in the On_Current, On_Load, On_Open, On_Activate, On_Filter,.... Or for that matter any of the events in the report?
Open your form in design view.
Show the property sheet for the report
Double check that "Report" is shown in the PS dropdown
Events tab
The attached is from data based on a MS tutorial.
I've set the where condition as:
[City_Name]='Jakarta' or [City_Name]='Paris'

In the report itself, the grouping is by [City_Name] sorted on [RetailSales] as descending.
[RetailSales] is a calculated field in the underlying query.

[City_Name] moved to the [City_Name] group header
Sub totals are calculated textbox in the [City_Name] group footer

The grand totals are in the report footer

The form use the DoCmd.OpenReport passing the wherecondition in as the above string.

Once the report was opened, used the DoCmd.OutPutTo command to send to PDF.

The formatting is generally default; thus, kindof ugly :)
Attached Files
File Type: pdf test.pdf (316.0 KB, 151 views)
Dec 3 '15 #10

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

Similar topics

16
by: Nhmiller | last post by:
I already have a report designed under Reports. When I use filtering in Forms or Tables, I see no way to select that filtered list when I am in Reports, and there appears to be no way to do the...
1
by: singhneeta | last post by:
Hi all, I have a funny issue. I have a stored procedure that opens up as a datasheet. User can use the filter by selection option in Access to filter records. For some reson, some of the columns...
3
by: MLH | last post by:
Private Sub Command0_Click() Dim MyReport As Report MyReport.Name = "rptStateOfAffairs" MyReport.Filter = "ClusterName = 'Auto Company, Inc'" Me.FilterOn = True DoCmd.OpenReport MyReport.Name...
1
by: mattscho | last post by:
Re: Filter By From, Apply Filter, Remove Filter Buttons in a Form. -------------------------------------------------------------------------------- Hi All, Trying to create a set of 3 buttons in...
0
by: riouw68 | last post by:
I try to open a report at a predifined place to be filled in into a inputbox. Private Sub Command68_Click() On Error GoTo Err_Command68_Click Dim stDocName As String Dim...
10
by: dbdb | last post by:
Hi, i create a chart in ms access based on my query, then i want my chart when is it open is only show value based on my criteria. i'll try to used it in the properties apply filter using the...
2
by: phill86 | last post by:
Hi, I am filtering a report using the form filter with the following code DoCmd.OpenReport "report1", acViewReport, , Me.filter Which works fine untill I filter the form on one of the...
1
by: Azriel928 | last post by:
I know this is a simple function to program, but I'm a little rusty. I'm trying to set the "On Open" event on a report using a macro to apply a filter that I have saved as a query. Can you please...
9
by: philqw78 | last post by:
Hi all. I am trying to apply a filter in VBA to open a form. The filter fires on double click on a field value. I have got so far DoCmd.OpenForm "TrackerEditFmDSFrm", acNormal, , "AR_ID =" &...
1
by: Joe Martin | last post by:
I have a single report with several stats for each line. I would like the user to be able to apply one filter and then be able to apply a second filter to the same report. I have several buttons...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: 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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.