dbTalk Databases Forums  

Re: ActiveX & SQLDMO

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Re: ActiveX & SQLDMO in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dan Guzman
 
Posts: n/a

Default Re: ActiveX & SQLDMO - 03-21-2006 , 09:36 PM






Quote:
This code works in VB, but apparently not in VBA. Can someone please tell
me what I'm doing wrong?
It's difficult to help unless you post a code snippet. My guess is that you
aren't instantiating a DMO object using CreateObject:

Set oSQLServer = CreateObject("SQLDMO.SQLServer")

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Andre" <no (AT) spam (DOT) com> wrote

Quote:
I'm trying to write an ActiveX task to script my indexes for a couple
tables, into a file. This will be an ActiveX step in a DTS package. It's
failing at the step where I'm trying to script the index out. The error
is
"object required; SQLDMO".

This code works in VB, but apparently not in VBA. Can someone please tell
me what I'm doing wrong?

Thanks, Andre






Reply With Quote
  #2  
Old   
Andre
 
Posts: n/a

Default Re: ActiveX & SQLDMO - 03-21-2006 , 11:40 PM






Quote:
It's difficult to help unless you post a code snippet. My guess is that
you aren't instantiating a DMO object using CreateObject:
Interesting...I attached a file with my exact code. I'll paste it in this
time. Thanks for your time.

Function Main()

Dim svr
Dim strFile
Dim strDB

Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\TableIndexes.txt")
Set f1 = fso.GetFile("c:\TableIndexes.txt")
Set ts = fso.OpenTextFile("c:\TableIndexes.txt", ForWriting, True)

set svr = CreateObject("SQLDMO.SQLServer")
strDB = "MyDB"

' logon to SQL Server
With svr
.Name = "MyServer"
.LoginTimeout = 15
.LoginSecure = True
.Connect()
End With


For Each View In svr.Databases.Item(strDB).Tables
' add index info to text file
If View.Name = "table1" Or View.Name = "table2" Then
'msgbox View.Name
'ts.WriteLine View.Name
ts.WriteLine(View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE .SQLDMOScript_DRI_PrimaryKey))
' ts.WriteLine 1,
(View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScrip t_Indexes))
End If

Next

MsgBox "Finished scripting indexes."

svr.Disconnect()
ts.Close

Main = DTSTaskExecResult_Success
End Function




Reply With Quote
  #3  
Old   
Enric
 
Posts: n/a

Default Re: ActiveX & SQLDMO - 03-22-2006 , 05:16 AM



hi Andre,
I've got a a script running properly using DMO:
Perhaps, it would be useful for you, I haven't idea, but in any case, how
odd having issues with DMO dll..

Function Main()

Const SQLDMOXfrFile_SingleFile = 2

Dim sS, i, fichero
Dim Data1,Data2,Data3

Set fso = CreateObject("Scripting.FileSystemObject")
Set oSS = CreateObject("sqldmo.sqlserver")
Set oDb = CreateObject("sqldmo.database")


on error resume next

oSS.Connect "sql1", "usrdts", "dts"


For i = 1 To oSS.Databases.Count

Set oDb = oSS.Databases(i)


Set oTablas = CreateObject("sqldmo.transfer")
Set oProcedimientos = CreateObject("sqldmo.transfer")
Set oVistas = CreateObject("sqldmo.transfer")


oTablas.CopyAllTables = True
oProcedimientos.CopyAllStoredProcedures = True
oVistas.CopyAllViews = True

oDb.ScriptTransfer oTablas, SQLDMOXfrFile_SingleFile,
"\\srvaa8\unidades\DesaGes\GestionAplicaciones\SQL \SQL2000\ScriptsAutomaticos\Tablas" & oDb.Name & ".txt"
oDb.ScriptTransfer oProcedimientos, SQLDMOXfrFile_SingleFile,
"\\srvaa8\unidades\DesaGes\GestionAplicaciones\SQL \SQL2000\ScriptsAutomaticos\Proc1" & oDb.Name & ".txt"
oDb.ScriptTransfer oVistas, SQLDMOXfrFile_SingleFile,
"\\srvaa8\unidades\DesaGes\GestionAplicaciones\SQL \SQL2000\ScriptsAutomaticos\Vistas" & oDb.Name & ".txt"




fichero1 =
"\\srvaa8\unidades\DesaGes\GestionAplicaciones\SQL \SQL2000\ScriptsAutomaticos\Tablas" & oDb.Name & ".txt"
fichero2 =
"\\srvaa8\unidades\DesaGes\GestionAplicaciones\SQL \SQL2000\ScriptsAutomaticos\Proc1" & oDb.Name & ".txt"
fichero3 =
"\\srvaa8\unidades\DesaGes\GestionAplicaciones\SQL \SQL2000\ScriptsAutomaticos\Vistas" & oDb.Name & ".txt"


Set inFile = fso.OpenTextFile(fichero1)
Data1 = inFile.ReadAll
inFile.Close

Set inFile = fso.OpenTextFile(fichero2)
Data2 = inFile.ReadAll
inFile.Close


Set inFile = fso.OpenTextFile(fichero3)
Data3 = inFile.ReadAll
inFile.Close

ficherosalida =
"\\srvaa8\unidades\DesaGes\GestionAplicaciones\SQL \SQL2000\ScriptsAutomaticos\" & oDb.Name & ".txt"
Set outfile = fso.CreateTextFile(ficherosalida)

'copiando los 3 ficheros en 1 solo
outfile.Write Data1
outfile.WriteLine
outfile.Write Data2
outfile.WriteLine
outfile.Write Data3
outfile.Close

'esborrat dels fitxers
fso.DeleteFile fichero1
fso.DeleteFile fichero2
fso.DeleteFile fichero3

Set oTablas = Nothing
Set oProcedimientos = Nothing
Set oVistas = Nothing


' if i = 4 then
' exit for
' end if

Next

Main = DTSTaskExecResult_Success

End Function

--
Current location: Alicante (ES)


"Andre" wrote:

Quote:
It's difficult to help unless you post a code snippet. My guess is that
you aren't instantiating a DMO object using CreateObject:
Interesting...I attached a file with my exact code. I'll paste it in this
time. Thanks for your time.

Function Main()

Dim svr
Dim strFile
Dim strDB

Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\TableIndexes.txt")
Set f1 = fso.GetFile("c:\TableIndexes.txt")
Set ts = fso.OpenTextFile("c:\TableIndexes.txt", ForWriting, True)

set svr = CreateObject("SQLDMO.SQLServer")
strDB = "MyDB"

' logon to SQL Server
With svr
.Name = "MyServer"
.LoginTimeout = 15
.LoginSecure = True
.Connect()
End With


For Each View In svr.Databases.Item(strDB).Tables
' add index info to text file
If View.Name = "table1" Or View.Name = "table2" Then
'msgbox View.Name
'ts.WriteLine View.Name
ts.WriteLine(View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE .SQLDMOScript_DRI_PrimaryKey))
' ts.WriteLine 1,
(View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScrip t_Indexes))
End If

Next

MsgBox "Finished scripting indexes."

svr.Disconnect()
ts.Close

Main = DTSTaskExecResult_Success
End Function




Reply With Quote
  #4  
Old   
Dan Guzman
 
Posts: n/a

Default Re: ActiveX & SQLDMO - 03-22-2006 , 06:57 AM



The problem is that you have not declared SQLDMOScript_DRI_PrimaryKey. You
get the constants via static members when you reference the SQLDMO class on
VB but you need to declare these in VBScript, like you did the ForWriting
FSO constant.

Add the following to you code and all should be fine. See
SQLDMO_SCRIPT_TYPE in the Books Online for a complete list of the constants.

Const SQLDMOScript_DRI_PrimaryKey = 268435456

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Andre" <no (AT) spam (DOT) com> wrote

Quote:
It's difficult to help unless you post a code snippet. My guess is that
you aren't instantiating a DMO object using CreateObject:
Interesting...I attached a file with my exact code. I'll paste it in this
time. Thanks for your time.

Function Main()

Dim svr
Dim strFile
Dim strDB

Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\TableIndexes.txt")
Set f1 = fso.GetFile("c:\TableIndexes.txt")
Set ts = fso.OpenTextFile("c:\TableIndexes.txt", ForWriting, True)

set svr = CreateObject("SQLDMO.SQLServer")
strDB = "MyDB"

' logon to SQL Server
With svr
.Name = "MyServer"
.LoginTimeout = 15
.LoginSecure = True
.Connect()
End With


For Each View In svr.Databases.Item(strDB).Tables
' add index info to text file
If View.Name = "table1" Or View.Name = "table2" Then
'msgbox View.Name
'ts.WriteLine View.Name

ts.WriteLine(View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE .SQLDMOScript_DRI_PrimaryKey))
' ts.WriteLine 1,
(View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScrip t_Indexes))
End If

Next

MsgBox "Finished scripting indexes."

svr.Disconnect()
ts.Close

Main = DTSTaskExecResult_Success
End Function





Reply With Quote
  #5  
Old   
Andre
 
Posts: n/a

Default Re: ActiveX & SQLDMO - 03-22-2006 , 10:03 AM



Quote:
Add the following to you code and all should be fine. See
SQLDMO_SCRIPT_TYPE in the Books Online for a complete list of the constants.

Const SQLDMOScript_DRI_PrimaryKey = 268435456
I added the line above and I still get the "object required: SQLDMO" error
on line 38. My code now looks like this:

Function Main()

Dim svr
Dim strFile
Dim strDB
Dim fso, f1, ts

Const SQLDMOScript_DRI_PrimaryKey = 268435456
Const SQLDMOScript_Indexes = 73736
Const ForWriting = 2

Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\TableIndexes.txt")
Set f1 = fso.GetFile("c:\TableIndexes.txt")
Set ts = fso.OpenTextFile("c:\TableIndexes.txt", ForWriting, True)

set svr = CreateObject("SQLDMO.SQLServer")
strDB = "MyDB"

' logon to SQL Server
With svr
.Name = "MyServer"
.LoginTimeout = 15
.LoginSecure = True
.Connect()
End With

' display error msgbox if logon failed
If Err.Number <> 0 Then
MsgBox "Connect Failed" & Err.Description & " (" & Err.Number & ")",
MsgBoxStyle.OKOnly, "Error"
End If

For Each View In svr.Databases.Item(strDB).Tables
' add index info to text file
If View.Name = "MyTable" Then
'msgbox View.Name
'ts.WriteLine View.Name
' ts.WriteLine
View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript _DRI_PrimaryKey)
ts.WriteLine 1,
(View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScrip t_Indexes))
End If

' Some simple error handling
If Err.Number <> 0 Then
MsgBox "Script error while scripting " & "Details: " &
Err.Description & VBCLF & "Error number: " & Err.Number
svr.DisConnect()
End If
Next

MsgBox "Finished scripting indexes. The file is in c:\"

svr.Disconnect()
ts.Close

Main = DTSTaskExecResult_Success
End Function




Reply With Quote
  #6  
Old   
Andre
 
Posts: n/a

Default Re: ActiveX & SQLDMO - 03-22-2006 , 04:39 PM



I figured it out.

Instead of:
View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript _DRI_PrimaryKey)
It should have been this:
View.Script(SQLDMOScript_DRI_PrimaryKey)

Thanks for all your help - it definitely got me going in the correct
direction.

Andre



Reply With Quote
  #7  
Old   
Dan Guzman
 
Posts: n/a

Default Re: ActiveX & SQLDMO - 03-22-2006 , 08:44 PM



Quote:
Instead of:
View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript _DRI_PrimaryKey)
It should have been this:
View.Script(SQLDMOScript_DRI_PrimaryKey)
That's because you have no 'SQLDMO' object reference like you do in VB.

I'm glad you were able to sort things out.

--
Hope this helps.

Dan Guzman
SQL Server MVP

"Andre" <no (AT) spam (DOT) com> wrote

Quote:
I figured it out.

Instead of:
View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript _DRI_PrimaryKey)
It should have been this:
View.Script(SQLDMOScript_DRI_PrimaryKey)

Thanks for all your help - it definitely got me going in the correct
direction.

Andre





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.