You can use SQLDMO (SQL Distributed Management Objects) in VB to script a SQL Server database. There is a VB example included on the SQL 2000 install CD. If you installed the SQL Server developer tools, you should be able to find the sample code in...
Modify the path as needed if the SQL installation is not in Program Files on C:
I have written a VB subroutine using SQLDMO for scripting all databases on all servers. This code has been tested in Access 2000 VBA. It should run in VB6 but hasn't been tested, yet.
The routine will script SQL 7 and 2000 databases. It will may work for SQL 6.5 but no guarantees.
You'll need to add a reference for the "Microsoft SQLDMO Object Library" to your VB or Access project.
'Dim objects and variables
Dim oSQL As New SQLDMO.Application
Dim oServerNames As NameList
Dim oObjNames As NameList
Dim oSS As SQLDMO.SQLServer
Dim oDb As SQLDMO.Database
Dim oT As SQLDMO.Transfer
Dim i As Integer, j As Integer, k As Integer
Dim strServer As String, strDBName As String
Dim sMsg As String
On Error GoTo Scripting_Error
Debug.Print "Scripting started - " & _
CStr(Now()) & vbCrLf
'Create list of servers
Set oServerNames = oSQL.ListAvailableSQLServers()
'Iterate through server name list
For k = 1 To oServerNames.Count
'Get server name
strServer = oServerNames(k)
DoEvents
If strServer <> "." Then
'Create Server object
Set oSS = New SQLDMO.SQLServer
'Set LoginSecure if using trusted connection
'oSS.LoginSecure = True
'Connect to the server - integrated security
'oSS.Connect strServer
'Connect to the server - SQL Security
oSS.Connect strServer, "sa", "password"
DoEvents
'Iterate through databases on current server
For i = 1 To oSS.Databases.Count
'Create database object
Set oDb = oSS.Databases.Item(i)
'Get DB name
strDBName = oDb.Name
DoEvents
'Bypass system dbs
If strDBName <> "tempdb" And _
strDBName <> "master" And _
strDBName <> "msdb" And _
strDBName <> "model" Then
'Create transfer object
Set oT = New SQLDMO.Transfer
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.