PPT97: Sample VB Code Uses DAO to Create Presentation

ID: Q164582


The information in this article applies to:
  • Microsoft PowerPoint 97 For Windows


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 97 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 code in this sample is designed to run from within a PowerPoint 97 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. If any of the components listed above are not installed on your computer, please see the following article in the Microsoft Knowledge Base:
Q120802 Office: How to Add/Remove a Single Office Program or Component
Before you can use this macro, you must add a reference to the DAO object model, using these steps:

  1. Start the Visual Basic Editor.


  2. On the Tools menu, click References.


  3. Click to select the "Microsoft DAO 2.5/3.5 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 additional information about getting help with Visual Basic for Applications, please see the following article in the Microsoft Knowledge Base:

Q163435 VBA: Programming Resources for Visual Basic for Applications

Additional query words: 97 8.00 kbmacro kbpptvba ppt8 vba vbe

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


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