![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
|
This code works in VB, but apparently not in VBA. Can someone please tell me what I'm doing wrong? |
|
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 |
#2
| |||
| |||
|
|
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 |
#3
| |||
| |||
|
|
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 |
#4
| |||
| |||
|
|
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 |
#5
| |||
| |||
|
|
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 |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
Instead of: View.Script(SQLDMO.SQLDMO_SCRIPT_TYPE.SQLDMOScript _DRI_PrimaryKey) It should have been this: View.Script(SQLDMOScript_DRI_PrimaryKey) |
|
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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |