PPT2000: Sample VB Code That Uses DAO to Create Presentation

ID: Q222724


The information in this article applies to:
  • Microsoft PowerPoint 2000


SUMMARY

This article provides a sample Microsoft Visual Basic for Applications macro that uses Data Access Objects (DAO) to pull records from the NorthWind sample database. The records pulled from the database are then used to create a PowerPoint presentation.


MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft Support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

http://www.microsoft.com/support/supportnet/overview/overview.asp
NOTE: The following macro examples only work from within the PowerPoint application. Visual Basic for Applications macros are not supported by the Microsoft PowerPoint Viewer. For additional information, please see the following article in the Microsoft Knowledge Base:
Q230746 PPT: Viewer: Presentation Macros Don't Run
NOTE: The code in this sample is designed to run from within a PowerPoint macro. You may need to modify the code if you intend to run it from another application or another development environment. You must have the Microsoft Jet Engine and the NorthWind sample database installed and you must have read/write access to the folder that contains the NorthWind database.

Before you can use this macro, you must add a reference to the DAO object model. To do this, follow these steps:
  1. Start the Visual Basic Editor.


  2. On the Tools menu, click References.


  3. Click to select the Microsoft DAO 3.6 Compatibility Library check box.


  4. Click OK.



Sub UseDAO()

         ' Used for error trapping.
         On Error Resume Next

         ' The name of the database to open.
         Const DATABASE_NAME As String = "nwind.mdb"
         Const DATABASE_LONG As String = "Northwind.mdb"

         ' Holds the path to the Office Directory.
         Dim strOfficePath As String
         Dim strNWpath As String

         ' Holds the database query.
         Dim strSQL As String

         ' Variables used in DAO.
         Dim db As Database
         Dim rs As Recordset
         Dim i, Num As Long
         Dim oPres As Presentation

         ' Get the path to the Powerpnt.exe file
         strOfficePath = Application.Path

         ' Check if the Northwind database is present, has a long file 
         ' name.
         strNWpath = UCase(strOfficePath _
            & "\Samples\" & DATABASE_LONG)

         ' Open the Northwind database.
         Err.Clear
         Set db = DBEngine.OpenDatabase(strNWpath)

         ' Check if error occured when opening Northwind database.
         If Err.Number <> 0 Then

            ' Use the short file name.
            strNWpath = UCase(Left(strOfficePath, Len(strOfficePath) - 6) _
               & "\Samples\" & DATABASE_NAME)

            ' Open the Northwind database using the short file name.
            Err.Clear
            Set db = DBEngine.OpenDatabase(strNWpath)
            If Err.Number <> 0 Then
               MsgBox Err.Description, vbCritical, "Database Not Found"
               End
            End If
         End If

         ' Compose the query.
         ' Finds products that cost over 50 dollars.
         strSQL = "SELECT Products.ProductName, Products.UnitPrice" _
            & " FROM Products WHERE UnitPrice >= 50"

         ' Get the record set.
         Err.Clear
         Set rs = db.OpenRecordset(strSQL, dbOpenSnapshot)

         ' Check if error occured reading the table.
         If Err.Number <> 0 Then

            ' If error occurred display message, close the database, and
            ' exit from the macro.
            MsgBox Err.Description, vbCritical, "Unable To Open Table"
            db.Close
            End
         End If

         ' Move to the last record in the record set.
         rs.MoveLast

         ' Assign num the total number of records.
         Num = rs.RecordCount

         ' Move to the first record.
         rs.MoveFirst

         ' Create a presentation and insert a slide.
         Set oPres = Presentations.Add()

         ' Create a new slide using the two column text layout.
         oPres.Slides.Add 1, ppLayoutTwoColumnText

         ' Add a title to the slide.
         oPres.Slides(1).Shapes.Title.TextFrame.TextRange = "Products " _
         & "over 50 dollars"

         ' Set the font size for the shapes.
         With oPres.Slides(1).Shapes(2).TextFrame.TextRange
            .Font.Size = 24
            .ParagraphFormat.Bullet = msoFalse
         End With
         With oPres.Slides(1).Shapes(3).TextFrame.TextRange
            .Font.Size = 24
            .ParagraphFormat.Bullet = msoFalse
         End With

         ' Add a tab stop in the price list box.
         With oPres.Slides(1).Shapes(3).TextFrame.Ruler
            .TabStops.Add ppTabStopDecimal, 72
         End With

         ' Loop through the records in the record set.
         For i = 1 To Num
            With oPres.Slides(1)

               ' Adds the Product Name to the slide.
               With .Shapes(2).TextFrame
                  .TextRange = .TextRange & rs.Fields(0).Value & Chr(13)
               End With

               ' Adds the price of the item to the slide.
               With .Shapes(3).TextFrame
                  ' Create a decimal tab at one inch.
                  .TextRange = .TextRange & Chr(9) & Chr(9) _
                     & Format(rs.Fields(1).Value, "####0.00") & Chr(13)
               End With

            End With

            ' Move the record in the record set.
            rs.MoveNext
         Next i

         ' Close the database.
         db.Close

      End Sub 


REFERENCES

For more information about using the sample code in this article, please see the following article in the Microsoft Knowledge Base:

Q212536 OFF2000: How to Run Sample Code from Knowledge Base Articles

Additional query words: 9.00 ppt9 vba vbe ppt2k powerpt vba2k ppt9.0 ppt2000 program programming

Keywords : kbcode kbmacro kbprg kbdta kbdtacode kbpptvba
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: July 14, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.