Share via


Customizing the Controls on a Data Access Page at Run Time

This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.

 

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.

Figure 1. A page with conditional formatting of controls

Apply Conditional Formatting to Controls on a Data Access Page

  1. Open a data access page in Design view.
  2. Do one of the following:
    • On an ungrouped page where the size of the data page is set to 1, add code to the Current event to apply conditional formatting to the controls on the page.

    • On all other pages, add code to the DataPageComplete event to apply conditional formatting to the controls on the page.

      The following is a sample DataPageComplete event procedure. It sets the Color property of the controls in the header section to red if the UnitsInStock field has a value that is less than or equal to 20.

      <SCRIPT language=vbscript event=DataPageComplete(dscei) for=MSODSC>
      <!--
      ' Highlight items that are almost out of stock.
      dim sect
      dim dscconst
      dim bandHTML
      
      ' Check that the event fired for the DataPage in the Products 
      ' GroupLevel.
      If (dscei.DataPage.GroupLevel.RecordSource = "Products") Then
          Set dscconst = MSODSC.Constants
          Set sect = dscei.DataPage.FirstSection
      
          ' Go through the sections of the event's DataPage object.
          Do
              ' Ignore all sections except the header section.
              If (sect.Type = dscconst.sectTypeHeader) Then
                  Set bandHTML = sect.HTMLContainer 
      
                  ' Change the text to red if there are <= 20 units in 
                  ' stock.
                  If (CInt(bandHTML.children("UnitsInStock").innerText) _
                                                              <= 20) Then
                      bandHTML.children("UnitsInStock").style. _
                                                          color = "red"
                      bandHTML.children("UnitPrice").style.color = "red"
                      bandHTML.children("ProductName").style.color = "red"
                  End If
              End If
              Set sect = sect.NextSibling
          Loop until (sect is nothing)
      End If
      -->
      </SCRIPT>
      

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.

Figure 2. A page with controls that can grow at run time

Make a Control and the Containing Section Grow at Run Time

  1. Open a data access page in Design view.

  2. Set the Overflow property to Visible for the bound span control whose height needs to be adjusted.

  3. 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.

  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

  1. Open a data access page in Design view.

  2. 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>