ACC2000: Scripting Server Objects with SQL Distributed Management Objects (SQL-DMO)ID: Q233392
|
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.
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:
http://www.microsoft.com/mcsp/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.aspTo generate an SQL Script, follow these steps:
Option Explicit
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 arguement, 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
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.
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:
http://support.microsoft.com/download/support/mslfiles/sqlbol.exe
Additional query words:
Keywords : kbdta AccessCS
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto
Last Reviewed: July 19, 1999