ACC2000: Scripting Server Objects with SQL Distributed Management Objects (SQL-DMO) (233392)



The information in this article applies to:

  • Microsoft Access 2000

This article was previously published under Q233392
Advanced: Requires expert coding, interoperability, and multiuser skills.

This article applies only to a Microsoft Access project (.adp).

SUMMARY

When you create server-based objects such as tables and views in a Microsoft Access project, it's a good idea to document the schema of each object so that you can re-create it in the future.

If you own Microsoft SQL Server 7.0, you can use the Generate SQL Scripts functionality of SQL Server Enterprise Manager to document your work by outputting a file that you can run in SQL Server Query Analyzer or in OSQL to re-create the objects in your database. Microsoft Access 2000 does not have user-interface components that enable you to take advantage of this feature in SQL Server. However, you can write Visual Basic for Applications code using SQL Distributed Management Objects (SQL-DMO) that automates an instance of SQL Server 7.0 or the Microsoft Data Engine (MSDE) to create scripts.

MORE INFORMATION

The following code sample uses the Script method of SQL-DMO to generate a Transact-SQL command batch. You can use this command batch to re-create the following objects from any user database:
  • Tables
  • Views
  • Stored Procedures
  • Triggers
  • User-Defined Data Types
  • Rules
  • Defaults
Note that the demonstration code generates Transact-SQL to re-create SQL Server objects only.

NOTE: The code does not save or restore user data. If you need to back up data in your SQL Server or MSDE database, use the Backup command. To do this, in an Access project, click the Tools menu, point to Database Utilities, and then click Backup.

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. To generate an SQL Script, follow these steps:
  1. Open Microsoft Access 2000, and create a new, blank database. Name it SQLScript.

    NOTE: The following code will work as written when run from an Access 2000 project that is on the same computer as the SQL Server. To run this code from an Access project that will generate a script for a remote SQL Server database, you must modify the following line:
    sql.Connect "(local)", strLogin, strPwd
    						
    Change "(local)" to reflect the name of your SQL Server.

    Also note that this code will only work when run from a computer with Access 2000 and either MSDE or the SQL Server Client Tools installed.
  2. Create a new module and type the following line in the Declarations section if it is not already there:
    Option Explicit
    					
  3. On the Tools menu, click References. In the list of available references, click to select Microsoft SQLDMO Object Library. Click OK.
  4. Type the following procedure:
    Sub ScriptDB(strLogin As String, strPwd As String, _
                 strDataBase As String, StrFilePath As String)
    
    '==========================================================================
    ' Parameters
    ' -------------------------------------------------------------------------
    ' strLogin:  The Login Name of the account you use to connect to the server
    ' strPwd:  The Password for the account you use to connect to the server
    ' strDataBase:  The name of the database you want to create script for
    ' StrFilePath:  The path and filename to store the SQL file
    '==========================================================================
    
       Dim sql As Object
       Dim db As Object
       Dim objTrigger As Object
       Dim intOptions As Long
       Dim genObj
       
       Set sql = CreateObject("SQLDMO.SQLServer")
       Set db = CreateObject("SQLDMO.Database")
       Set objTrigger = CreateObject("SQLDMO.Trigger")
        
       Const sDrops As Integer = 1
       Const sIncludeHeaders As Long = 131072
       Const sDefault As Integer = 4
       Const sAppendToFile As Integer = 256
       Const sBindings As Integer = 128
        
       ' Set scripting options. Because you need to specify multiple behaviors
       ' for the ScriptType argument, you use "Or" to combine these.
       intOptions = sDrops Or sIncludeHeaders Or _
       sDefault Or sAppendToFile Or sBindings
        
       ' Connect to local server
       sql.Connect "(local)", strLogin, strPwd
       Set db = sql.Databases(strDataBase, "dbo")
        
       ' Script User Defined Data Types
       For Each genObj In db.UserDefinedDatatypes
          genObj.Script intOptions, StrFilePath
       Next
        
       ' Script Tables and Triggers, ignoring system
       ' tables and system generated triggers
       For Each genObj In db.Tables
          If genObj.SystemObject = False Then
             genObj.Script intOptions, StrFilePath
             For Each objTrigger In genObj.Triggers
                If objTrigger.SystemObject = False Then
                   objTrigger.Script intOptions, StrFilePath
                End If
             Next
          End If
       Next
            
       ' Script Rules 
       For Each genObj In db.Rules
          genObj.Script intOptions, StrFilePath
       Next
        
       ' Script Defaults
       For Each genObj In db.Defaults
          genObj.Script intOptions, StrFilePath
       Next
        
       ' Script Sprocs, ignoring system sprocs
       For Each genObj In db.StoredProcedures
          If genObj.SystemObject = False Then
             genObj.Script intOptions, StrFilePath
          End If
       Next
            
       ' Script Views, ignoring system views and informational schemas
       For Each genObj In db.Views
          If genObj.SystemObject = False Then
             genObj.Script intOptions, StrFilePath
          End If
       Next
                
       MsgBox "Finished generating SQL scripts."
       
    End Sub
    					
  5. Save the module as MyModule.
  6. To call the procedure, open the Immediate window, type the following line, and then press ENTER:
    Call ScriptDB("UserName","Password","DatabaseName","C:\MyResults.SQL")
    					
    NOTE: In this sample, replace UserName and Password with the logon account and password of a user who has permissions on the server. Replace DatabaseName with the name of the database in which you want to script objects. Replace C:\MyResults.SQL with the path and name of the file that you want the code to generate.

REFERENCES

For more information about SQL-DMO, refer to SQL Server 7.0 Books Online, which is available for download from the following Microsoft Web site:

Modification Type:MajorLast Reviewed:6/23/2005
Keywords:kbdownload KbClientServer kbhowto KB233392