Article ID: 167472
Article Last Modified on 1/19/2007
Public Sub OpenNwindCategoryForm()
' Create a variable that will refer to Microsoft Access.
Dim MyAccessObject As Access.Application
' Create variables for file pathing.
Dim sPsep, DBPath As String
' Set DBPath to the path of Northwind database example.
' NOTE: Because Microsoft PowerPoint Visual Basic for
' Applications does not recognize the PathSeparator
' function, check for the program that is running
' this routine. For Microsoft Word and Microsoft Excel,
' use the PathSeparator function.
If Application.Name <> "Microsoft PowerPoint" Then
' The PathSeparator function returns the path separator,
' which is Operating System Platform specific.)
sPsep = Application.PathSeparator
Else
' For Microsoft PowerPoint 97, use the following line of
' code for the path separator.
sPsep = "\"
End If
DBPath = Application.Path & sPsep & "Samples" & sPsep _
& "Northwind.mdb"
On Error GoTo ErrHandler
' Create an instance of the Access application object.
Set MyAccessObject = CreateObject("Access.Application.8")
With MyAccessObject
.OpenCurrentDatabase DBPath 'Opens the Northwind file.
.DoCmd.OpenForm "Categories" 'Opens the Employees form.
'Set the caption Property of the form
.Forms!Categories.Caption = "Test OLE Automation - Add _
Categories"
' Add a new record to the form...
' acDataForm and acNewRec are pre-defined constants
' within the Access Object Library.
.DoCmd.GoToRecord acDataForm, "Categories", acNewRec
' Set the field Category Name to a value.
.Forms!Categories.CategoryName = _
InputBox("Enter a category name", "Category name", _
"A name")
' Set the field Description to a value.
.Forms!Categories.Description = _
InputBox("Enter a description", "Description", _
"A description")
' Save the record...
' acCmdSaveRecord is a pre-defined constant
' within the Access Object Library.
.DoCmd.RunCommand acCmdSaveRecord
.Visible = True ' Maximize Microsoft Access.
End With
ErrHandler: ' < NOTE: This line must be left aligned!
' Error handler displays the error message
' and then quits the program.
If Err <> 0 Then
MsgBox Err.Description, Title:="Test OLE Automation"
MyAccessObject.Quit ' Close Microsoft Access.
End If
Set MyAccessObject = Nothing ' Set the object variable to
' nothing
End Sub
120802 Office: How to Add/Remove a Single Office Program or Component
automation overview
Additional query words: wordcon inf word8 word97 PgmObj 8.00 8.0 vb vbe vba OLE OFF97 XL97 ACC97 PPT97
Keywords: kbinfo kbinterop kbprogramming KB167472