Meyyammai Subramanian
Microsoft Corporation
April 2001
Applies to:
Microsoft® Access 2002
Summary: This article covers examples of applying conditional formatting, adjusting control height, and setting the Title property of controls on a data access page at run time. (7 printed pages)
Contents
Apply Conditional Formatting to a Control
Adjust the Height of a Control to Fit Its Contents at Run Time
Customize ScreenTips for Controls at Run Time
Apply Conditional Formatting to a Control
In a form or report, you can apply conditional formatting to change the appearance of a control based on its contents. In a data access page, you can apply conditional formatting to a control programmatically.
The following illustration shows the Products page, where a record is displayed in red if the # In Stock field has a value that is less than or equal to 20.
.gif)
Figure 1. A page with conditional formatting of controls
Apply Conditional Formatting to Controls on a Data Access Page
- Open a data access page in Design view.
- Do one of the following:
Adjust the Height of a Control to Fit Its Contents at Run Time
In a form or report, you can set the CanGrow property of a text box to increase the height of a control to fit its contents. Similarly, in a data access page, you can make a bound span control adjust its height to fit its contents.
The following illustration shows how the Description bound span control and the header section increase in size when a longer string is displayed in the control.
.gif)
Figure 2. A page with controls that can grow at run time
Make a Control and the Containing Section Grow at Run Time
- Open a data access page in Design view.
- Set the Overflow property to Visible for the bound span control whose height needs to be adjusted.
- Add code to the DataPageComplete event to adjust the height of the containing section.
The following is a sample DataPageComplete event procedure that adjusts the height of the header section containing the Description text box.
<SCRIPT language=vbscript event=DataPageComplete(dscei) for=MSODSC>
<!--
Dim dscconst
Dim sec
Dim el
Set c = MSODSC.Constants
Set sec = dscei.DataPage.FirstSection
While (not(sec is nothing))
If (sec.type = c.sectTypeHeader) Then
Set el = sec.HTMLContainer.children("Description")
sec.HTMLContainer.style.height = el.offsetTop + _
el.offsetHeight + 5
End If
Set sec = sec.NextSibling
Wend
-->
</SCRIPT>
Note The bound span control whose height adjusts at run time must be at the bottom of the section. Otherwise, it will grow over other controls and hide their contents. If a control that can grow is not at the bottom of the section, you will have to manually move the controls below it at run time.
The following is a sample DataPageComplete event procedure that moves five controls that are below a control that grows, and resizes the section accordingly.
SCRIPT language=vbscript event=DataPageComplete(oEventInfo) for=MSODSC>
<!--
Dim c
Dim sect ' Section element.
Dim elem1, elem2, elem3, elem4, elem5 ' Dynamic elements.
Dim label2, label3, label4, label5
Dim SecHeight ' Height to add to section.
Set c = MSODSC.Constants
Set sect = oEventInfo.DataPage.FirstSection
While(Not(sect is Nothing))
If (sect.Type = c.sectTypeHeader) Then
' Set each of the dynamic elements to a variable.
Set elem1 = sect.HTMLContainer.children("Critical")
Set elem2 = sect.HTMLContainer.children("Summary")
Set elem3 = sect.HTMLContainer.children("Trends")
Set elem4 = sect.HTMLContainer.children("Bugs")
Set elem5 = sect.HTMLContainer.children("CustComments")
Set label2 = sect.HTMLContainer.children("lblSummary")
Set label3 = sect.HTMLContainer.children("lblTrends")
Set label4 = sect.HTMLContainer.children("lblBugs")
Set label5 = sect.HTMLContainer.children("lblComments")
' Reposition each dynamic control and its label based on the
' previous control.
elem2.style.Top = elem1.offsetTop + elem1.offsetHeight + 35
label2.style.Top = elem2.style.Top
elem3.style.Top = elem2.offsetTop + elem2.offsetHeight + 35
label3.style.Top = elem3.style.Top
elem4.style.Top = elem3.offsetTop + elem3.offsetHeight + 35
label4.style.Top = elem4.style.Top
elem5.style.Top = elem4.offsetTop + elem4.offsetHeight + 35
label5.style.Top = elem5.style.Top
' Height of section should be 5 below bottom of last control.
sect.HTMLContainer.style.Height = elem5.offsetTop + _
elem5.offsetHeight + 5
sect.HTMLContainer.children("rectSideBar").style.Height = _
sect.HTMLContainer.style.Height
End If
Set sect = sect.NextSibling
Wend
-->
</SCRIPT>
Customize ScreenTips for Controls at Run Time
You can set the Title property of a control at run time to show information about specific fields, records, or groups.
The following illustration shows a page where the expand control displays different ScreenTip text based on the group to which the instance of the control belongs.
.gif)
ScreenTip for the expand control in the Beverages group.
ScreenTip for the expand control in the Confections group.
Figure 3. Controls with custom ScreenTips
Set ScreenTip Text at Run Time
- Open a data access page in Design view.
- Add code to the DataPageComplete event to set the Title property of the controls to the string you want.
The following is a sample DataPageComplete event procedure that sets the Title property of the expand control in the Categories group level.
<SCRIPT language=vbscript event=DataPageComplete(dscei) for=MSODSC>
<!--
Dim dscconst
Dim sec
Dim secHTML
If (dscei.DataPage.GroupLevel.RecordSource = "Categories") Then
Set dscconst = MSODSC.Constants
Set sec = dscei.DataPage.FirstSection
' Walk the sections in the Categories DataPage object.
While not(sec is nothing)
If (sec.Type = dscconst.sectTypeHeader) Then
Set secHTML = sec.HTMLContainer
' Set the ScreenTip.
secHTML.children("CategoriesExpand").title = _
"Click here to view the products in the " & _
secHTML.children("CategoryName").innerText & " section."
End If
Set sec = sec.nextsibling
Wend
End If
-->
</SCRIPT>