CurrentProject Object, Document, and IsLoaded Property Example

This procedure illustrates how to use VBA code to add text to a data access page. The following information is supplied in the arguments to this procedure:

strPageName The name of an existing data access page.
strID The ID property (attribute) for the tag that contains the text you want to work with.
strText The text to insert.
blnReplace Whether to replace existing text in the tag.

Function DAPInsertText(strPageName As String, _
    strID As Variant, strText As String, _
    Optional blnReplace As Boolean = True) As Boolean

    Dim blnWasLoaded As Boolean

    On Error GoTo DAPInsertText_Err

    ' Determine if the page exists and whether it is
    ' currently open. If not open then open it in
    ' design view.
    If DAPExists(strPageName) = True Then
        If CurrentProject.AllDataAccessPages(strPageName) _
            .IsLoaded = False Then
            blnWasLoaded = False
            With DoCmd
                .Echo False
                .OpenDataAccessPage strPageName, _
                    acDataAccessPageDesign
            End With
        Else
            blnWasLoaded = True
        End If
    Else
        DAPInsertText = False
        Exit Function
    End If

    ' Add the new text to the specified tag.
    With DataAccessPages(strPageName).Document
        If blnReplace = True Then
            .All(strID).innerText = strText
        Else
            .All(strID).innerText = .All(strID).innerText & strText
        End If
        ' Make sure the text is visible.
        With .All(strID).Style
            If .display = "none" Then .display = ""
        End With
    End With

    ' Clean up after yourself.
    With DoCmd
        If blnWasLoaded = True Then
        .Save
    Else
        .Close acDataAccessPage, strPageName, acSaveYes
        End If
    End With
    DAPInsertText = True
DAPInsertText_End:
    DoCmd.Echo True
    Exit Function
DAPInsertText_Err:
    MsgBox "Error #" & Err.Number & ": " & Err.Description
    DAPInsertText = False
    Resume DAPInsertText_End
End Function