PPT2000: Sample VB Code That Uses DAO to Create Presentation (222724)



The information in this article applies to:

  • Microsoft PowerPoint 2000

This article was previously published under Q222724

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. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. NOTE: The following macro examples work only in PowerPoint. Visual Basic for Applications macros are not supported by the Microsoft PowerPoint Viewer. For additional information, click the following article number to view the article in the Microsoft Knowledge Base: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 how to use the sample code in this article, click the article number below to view the article in the Microsoft Knowledge Base:

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


Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbcode kbdtacode kbhowto kbmacro kbProgramming KB222724