Share via


Exploring Procedures

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.

This topic is designed to give users, who may be familiar with Microsoft FrontPage, but unfamiliar with Microsoft Visual Basic for Applications, background on some of the basic concepts in a FrontPage-based programming enivronment. Programming in FrontPage Visual Basic for Applications provides you with HTML tools in an Microsoft Office programming environment where you can create procedures that perform a task or a series of tasks. For example, you could:

  • Create a procedure that retrieves data from a Microsoft Access database and displays the data on your web page

  • Publish a Microsoft PowerPoint presentation for automatic updates over the Internet

  • Automatically update a Microsoft Excel spreadsheet with input from users responding to your web site

Click a hyperlink in the following list to move directly to that topic.

Organize code for modular use

Types of procedures

Public and private procedures

Types of procedure calls

Event procedures and arguments

Create a table in FrontPage from an Access database

Multiple projects

Organize code for modular use

Visual Basic procedures provide a way for developers to organize code for modular use. Instead of writing the same calculator function over and over for each program, you can take that segment of code (the calculator function) and compile it into a general program, which can then be accessed by many other programs. In Visual Basic, a block of code is enclosed between a procedure heading and a closure statementthe Sub and End Sub statements.

The basic syntax of a procedure within Visual Basic is shown in the following code sample.

[Private|Public|Static] Subprocedurename(arguments)

    statements

End Sub

To run any of the samples of complete code (a code block) follow these steps:

  1. Open FrontPage, select Macro from the Tools menu, and then click Visual Basic Editor.

  2. Double-click Microsoft_FrontPage (or the current project) in the Project window, and expand the Modules folder.

  3. Double-click Module 1 to open the Code window.

  4. Select the code block in the documentation that you want to run, press Ctrl + C to copy the selected area. Click the Microsoft Visual Basic Microsoft FrontPage button on the Windows taskbar.

  5. Click anywhere in the Code window and press Ctrl + V to paste the sample code.

  6. Click the Run Sub/User Forms button on the toolbar.

Your code will automatically be saved when you close the Visual Basic Editor. For more information on Visual Basic, refer to Understanding Objects, Properties, Methods, and Events in the online documentation Visual Basic Conceptual Topics.

Types of procedures

FrontPage Visual Basic provides two types of procedures, Sub procedures and Function procedures. What's the difference? Sub procedures execute a block of code in response to an event procedure, such as a mouse click or a keystroke, and are voidthey perform tasks that do not return any values. Therefore, you can't return a value from within an event procedure.

Note   A Sub procedure can be an event procedure, but it can also perform a task without returning a value and without responding to an event.

The following procedure retrieves the version number of FrontPage from with the active web, but doesn't return the version number to any other procedure.

Sub DisplayVersion()
Dim myWeb As Web
Dim myVersion As String

myWeb = "FrontPage version number: " & ActiveWeb.Application.Version
End Sub

A Function procedure also performs tasks, but returns one or more values as arguments. The following code sample returns the version number of FrontPage to the calling procedure GetAppVersion.

Function ReturnVersion(currAppVersion As Variant)
Dim varAppVersion As Variant
    varAppVersion = Application.Version
    ReturnVersion = varAppVersion
End Function

The variable currAppVersion now contains the version number of FrontPage. To access this value in the calling procedure, you could write code similar to the following code sample.

Sub GetAppVersion()
Dim myAppVersion As Variant

MsgBox "This version of FrontPage is version " _
    & ReturnVersion(myAppVersion)
End Sub

Alternatively, you could assign the expression ReturnVersion(varDisplayAppVersion) to a variable and append the variable to the message box statement instead of the function call.

Both Sub and Function procedures can be called to perform their tasks from within other modules or procedures, depending on whether the procedures are declared Public or Private procedures.

Note   To use the previous function for more than one module, place the code in the Declarations section of the General procedure.

A macro is a third term used to describe code in Visual Basic for Applications. As a public Sub procedure that doesn't take arguments, a macro may or may not call other Sub or Function procedures, and can be assigned to command bars and shortcut keys or run from the Macro dialog box.

Public and private procedures

Visual Basic provides two ways to access a procedure. By default, procedures are publicthey can be called from any other procedure in any module within your application. For example, if you've written a procedure that lists images by file name on a Web page, you would want to declare that procedure public so that you could use it across all of your Web sites. However, if you've written a procedure that edits a specific database, you would want that procedure to be available only to the module that handles editing the databasein that case, you can declare the procedure private. Procedures that have been declared private can only be referenced by other procedures within the same module. The function shown previously has been declared a public function in the following code sample and can be called across modules and projects.

Public Function ReturnVersion(varCurrentAppVersion As Variant)
    statements
End Function

In contrast, a procedure that is used to edit a database should be declared private.

Private Function EditCustomerName(strFirstName As String)
    statements
End Function

Types of procedure calls

How do you programmatically run a procedure? You declare it the same way that you would use a keyword, such as Open. The following procedure calls the ReturnVersion function and includes a local variable, MyVersion, for the value that is passed to the procedure.

Function TestCall(AppVersion As Variant)
Dim MyVersion As Variant
    ReturnVersion(MyVersion)
End Sub

If you didn't have any information to pass from one procedure to another, you would simply declare the procedure name, as shown in the following code sample.

Sub TestCall2()
    DisplayCompanySplashScreen
End Sub

The TestCall2 procedure calls another procedure, DisplayCompanySplashScreen, which doesn't take any arguments or return any values.

Event procedures and arguments

If you want an event, such as clicking a command button, to trigger the execution of code in cases where you would usually pass a value to the calling procedure, you can execute the results from the function rather than pass a variable. Adding the following code shown in bold to the ReturnVersion function initiates the display of the version number for the active application.

Function ReturnVersion(varCurrentAppVersion As Variant)
Dim varAppVersion As Variant
    varAppVersion = Application.System.Version
    varCurrentAppVersion = varAppVersion
    DisplayMsgBox (varCurrentAppVersion)
End Function

The DisplayMsgBox function shown in the following code sample displays the contents of the variable varCurrentAppVersion that was passed from the ReturnVersion function.

Function DisplayMsgBox(varGotAppVersion As Variant)
Dim varDisplayAppVersion As Variant
    VarDisplayAppVersion = varGotAppVersion
    MsgBox "This application is version " _
        & varDisplayAppVersion

An event procedure can now initiate the display of the value that is passed from the ReturnVersion function.

Private Sub CommandButton1_Click()
Dim varThisAppVersion As Variant
    ReturnVersion (varThisAppVersion)
End Sub

Create a table in FrontPage from an Access database

The following procedure combines objects from the Page object model and the Web object model to retreive data from an open Microsoft Access database and insert it into a table on a FrontPage-based web page. The ParseDBTable procedure provides the parameters for the ParseAccessTable function which calls the following functions to create and populate the table:

  • AddDBTableToPagecreates a new table

  • AddDBRowinserts a row onto the web page

  • AddMemoretrieves the memos from the Access database, returns them as bookmarks at the bottom of the page below the new table, and returns the URL to the bookmark

Note   The Access database, Northwind.mdb, was used for this example. To run the example, you must have references in the Visual Basic Editor to Microsoft DAO 3.6 Object Library and Microsoft Access 9.0 Object Library. You must also open an Access database before running the example, and you must add a blank temporary file called tmp.htm in the active web. If you use a database other than Northwind.mdb, you must specify the database name and table in the ParseDBTable procedure.

Function AddDBTableToPage(myPage As PageWindow, _
    myTableName As String, myFields As Integer)
Dim myTable As FPHTMLTable
Dim myHTMLString As String
Dim myCount As Integer

myHTMLString = "<table border=""2"" id=""myRecordSet_" & _
    myTableName & """>" & vbCrLf
myHTMLString = myHTMLString & "<tr>" & vbCrLf

For myCount = 1 To myFields
    myHTMLString = myHTMLString & "<td id=""myDBField_" & _
        myCount & """> </td>" & vbCrLf
Next myCount

myHTMLString = myHTMLString & "</tr>" & vbCrLf
myHTMLString = myHTMLString & "</table>" & vbCrLf

Call myPage.Document.body.insertAdjacentHTML("BeforeEnd", _
    myHTMLString)

End Function

Function AddDBRow(myDBTable As FPHTMLTable)
Dim myHTMLString As String
Dim myTableRow As FPHTMLTableRow

Set myTableRow = myDBTable.rows(0)

myHTMLString = myTableRow.outerHTML
Call myDBTable.insertAdjacentHTML("BeforeEnd", myHTMLString)

End Function

Function AddMemo(myCurrentPage As PageWindow, myDBMemo As String, _
    myBkMarkField As String, myIndex) As String
Dim myHTMLString As String
Dim myMemoBkMark As String
Dim myBookMark As FPHTMLAnchorElement

myMemoBkMark = myBkMarkField & "_" & myIndex
myHTMLString = "<a name=""" & myMemoBkMark & """> Memo #" & _
    myIndex & "</a>" & vbCrLf

'Add the bookmark to the page.
Call myCurrentPage.Document.body.insertAdjacentHTML("BeforeEnd", _
    myHTMLString)

Set myBookMark = myCurrentPage.Document.all(myMemoBkMark)

'Add the memo text to the page.
Call myCurrentPage.Document.body.insertAdjacentHTML("BeforeEnd", _
    myDBMemo)
AddMemo = "<a href=""#" & myBookMark.Name & """>"

End Function

Function ParseAccessTable(myDBName As String, myTableName As String)
'Access/DAO Declarations.
Dim myDBApp As Access.Application
Dim myRecordSet As DAO.recordset
Dim myDBField As DAO.Field

'FrontPage Page object model declarations.
Dim myPage As PageWindow
Dim myTable As FPHTMLTable
Dim myTableRow As FPHTMLTableRow
Dim myTableCell As FPHTMLTableCell

'Function declarations.
Dim myCount As Integer
Dim myFieldValue As String
Dim myRecordCount As Integer
myRecordCount = 0

'Function constants.
Const myTempPage = "tmp.htm"

'Get the current Access database.
On Error GoTo AccessNotThereYet
    Set myDBApp = GetObject(, "Access.Application")

'Get the database table.
On Error Resume Next
Set myRecordSet = myDBApp.CurrentDb.OpenRecordset(myTableName)

'Add a new page to the current web.
Set myPage = ActiveWeb.LocatePage(myTempPage)
myPage.SaveAs myTableName & ".htm"

'Delete the temporary file from web.
ActiveWeb.LocatePage(myTempPage).File.Delete

'Add a database-ready table to the page with the proper number of fields.
AddDBTableToPage myPage, myTableName, myRecordSet.Fields.Count

'Get a reference to the table.
Set myTable = myPage.Document.all.tags("table").Item(0)

'Populate the first row.
For myCount = 0 To myRecordSet.Fields.Count - 1
    myTable.rows(0).cells(myCount).innerHTML = "<b>" & _
        Trim(myRecordSet.Fields(myCount).Name) & "</b>"
Next

'Populate the rest of the table.
While Not (myRecordSet.EOF)
   
    AddDBRow myTable
    Set myTableRow = myTable.rows(myTable.rows.Length - 1)
   
    For myCount = 0 To myRecordSet.Fields.Count - 1
        Set myTableCell = myTableRow.cells(myCount)
       
        If IsNull(myRecordSet.Fields(myCount)) Then
            myFieldValue = "None"
        Else
            myFieldValue = Trim(myRecordSet.Fields(myCount).Value)
        End If

        If myRecordSet.Fields(myCount).Type = DAO.dbMemo Then
            myFieldValue = AddMemo(myPage, _
                myRecordSet.Fields(myCount).Value, _
                myRecordSet.Fields(myCount).Name, myRecordCount)
        End If

        myTableCell.innerHTML = myFieldValue

    Next myCount
    myRecordSet.MoveNext
    myRecordCount = myRecordCount + 1
Wend

myPage.Save
myDBApp.Quit
Exit Function

AccessNotThereYet:
    Debug.Print Err.Number & ":" & Err.Description
    Resume

End Function

Private Sub ParseDBTable()
Call ParseAccessTable("Northwind.mdb", "Products")
End Sub

Multiple projects

FrontPage doesn't support multiple projects. You can install the Microsoft Office Developer (ODE) to add flexibility to your Visual Basic Editor. ODE provides the capability of creating multiple projects, compiling .dlls, and managing your Visual Basic projects more effectively.