Solved

Make Access Sub-report visible or not visible based on record number in detail section of main report

Posted on 2013-01-23
16
1,672 Views
Last Modified: 2013-02-05
Hello Experts !!

I have several sub-reports all present with in the detail section of an Access report.
Some sub-reports need only show in even # records, and others only once (in detail record 1).

What VBA code can I use, and in which event do I do this?
Seems I need a counter (to count which detail record is current), and write code around this counter.

Any Ideas appreciated,

Larry
0
Comment
Question by:Larry Biederman
  • 9
  • 7
16 Comments
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
Use the Current Event of your main form.

The VBA to display a subform on even records :


 
       Me.MySubformControlName.Visible = (Me.RecordID MOD 2 = 0)

Open in new window


Or for just the first record:

 Me.MySubformControlName.Visible = (Me.RecordID = 1)

Open in new window


etc...
0
 

Author Comment

by:Larry Biederman
Comment Utility
mbizup,

Thanks!  Will give a try.
How does "mod" function?
-Larry
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
<<
How does "mod" function?
>>

Mod is an arithmetic function (which happens to be included in VBA) which will give you the remainder of division between two numbers.

So...

x mod y is the remainder from x/y

To test if a number is odd or even, you can divide it by two and see if there is a remainder

eg:  4/2 = 2 with a remainder of 0
       5/2 = 2 with a remainder of 1

Since Mod gives you the remainder of the division:
       4 Mod 2 = 0
       5 Mod 2 = 1

And in general...

--  an even number mod 2 will give you 0
--  an odd number mod 2 will give you 1

(That's your math lesson for today :-)  )
0
 

Author Comment

by:Larry Biederman
Comment Utility
I'm get error on (Me.RecordID = 1)

Should it be substituted for and actual object (textbox etc)?
 
To make invisible on records 2 on should I use something like this:

if (Me.RecordID = 1) then
  Me.MySubformControlName.Visible = true
else
  Me.MySubformControlName.Visible = false
end if

Thanks, Larry
0
 

Author Comment

by:Larry Biederman
Comment Utility
Thanks for math lesson.

I do remember mod from my vb6 days, but didn't know if use was different here.
Thanks!!
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
>> Should it be substituted for and actual object (textbox etc)?

Not necessarily a textbox (although that will work too) ... but it does need to be a valid field name from the underlying table or query.

What is the actual name of the field that you are using to determine whether to hide/show the form?  If you substitute that field name, it should work.  

You'll also have to subsitute the correct name of your subform.

Your block of code for hiding records that are not record 1 looks good... you just need to subsitute the correct fieldname for RecordID and subform for MySubformControlName.
0
 

Author Comment

by:Larry Biederman
Comment Utility
Having Problems:

I'm using this code:

Private Sub Report_Current()

If Me.TxtNozzle1 = 1 Then
  Me.ShellRpt_sub.Visible = True
Else
  Me.ShellRpt_sub.Visible = False
End If

End Sub

TxtNozzle1 is a text box (located in details section) bound to a field in the query that feeds the details section of report. I have four records.  The value of TxtNozzle1 will be 1,2,3,4 as each detail record is printed.

Problem 1: I have pressed an f9 on the first line of code (If....), so it will stop when this module executes.  It never does !!

Problem 2: the value of Txtnozzle1 (when it appears on the printout is 4,8,12,16.

First problem is only one I'm concerned about, as I can use value 4, to make sub-report visible.

Any idea why "Private Sub Report_Current()" code never executes?  It is located in main report, not sub-report

Thanks, Larry
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
Yikes!   I just realized we are talking about a report not a form.

In a report, use the Detail section's format event. Instead.   If that doesn't help,  post a sample database.   Actual data is not important if it is sensitive.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 

Author Comment

by:Larry Biederman
Comment Utility
Thanks. Will try again in morning
0
 

Author Comment

by:Larry Biederman
Comment Utility
mbizup,

OK, got most of it working!
Another sub-report has recordset with 4 records. It has the same TxtNozzle1 column in each record.  How do I use TxtNozzle1 as filter so sub-report selects only that row, each time another detail record is printed?

So, when detail record 1 is printed, it selects row where TxtNozzle1 =1.
When detail record 2 is printed, it selects row where TxtNozzle1 =2, etc.

I see the 'filter' and 'filter on load' properties, which may be useful.
Your thoughts....

Thanks, Larry
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
Hi -

I'm having a hard time picturing that one...  can you maybe arrange your next post, using numbers, to visually/graphically show what you are trying to accomplish?
0
 
LVL 61

Accepted Solution

by:
mbizup earned 400 total points
Comment Utility
Ah!  I think I understand what your're saying - you want to filter your subreport down to just the record that matches the parent report's TxtNozzle1, right?

You might be able to do this without code... by setting the Master/child links on that subreport to include whatever field txtNozzle1 contains.  If you do that, the main report will automatically control the subreport records as you are describing.

Have you tried it that way yet?
0
 

Author Comment

by:Larry Biederman
Comment Utility
Mbizup,

thanks, I will give that idea a try today.
-Larry
0
 

Author Comment

by:Larry Biederman
Comment Utility
Master/child works!  
Finally, in which procedure do I put code to make these sub-reports visible or not visible?

With that, I'll award you the points.
Thanks,
-Lbiederman
0
 
LVL 61

Assisted Solution

by:mbizup
mbizup earned 400 total points
Comment Utility
Try the code in the Format Event of the main report's detail section.

(aka: main report's Detail Format Event)
0
 

Author Closing Comment

by:Larry Biederman
Comment Utility
Its all working!
Now I just need a table to control which sub-report is visible or not visible.

Thanks Much,
Lbiederman
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Technology opened people to different means of presenting information, but PowerPoint remains to be above competition. Know why PPT still works today.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now