Dynamically add image controls on Access report - ms-access

I have a table in Access that has comments in one field and a file path to an associated picture in another. My report has the comment and then an image bound to the file path field underneath that comment. However, most comments do not have a picture with it, and the empty space between comments makes the report unnecessarily long.
Is there a way in VBA to only add an image control if there is a file path and minimize the spacing between the pictureless comments?

You can't add any controls to a report when Access is in Runtime mode. But you can easily resize the image control using VBA in one of the Report's Format methods. Assuming your Image control is named "Image1", and it is in the Detail section:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.Image1.ImageHeight = 0 Then
Me.Image1.Height = 144 ' whatever minimum height you want
Me.Detail.Height = 144
Else
Me.Image1.Height = 1440 ' whatever the normal Image1 height is
Me.Detail.Height = 1530 ' whatever maximum detail height you want
Endif
End Sub
That should be very close to what you need.

Related

Available space for forms in msaccess

I am trying to find a way to get the dimensions of the space available in ms-access to show forms. The yellow part in the image below
Both height and width would be nice to know but for the moment it is sufficient to get the height to be able to resize forms in the available height.
It depends on the presence of the ribbon and size of windows borders and statusbar. I cannot find any Application properties that show these dimensions.
Is that possible at all?
I think the easiest way is to create a form .
Lets call him Form1 then
add this to load event and open the form
Private Sub Form_Load()
Docmd.RunCommand acCmdAppMaximize ' to maximize access
DoCmd.Maximize ' to maximize the form
Debug.Print Form_Form1.FormHeader.Height 'if exist
Debug.Print Form_Form1.PageHeaderSection.Height 'if exist
Debug.Print Form_Form1.Detail.Height
Debug.Print Form_Form1.PageFooterSection.Height 'if exist
Debug.Print Form_Form1.FormFooter.Height ' if exist
' Total form height is total of the above
Debug.Print Form_Form1.Width
Debug.Print CommandBars("Ribbon").Height ' this is the Ribbon height
End Sub
Hope that help. Important all the values are in points (some Strange units of measure from microsoft , google for points 2 cm or what ever you like ) Ops... the measure values are in TWIPs as june7 commented.

Ribbon form. Image object. How to programmatically set the path to the picture in the form element "picture"?

The combox cmb_pic_name is filled with the data from the directory_Pic with the query:
SELECT [directory_Pic].pic_name, [directory_Pic].pic_path FROM directory_Pic;
The user selects the name of the image from the combox cmb_pic_name.
Acces displays the image in the frm_Image (Image object) element.
 
I try to do with the code:
Private Sub cmb_pic_name_AfterUpdate()
Me.frm_Image.Picture = cmb_pic_name.Column(1)
End Sub
Private Sub Form_Load()
frm_Image.Picture = cmb_pic_name.Column(1)
End Sub
Does not work.
 
Question: how to programmatically set the path to the picture in the form element "picture"?
Update_1
Register for the link that is specified in the Gustav solution.
In a single-record form it would work, but your picture control is unbound, thus - in a continuous form - it will display the same picture for all records.
Make the picture control bound to pic_path.
An example for this - though for downloadable pictures - can be found in my article:
Show pictures directly from URLs in Access forms and reports
As you'll see, for each record it retrieves the path to the picture.
Click the link:
Click here to view the full article

Concatenate Rich Text Fields (HTML) and display result on Access form

I have an access database which deals with "articles" and "items" which are all textual stuff. An article is composed of several items. Each item has a rich text field and I wish to display the textual content of an article by concatenating all rich text fields of its items.
I have written a VBA program which concatenates the items rich text fields and feeds this into an independent TextBox control on my form (Textbox.Text = resulting string) but it does not work, I get an error message saying "this property parameter is too long".
If I try to feed a single textual field into the Textbox control, I get another error stating "Impossible to update the recordset" which I do not understand, what recordset is this about ?
Each item field is typically something like this (I use square brackets instead of "<" and ">" because otherwise the display of the post is not right) [div][font ...]Content[/font] [/div]", with "[em]" tags also included.
In front of my problem, I have a number of questions :
1) How do you feed an HTML string into an independent Textbox control ?
2) Is it OK to concatenate these HTML strings or should I modify tags, for example have only one "[div]" block instead of several in a row (suppress intermediate div tags) ?
3) What control should I use to display the result ?
You might well answer that I might as well use a subform displaying the different items of which an article is made up. Yes, but it is impossible to have a variable height for each item, and the reading of the whole article is very cumbersome
Thank you for any advice you may provide
It works for me with a simple function:
Public Function ConcatHtml()
Dim RS As Recordset
Dim S As String
Set RS = CurrentDb.OpenRecordset("tRichtext")
Do While Not RS.EOF
' Visually separate the records, it works with and without this line
If S <> "" Then S = S & "<br>"
S = S & RS!rText & vbCrLf
RS.MoveNext
Loop
RS.Close
ConcatHtml = S
End Function
and an unbound textbox with control source =ConcatHtml().
In your case you'd have to add the article foreign key as parameter to limit the item records you concatenate.
The "rich text" feature of a textbox is only intended for simple text.
We use the web browser control to display a larger amount of HTML text, and load it like this:
Private Sub Form_Current()
LoadWebPreview
End Sub
Private Sub HtmlKode_AfterUpdate()
LoadWebPreview
End Sub
Private Sub LoadWebPreview()
' Let the browser control finish the rendering of its standard content.
While Me!WebPreview.ReadyState <> acComplete
DoEvents
Wend
' Avoid the pop-up warning about running scripts.
Me!WebPreview.Silent = True
' Show body as it would be displayed in Outlook.
Me!WebPreview.Document.body.innerHTML = Me!HtmlBody.Value
End Sub

Access: Display Textbox Control In Sub-report when it has No Data

In a subreport I created a sub on detail_format event that will display a text when there is no data returned.
‘Code in sub-report
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me.Report.HasData Then
Me.Label43.Visible = True
Me.txtNotEntered.Visible = False
Else
Me.Label43.Visible = True
Me.txtNotEntered.Visible = True
End If
End Sub
It works fine on the subreport when run alone. When I run the main report it doesn’t trigger.
I added the same code in the main report to see if it would work. It runs through the lines of code but still cannot see the txtNotEntered textbox control.
‘Code in main report
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
If Me!rptResults_Comments.Report.HasData Then
Me!rptResults_Comments.Report.Label43.Visible = True
Me!rptResults_Comments.Report.txtNotEntered.Visible = False
Else
Me!rptResults_Comments.Visible = True
Me!rptResults_Comments.Report.Label43.Visible = True
Me!rptResults_Comments.Report.txtNotEntered.Visible = True
End If
End sub
I am using MS Access 2003.
Since the subreport is bound to the main report, the subreport itself will not be shown if there is no data to connect it to the main report. You can see this better by setting the background color of the subreport's detail section to red, or any other non-white color.
One workaround is to move your txtNotEntered control to the main report and put it "under" the subreport control (using Send to Back). Then set your subreport control's Can Shrink property to True.
Then, when there is data in the subreport you will see the subreport and it will cover the txtNotEntered control. When there is no data, the subreport will shrink out of the way and you will be able to see the txtNotEntered control.
One advantage to this approach is that it requires no code. Just leave the txtNotEntered Visible property set to True. The shrinking of the subreport will take care of revealing it when appropriate.
In addition to the answer from mwolfe02, I would suggest instead of using a label and VB code use a textbox with the following expression:
=IIf([HasData],"","No data found")
I use this on all my reports. An expression like
=IIf([rptResults_Comments].[Report].[HasData],","No data found")
should work as well. I didn't test the latter expression, so I'm not sure if [Report] is needed or not.
In addition to #rick's problem, how to display "No data" text when a subreport has No Data, it is also common enough to want to handle the case where main report controls reference subreport controls (and the subreport(s) has(have) No Data). This might occur when you are calculating a total that references subtotals from many subreports.
So we can put together #mwolfe02's and #TheOtherTimDuncan solutions (repurposing from #TheOtherTimDuncan his use of the HasData property), with some incidental additions of my own, as a checklist.
We have a main report rptMain and a subreport srpSub. In rptMain, detail section, we have a subcontrol sbctSub which references srpSub.
On rptMain create a label (there's no general need for it to be a textbox) lblNoData with whatever text you like (e.g. "No Data Returned"). Place lblNoData under sbctSub (using Send to Back). Use the same Top value. This ensures these don't overlap vertically so problems don't occur when Shrinking/Growing occurs.
Add an "annotating" label that is always viewable by a developer in design view but is permanently set to Visible = No (which makes it invisible in non-design views) with text like "Hidden lblNoData under subcontrol". If this needs to shrink (Can Shrink = Yes) then make it an unbound textbox. Colour such an annotating label (or textbox) to stand out in design view (e.g. I generally use a blue). Otherwise, down the track, there's a danger of losing sight, as a developer, of what's going on (worse still if another developer has to analyze your work).
Ensure the following are set to Can Shrink = Yes:
rptMain, Detail.
sbctSub.
When srpSub has no data then lblNoData will be revealed to the user as sbctSub will have shrunk to zero.
In production there might be issues with the No Data label showing through (white backgrounds seem to act transparently in some circumstances). If so, in rptMain:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
Me.lblNoData.Visible = (Not Me.sbctSub.Report.HasData)
End Sub
(My "design spike" testing worked as #mwolfe02 mentioned. That is, no additional code was necessary and I could rely on the subcontrol to hide and reveal the No Data label as needed. However my production report had these weird transparency issues.)
If you have any control on rptMain (e.g. txtGrandTotal) that references a control on srpSub, then set the control source as follows
=IIf([sbctTemp].[Report].[HasData],[sbctTemp].[Report]![txtSubTotal],0)
Null to zero Nz() won't work where the referenced control doesn't even exist, due to the subreport returning no data.
Edit 01: Added additional code to hide and show the No Data label in case of transparency issues. Rearranged this from being listed the last step, to being listed more sensibly in the order.
Edit 02: Made mention that the annotating label should be a textbox if it needs to shrink.

MS Access Force report footer to bottom of page

I am trying to make a report for a proposal. I would like to keep a professional look and always force the terms section to the bottom of the page. I could use the page footer but I only want the terms to show on the last page.
My idea is somehow with VBA to set the height of a dummy group such that it forces the report footer to the bottom. The problem is that you can't explicitly set the height of a section.
Anyone else out there with another idea (That works)? :-)
Thanks, Jeff
Here's an idea:
http://bytes.com/topic/misc/answers/499733-report-footer-location-problem#post1939746
To accomodate single page reports you could do something like
If Me.Pages > 0 Then
Me.Section(4).Visible = (iif(Me.Pages = 1, True, Me.Page = Me.Pages))
EndIf
Note that Access only calculates the number of pages if you have page numbers on your report. If you don't want them displayed you can set their Visible property to False.
This did the trick for me:
Private Sub PageHeaderSection_Format(Cancel As Integer, FormatCount As Integer)
Me.PageFooterSection.Visible = (Me.txtPage <> Me.txtPages)
End Sub
Thanks for your help.