home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / SQLScripte2866610122001.psc / frmMain.frm (.txt) next >
Encoding:
Visual Basic Form  |  2001-10-12  |  32.3 KB  |  889 lines

  1. VERSION 5.00
  2. Object = "{831FDD16-0C5C-11D2-A9FC-0000F8754DA1}#2.0#0"; "MSCOMCTL.OCX"
  3. Begin VB.Form frmMain 
  4.    BorderStyle     =   3  'Fixed Dialog
  5.    Caption         =   "SQLScripter"
  6.    ClientHeight    =   7470
  7.    ClientLeft      =   45
  8.    ClientTop       =   360
  9.    ClientWidth     =   7950
  10.    Icon            =   "frmMain.frx":0000
  11.    LinkTopic       =   "Form1"
  12.    MaxButton       =   0   'False
  13.    MinButton       =   0   'False
  14.    ScaleHeight     =   7470
  15.    ScaleWidth      =   7950
  16.    StartUpPosition =   3  'Windows Default
  17.    Begin VB.CommandButton cmdClearLog 
  18.       Caption         =   "&Clear Log"
  19.       Height          =   375
  20.       Left            =   3360
  21.       TabIndex        =   23
  22.       ToolTipText     =   "Clear the Log."
  23.       Top             =   6720
  24.       Width           =   1215
  25.    End
  26.    Begin VB.CommandButton cmdSaveLog 
  27.       Caption         =   "&Save Log"
  28.       Height          =   375
  29.       Left            =   4920
  30.       TabIndex        =   24
  31.       ToolTipText     =   "Save the Log."
  32.       Top             =   6720
  33.       Width           =   1215
  34.    End
  35.    Begin VB.CommandButton cmdGenerate 
  36.       Caption         =   "&Generate"
  37.       Default         =   -1  'True
  38.       Height          =   375
  39.       Left            =   1800
  40.       TabIndex        =   22
  41.       ToolTipText     =   "Start generating the scripts."
  42.       Top             =   6720
  43.       Width           =   1215
  44.    End
  45.    Begin VB.CommandButton cmdExit 
  46.       Cancel          =   -1  'True
  47.       Caption         =   "E&xit"
  48.       Height          =   375
  49.       Left            =   6600
  50.       TabIndex        =   25
  51.       ToolTipText     =   "Exit the program (by special request :)"
  52.       Top             =   6720
  53.       Width           =   1215
  54.    End
  55.    Begin VB.CommandButton cmdDisconnect 
  56.       Caption         =   "&Disconnect"
  57.       Height          =   375
  58.       Left            =   120
  59.       TabIndex        =   21
  60.       ToolTipText     =   "Disconnect from the currently selected server."
  61.       Top             =   6720
  62.       Width           =   1215
  63.    End
  64.    Begin VB.Frame fraServerOptions 
  65.       Caption         =   "Server Scripting options (sysadmins only):"
  66.       Height          =   1935
  67.       Left            =   240
  68.       TabIndex        =   32
  69.       Top             =   4560
  70.       Visible         =   0   'False
  71.       Width           =   7455
  72.       Begin VB.CheckBox chkOptionBackup 
  73.          Caption         =   "Script Backup Devices"
  74.          Height          =   375
  75.          Left            =   3960
  76.          TabIndex        =   42
  77.          ToolTipText     =   "Script the Backup Devices."
  78.          Top             =   720
  79.          Width           =   3135
  80.       End
  81.       Begin VB.CheckBox chkOptionDTS 
  82.          Caption         =   "Save DTS Packages"
  83.          Height          =   375
  84.          Left            =   240
  85.          TabIndex        =   36
  86.          ToolTipText     =   "Save DTS Packages as Structured Storage File."
  87.          Top             =   360
  88.          Width           =   3135
  89.       End
  90.       Begin VB.CheckBox chkOptionJobs 
  91.          Caption         =   "Script SQL Agent Jobs"
  92.          Height          =   375
  93.          Left            =   240
  94.          TabIndex        =   35
  95.          ToolTipText     =   "Script the MS SQL Server Agent Jobs."
  96.          Top             =   720
  97.          Width           =   3135
  98.       End
  99.       Begin VB.CheckBox chkOptionLogins 
  100.          Caption         =   "Script Server Logins"
  101.          Height          =   375
  102.          Left            =   240
  103.          TabIndex        =   34
  104.          ToolTipText     =   "Script the Server Logins."
  105.          Top             =   1080
  106.          Width           =   3135
  107.       End
  108.       Begin VB.CheckBox chkOptionAlerts 
  109.          Caption         =   "Script Operators/Alerts"
  110.          Height          =   375
  111.          Left            =   3960
  112.          TabIndex        =   33
  113.          ToolTipText     =   "Script the Operators and Alerts."
  114.          Top             =   360
  115.          Width           =   3135
  116.       End
  117.    End
  118.    Begin VB.Frame fraDBOptions 
  119.       Caption         =   "Database Scripting options:"
  120.       Height          =   3975
  121.       Left            =   240
  122.       TabIndex        =   27
  123.       Top             =   480
  124.       Visible         =   0   'False
  125.       Width           =   7455
  126.       Begin VB.CheckBox chkScriptDropsFirst 
  127.          Caption         =   "Script database object drops first"
  128.          Height          =   255
  129.          Left            =   3240
  130.          TabIndex        =   48
  131.          Top             =   2280
  132.          Width           =   3135
  133.       End
  134.       Begin VB.CheckBox chkScriptDatabase 
  135.          Caption         =   "Script the Database"
  136.          Height          =   375
  137.          Left            =   240
  138.          TabIndex        =   47
  139.          Top             =   240
  140.          Width           =   2175
  141.       End
  142.       Begin VB.CommandButton cmdSelectTables 
  143.          Caption         =   "..."
  144.          Height          =   315
  145.          Left            =   6600
  146.          TabIndex        =   46
  147.          Top             =   1920
  148.          Width           =   495
  149.       End
  150.       Begin VB.CheckBox chkInsertScript 
  151.          Caption         =   "Create INSERT scripts for selected tables"
  152.          Height          =   255
  153.          Left            =   3240
  154.          TabIndex        =   45
  155.          Top             =   1920
  156.          Width           =   3255
  157.       End
  158.       Begin VB.CheckBox chkDSFilePrefix 
  159.          Caption         =   "Prefix files with date stamp"
  160.          Height          =   315
  161.          Left            =   3240
  162.          TabIndex        =   44
  163.          Top             =   1560
  164.          Value           =   1  'Checked
  165.          Width           =   2295
  166.       End
  167.       Begin VB.CheckBox chkSingleFile 
  168.          Caption         =   "Script each Table, View and SP in separate files"
  169.          Height          =   375
  170.          Left            =   3240
  171.          TabIndex        =   43
  172.          Top             =   1200
  173.          Width           =   3855
  174.       End
  175.       Begin VB.CheckBox chkDBOptionRules 
  176.          Caption         =   "Script the Rules"
  177.          Height          =   375
  178.          Left            =   240
  179.          TabIndex        =   41
  180.          Top             =   1680
  181.          Width           =   2655
  182.       End
  183.       Begin VB.CheckBox chkDBOptionDefaults 
  184.          Caption         =   "Script the Defaults"
  185.          Height          =   375
  186.          Left            =   240
  187.          TabIndex        =   40
  188.          Top             =   2040
  189.          Width           =   2415
  190.       End
  191.       Begin VB.CheckBox chkDBOptionSPs 
  192.          Caption         =   "Script the Stored Procedures"
  193.          Height          =   375
  194.          Left            =   240
  195.          TabIndex        =   39
  196.          Top             =   1320
  197.          Width           =   2895
  198.       End
  199.       Begin VB.CheckBox chkDBOptionViews 
  200.          Caption         =   "Script the Views"
  201.          Height          =   375
  202.          Left            =   240
  203.          TabIndex        =   38
  204.          Top             =   960
  205.          Width           =   6015
  206.       End
  207.       Begin VB.CheckBox chkDBOptionTables 
  208.          Caption         =   "Script the Tables (including indexes, triggers and bindings)"
  209.          Height          =   375
  210.          Left            =   240
  211.          TabIndex        =   37
  212.          Top             =   600
  213.          Width           =   6015
  214.       End
  215.       Begin VB.CheckBox chkDBOptionUDT 
  216.          Caption         =   "Script the User Defined Datatypes"
  217.          Height          =   375
  218.          Left            =   240
  219.          TabIndex        =   31
  220.          Top             =   3120
  221.          Width           =   2895
  222.       End
  223.       Begin VB.CheckBox chkDBOptionUsers 
  224.          Caption         =   "Script the Database Users"
  225.          Height          =   375
  226.          Left            =   240
  227.          TabIndex        =   30
  228.          Top             =   2760
  229.          Width           =   2415
  230.       End
  231.       Begin VB.CheckBox chkDBOptionFullText 
  232.          Caption         =   "Include Full Text Catalogs and Indexes with DB and Table scripts"
  233.          Height          =   375
  234.          Left            =   240
  235.          TabIndex        =   29
  236.          Top             =   3480
  237.          Width           =   6015
  238.       End
  239.       Begin VB.CheckBox chkDBOptionRoles 
  240.          Caption         =   "Script the Database Roles"
  241.          Height          =   375
  242.          Left            =   240
  243.          TabIndex        =   28
  244.          Top             =   2400
  245.          Width           =   2415
  246.       End
  247.    End
  248.    Begin VB.Frame fraScripting 
  249.       Height          =   6015
  250.       Left            =   240
  251.       TabIndex        =   1
  252.       Top             =   480
  253.       Width           =   7455
  254.       Begin VB.CommandButton cmdFetchDBs 
  255.          Caption         =   "Connect && &Fetch Databases"
  256.          Height          =   375
  257.          Left            =   2640
  258.          TabIndex        =   12
  259.          ToolTipText     =   "Connect to the server and fetch the list of available databases."
  260.          Top             =   1920
  261.          Width           =   4695
  262.       End
  263.       Begin VB.ListBox lstDatabases 
  264.          Height          =   3435
  265.          Left            =   2640
  266.          Style           =   1  'Checkbox
  267.          TabIndex        =   13
  268.          ToolTipText     =   "Select (check) the databases that you want to create scripts for."
  269.          Top             =   1920
  270.          Visible         =   0   'False
  271.          Width           =   4695
  272.       End
  273.       Begin VB.TextBox txtLogin 
  274.          Height          =   285
  275.          Left            =   5160
  276.          TabIndex        =   8
  277.          ToolTipText     =   "Type the Login to use for SQL Server Authentication mode."
  278.          Top             =   960
  279.          Width           =   2175
  280.       End
  281.       Begin VB.ComboBox cboServers 
  282.          Height          =   315
  283.          ItemData        =   "frmMain.frx":0442
  284.          Left            =   2640
  285.          List            =   "frmMain.frx":0444
  286.          TabIndex        =   3
  287.          ToolTipText     =   "Choose a server from the list or type the server-name in the box."
  288.          Top             =   240
  289.          Width           =   4695
  290.       End
  291.       Begin VB.Frame fraAuthentication 
  292.          Caption         =   "Authentication mode:"
  293.          Height          =   855
  294.          Left            =   240
  295.          TabIndex        =   4
  296.          ToolTipText     =   "Choose your authentication mode."
  297.          Top             =   720
  298.          Width           =   3255
  299.          Begin VB.OptionButton optAuthMode 
  300.             Caption         =   "NT (Trusted)"
  301.             Height          =   255
  302.             Index           =   0
  303.             Left            =   120
  304.             TabIndex        =   5
  305.             ToolTipText     =   "NT Authentication."
  306.             Top             =   360
  307.             Width           =   1215
  308.          End
  309.          Begin VB.OptionButton optAuthMode 
  310.             Caption         =   "SQL Server"
  311.             Height          =   255
  312.             Index           =   1
  313.             Left            =   1800
  314.             TabIndex        =   6
  315.             ToolTipText     =   "MS SQL Server Authentication."
  316.             Top             =   360
  317.             Value           =   -1  'True
  318.             Width           =   1215
  319.          End
  320.       End
  321.       Begin VB.TextBox txtPassword 
  322.          Height          =   285
  323.          IMEMode         =   3  'DISABLE
  324.          Left            =   5160
  325.          PasswordChar    =   "*"
  326.          TabIndex        =   10
  327.          ToolTipText     =   "Type the password to use for SQL Server Authentication mode."
  328.          Top             =   1320
  329.          Width           =   2175
  330.       End
  331.       Begin VB.TextBox txtDirectory 
  332.          Height          =   285
  333.          Left            =   2640
  334.          TabIndex        =   18
  335.          ToolTipText     =   "Destination directory for the script-files."
  336.          Top             =   5520
  337.          Width           =   4335
  338.       End
  339.       Begin VB.CommandButton cmdBrowseDir 
  340.          Caption         =   "..."
  341.          BeginProperty Font 
  342.             Name            =   "Arial Black"
  343.             Size            =   8.25
  344.             Charset         =   0
  345.             Weight          =   400
  346.             Underline       =   0   'False
  347.             Italic          =   0   'False
  348.             Strikethrough   =   0   'False
  349.          EndProperty
  350.          Height          =   285
  351.          Left            =   6960
  352.          TabIndex        =   19
  353.          ToolTipText     =   "Browse for a destination directory for the script files."
  354.          Top             =   5520
  355.          Width           =   375
  356.       End
  357.       Begin VB.TextBox txtDelWeeks 
  358.          Alignment       =   1  'Right Justify
  359.          Height          =   285
  360.          Left            =   630
  361.          TabIndex        =   15
  362.          Text            =   "4"
  363.          ToolTipText     =   "Number of weeks to keep old script-files."
  364.          Top             =   2760
  365.          Width           =   375
  366.       End
  367.       Begin VB.CheckBox chkDelWeeks 
  368.          Caption         =   "Delete files older then:"
  369.          Height          =   375
  370.          Left            =   360
  371.          TabIndex        =   14
  372.          ToolTipText     =   "Check to delete old script-files."
  373.          Top             =   2400
  374.          Value           =   1  'Checked
  375.          Width           =   1935
  376.       End
  377.       Begin VB.Label lblDirectory 
  378.          Caption         =   "Destination directory:"
  379.          Height          =   255
  380.          Left            =   240
  381.          TabIndex        =   17
  382.          Top             =   5520
  383.          Width           =   2295
  384.       End
  385.       Begin VB.Label lblServer 
  386.          Caption         =   "Server:"
  387.          Height          =   255
  388.          Left            =   240
  389.          TabIndex        =   2
  390.          Top             =   240
  391.          Width           =   2295
  392.       End
  393.       Begin VB.Label lblDatabases 
  394.          Caption         =   "Databases:"
  395.          Height          =   255
  396.          Left            =   240
  397.          TabIndex        =   11
  398.          Top             =   1920
  399.          Width           =   2295
  400.       End
  401.       Begin VB.Label lblLogin 
  402.          Caption         =   "Login:"
  403.          Height          =   255
  404.          Left            =   3840
  405.          TabIndex        =   7
  406.          Top             =   960
  407.          Width           =   1215
  408.       End
  409.       Begin VB.Label lblPassword 
  410.          Caption         =   "Password:"
  411.          Height          =   255
  412.          Left            =   3840
  413.          TabIndex        =   9
  414.          Top             =   1320
  415.          Width           =   1215
  416.       End
  417.       Begin VB.Label lblDelWeeks 
  418.          Caption         =   "weeks."
  419.          Height          =   255
  420.          Left            =   1080
  421.          TabIndex        =   16
  422.          Top             =   2805
  423.          Width           =   975
  424.       End
  425.    End
  426.    Begin VB.TextBox txtLog 
  427.       Height          =   5895
  428.       Left            =   240
  429.       Locked          =   -1  'True
  430.       MultiLine       =   -1  'True
  431.       ScrollBars      =   3  'Both
  432.       TabIndex        =   20
  433.       Top             =   600
  434.       Visible         =   0   'False
  435.       Width           =   7455
  436.    End
  437.    Begin MSComctlLib.TabStrip tsPage 
  438.       Height          =   6495
  439.       Left            =   120
  440.       TabIndex        =   0
  441.       Top             =   120
  442.       Width           =   7695
  443.       _ExtentX        =   13573
  444.       _ExtentY        =   11456
  445.       _Version        =   393216
  446.       BeginProperty Tabs {1EFB6598-857C-11D1-B16A-00C0F0283628} 
  447.          NumTabs         =   3
  448.          BeginProperty Tab1 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
  449.             Caption         =   "Scripting"
  450.             Key             =   "Scripting"
  451.             Object.ToolTipText     =   "On this tab you select what you want to script."
  452.             ImageVarType    =   2
  453.          EndProperty
  454.          BeginProperty Tab2 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
  455.             Caption         =   "Options"
  456.             Key             =   "Options"
  457.             Object.ToolTipText     =   "On this tab you can select the scripting options."
  458.             ImageVarType    =   2
  459.          EndProperty
  460.          BeginProperty Tab3 {1EFB659A-857C-11D1-B16A-00C0F0283628} 
  461.             Caption         =   "Log"
  462.             Key             =   "Log"
  463.             Object.ToolTipText     =   "This tab contains the log of the scripting actions."
  464.             ImageVarType    =   2
  465.          EndProperty
  466.       EndProperty
  467.    End
  468.    Begin VB.Label lblStatus 
  469.       BorderStyle     =   1  'Fixed Single
  470.       Height          =   255
  471.       Left            =   0
  472.       TabIndex        =   26
  473.       ToolTipText     =   "Current activity of SQLScripter."
  474.       Top             =   7200
  475.       Width           =   7935
  476.    End
  477. Attribute VB_Name = "frmMain"
  478. Attribute VB_GlobalNameSpace = False
  479. Attribute VB_Creatable = False
  480. Attribute VB_PredeclaredId = True
  481. Attribute VB_Exposed = False
  482. Option Explicit
  483. Option Compare Text
  484. DefInt A-Z
  485. Private mobjApplication As New SQLDMO.Application
  486. Private Sub cboServers_Click()
  487.     cmdDisconnect_Click
  488.     If cboServers.ListIndex <> -1 Then
  489.         gobjServer.Name = cboServers.List(cboServers.ListIndex)
  490.         If CBool(cboServers.ItemData(cboServers.ListIndex)) Then
  491.             optAuthMode(0).Value = True
  492.             optAuthMode_Click 0
  493.         Else
  494.             optAuthMode(1).Value = True
  495.             optAuthMode_Click 1
  496.         End If
  497.     End If
  498. End Sub
  499. Private Sub chkDelWeeks_Click()
  500.     If chkDelWeeks.Value = 0 Then
  501.         With txtDelWeeks
  502.             .BackColor = vbButtonFace
  503.             .Enabled = False
  504.         End With
  505.     Else
  506.         With txtDelWeeks
  507.             .Enabled = True
  508.             .BackColor = vbWindowBackground
  509.         End With
  510.     End If
  511. End Sub
  512. Private Sub chkInsertScript_Click()
  513.     If chkInsertScript.Value = vbUnchecked Then
  514.         gsSelTables = vbNullString
  515.     End If
  516. End Sub
  517. Private Sub cmdBrowseDir_Click()
  518.     txtDirectory.Text = BrowseForFolder("Select a directory")
  519. End Sub
  520. Private Sub cmdClearLog_Click()
  521.     txtLog.Text = ""
  522.     gsUnattendedLog = ""
  523. End Sub
  524. Private Sub cmdDisconnect_Click()
  525.     On Error Resume Next
  526.     With gobjServer
  527.         If .VerifyConnection(SQLDMOConn_CurrentState) Then
  528.             StatusMessage "Disconnecting from server " & gobjServer.Name
  529.             .DisConnect
  530.         End If
  531.     End With
  532.     With lstDatabases
  533.         .Visible = False
  534.         .Clear
  535.     End With
  536.     fraServerOptions.Visible = False
  537.     cmdFetchDBs.Visible = True
  538. End Sub
  539. Private Sub cmdExit_Click()
  540.     Unload Me
  541. End Sub
  542. Private Sub cmdFetchDBs_Click()
  543.     Dim objDatabase As SQLDMO.Database
  544.     Dim i As Integer
  545.     Dim qryResults As SQLDMO.QueryResults
  546.     If Len(cboServers.Text) = 0 Then
  547.         MsgBox "You'll have to choose or enter a servername first.", vbExclamation, "Error!"
  548.         cboServers.SetFocus
  549.     Else
  550.         Me.MousePointer = vbArrowHourglass
  551.         With gobjServer
  552.             .Name = cboServers.Text
  553.             DoEvents
  554.             .ApplicationName = App.Title
  555.             If optAuthMode(1).Value = True Then
  556.                 .LoginSecure = False
  557.                 .Login = txtLogin.Text
  558.                 .Password = txtPassword.Text
  559.                 StatusMessage "Using Login " & txtLogin.Text
  560.             Else
  561.                 .Login = ""
  562.                 .Password = ""
  563.                 .LoginSecure = True
  564.                 StatusMessage "Using Trusted Connection"
  565.             End If
  566.             On Error GoTo ErrHandler
  567.             StatusMessage "Connecting to " & gobjServer.Name
  568.             .Connect
  569.             DoEvents
  570.             If .Issysadmin Then
  571.                 StatusMessage "User is sysadmin"
  572.                 fraServerOptions.Enabled = True
  573. '                chkOptionDTS.Value = 1
  574. '                chkOptionAlerts.Value = 1
  575. '                chkOptionJobs.Value = 1
  576. '                chkOptionLogins.Value = 1
  577. '                chkOptionBackup.Value = 1
  578.             Else
  579.                 StatusMessage "User is not a sysadmin"
  580.                 fraServerOptions.Enabled = False
  581.                 chkOptionDTS.Value = 2
  582.                 chkOptionAlerts.Value = 2
  583.                 chkOptionJobs.Value = 2
  584.                 chkOptionLogins.Value = 2
  585.                 chkOptionBackup.Value = 2
  586.             End If
  587.             StatusMessage "Fetching databases:"
  588.             On Error GoTo 0
  589.             For Each objDatabase In gobjServer.Databases
  590.                 'This freaky "On Error" hack needs to be used since the .IsUser method
  591.                 ' will raise an error (instead of returning False as it should) when called
  592.                 ' with an invalid login.
  593.                 On Error GoTo ErrNoUser
  594.                 If objDatabase.IsUser(.Login) Or .Issysadmin Then
  595. ResumeSaUser:
  596.                     If ((Not objDatabase.SystemObject) Or .Issysadmin) Then
  597.                         i = i + 1
  598.                         StatusMessage "- Found Database " & CStr(i) & ": " & objDatabase.Name
  599.                         lstDatabases.AddItem objDatabase.Name
  600.                     End If
  601.                 End If
  602. ResumeNoUser:
  603.             DoEvents
  604.             Next objDatabase
  605.             StatusMessage CStr(i) & " Databases found."
  606.         End With
  607.         Me.MousePointer = vbNormal
  608.         cmdFetchDBs.Visible = False
  609.         lstDatabases.Visible = True
  610.         StatusMessage "Ready."
  611.     End If
  612.     Exit Sub
  613. ErrHandler:
  614.     StatusMessage "Error: 0x" & Hex$(Err.Number) & vbTab & Error(Err.Number)
  615.     If Err.Number = &H80044818 Then
  616.         MsgBox "Your login credentials are not recognised by the server.", vbExclamation, "Error!"
  617.     Else
  618.         MsgBox "Error: 0x" & Hex$(Err.Number) & vbNewLine & Err.Description, vbExclamation, "Error!"
  619.     End If
  620.     Me.MousePointer = vbNormal
  621.     Exit Sub
  622. ErrNoUser:
  623.     If gobjServer.Issysadmin Then
  624.         Resume ResumeSaUser
  625.     Else
  626.         Resume ResumeNoUser
  627.     End If
  628. End Sub
  629. Private Sub cmdGenerate_Click()
  630.     Dim strServerName As String
  631.     Dim strDatabases As String
  632.     Dim vDatabases As Variant
  633.     Dim strDir As String
  634.     Dim msgResult As VbMsgBoxResult
  635.     Dim i As Integer
  636.     If chkInsertScript.Value = vbUnchecked Then
  637.         gsSelTables = vbNullString
  638.     End If
  639.     If Len(txtDirectory.Text) = 0 Then
  640.         MsgBox "Choose an output directory for the scripts first.", vbExclamation, "Error!"
  641.         On Error Resume Next
  642.         txtDirectory.SetFocus
  643.         Exit Sub
  644.     End If
  645.     If lstDatabases.SelCount = 0 And chkOptionDTS.Value <> 1 And chkOptionJobs.Value <> 1 And _
  646.        chkOptionLogins.Value <> 1 And chkOptionAlerts.Value <> 1 And chkOptionBackup.Value <> 1 Then
  647.         MsgBox "You haven't selected anything to script!", vbExclamation, "Error!"
  648.         Exit Sub
  649.     End If
  650.     Me.MousePointer = vbArrowHourglass
  651.     fraScripting.Enabled = False
  652.     fraServerOptions.Enabled = False
  653.     fraDBOptions.Enabled = False
  654.     cmdExit.Enabled = False
  655.     cmdDisconnect.Enabled = False
  656.     cmdGenerate.Enabled = False
  657.     tsPage.Tabs.Item("Log").Selected = True
  658.     strDir = Trim$(txtDirectory.Text)
  659.     If Right$(strDir, 1) <> "\" Then strDir = strDir & "\"
  660.     txtDirectory.Text = strDir
  661.     strServerName = gobjServer.Name
  662.     gsLogFile = strDir & Format(Date, "yyyymmdd") & "." & strServerName & "_Log.txt"
  663.     For i = 0 To lstDatabases.ListCount - 1
  664.         If lstDatabases.Selected(i) Then strDatabases = strDatabases & "," & lstDatabases.List(i)
  665.     Next i
  666.     vDatabases = Split(Mid$(strDatabases, 2, Len(strDatabases)), ",")
  667.     GenerateScripts strServerName, strDir, vDatabases, optAuthMode(0).Value, Trim$(txtLogin.Text), _
  668.                     txtPassword.Text, chkDelWeeks.Value = 1, CInt(txtDelWeeks.Text), _
  669.                     chkScriptDatabase.Value = 1, _
  670.                     chkDBOptionTables.Value = 1, chkDBOptionViews.Value = 1, _
  671.                     chkDBOptionSPs.Value = 1, chkDBOptionRules.Value = 1, _
  672.                     chkDBOptionDefaults.Value = 1, chkDBOptionRoles.Value = 1, _
  673.                     chkDBOptionFullText.Value = 1, chkDBOptionUsers.Value = 1, _
  674.                     chkDBOptionUDT.Value = 1, chkOptionDTS.Value = 1, _
  675.                     chkOptionAlerts.Value = 1, chkOptionJobs.Value = 1, _
  676.                     chkOptionLogins.Value = 1, chkOptionBackup.Value = 1, _
  677.                     chkSingleFile.Value = 1, chkDSFilePrefix.Value = 1, _
  678.                     gsSelTables, chkScriptDropsFirst.Value = 1
  679.                     
  680.     DoEvents
  681.     fraScripting.Enabled = True
  682.     fraServerOptions.Enabled = True
  683.     fraDBOptions.Enabled = True
  684.     Me.MousePointer = vbNormal
  685.     cmdExit.Enabled = True
  686.     cmdDisconnect.Enabled = True
  687.     cmdGenerate.Enabled = True
  688.     MsgBox "Scripting done.", vbInformation, "Information"
  689. End Sub
  690. Private Sub cmdSaveLog_Click()
  691.     SaveLog gsLogFile
  692.     MsgBox "Log saved as:" & vbNewLine & gsLogFile, vbInformation, "Information."
  693. End Sub
  694. Private Sub cmdSelectTables_Click()
  695.     Dim objTab  As SQLDMO.Table
  696.     Dim sDBName As String
  697.     Dim i       As Integer
  698.     ' Dim sTemp   As String
  699.     ' use the global gobjServer object
  700.     ' get the database name from the first selected entry in the list: lstDatabases.Selected(0)
  701.     If lstDatabases.SelCount > 0 Then
  702.         For i = 0 To lstDatabases.ListCount
  703.             If lstDatabases.Selected(i) Then
  704.                 sDBName = lstDatabases.List(i)
  705.                 Exit For
  706.             End If
  707.         Next
  708.         
  709.         If gobjServer.Databases(sDBName).Tables.Count > 0 Then
  710.             
  711.             Load frmSelectTables
  712.             
  713.             With frmSelectTables
  714.                 
  715.                 .lstTables.Clear
  716.                 
  717.                 For Each objTab In gobjServer.Databases(sDBName).Tables
  718.                 
  719.                     ' add each of the tables in the database to lstTables on frmSelectTables
  720.                     If Not objTab.SystemObject Then
  721.                         .lstTables.AddItem objTab.Name
  722.                         ' sTemp = sTemp & objTab.Name & vbNewLine
  723.                         If InStr(1, gsSelTables, objTab.Name & ",") <> 0 Then
  724.                             .lstTables.Selected(.lstTables.NewIndex) = True
  725.                         End If
  726.                         
  727.                     End If
  728.                     
  729.                 Next
  730.                 
  731.                 ' when the form is hidden grab the list of selected tables and store it in a mod level var
  732.                 .Show vbModal
  733.                 
  734.                 If .lstTables.ListCount > 0 Then
  735.                     If .lstTables.SelCount > 0 Then
  736.                         
  737.                         gsSelTables = vbNullString
  738.                         
  739.                         For i = 0 To .lstTables.ListCount - 1
  740.                             If .lstTables.Selected(i) Then
  741.                                 gsSelTables = gsSelTables & .lstTables.List(i) & ","
  742.                             End If
  743.                         Next
  744.                         
  745.                         If gsSelTables <> vbNullString Then
  746.                             chkInsertScript.Value = vbChecked
  747.                         End If
  748.                        
  749.                     Else
  750.                         gsSelTables = vbNullString
  751.                         chkInsertScript.Value = vbUnchecked
  752.                     End If
  753.                 End If
  754.                 
  755.             End With
  756.             
  757.         Else
  758.             MsgBox "There are no user tables in the selected database", vbOKOnly + vbInformation
  759.         End If
  760.     Else
  761.         MsgBox "Please select a database first", vbOKOnly + vbInformation
  762.     End If
  763.         
  764. End Sub
  765. Private Sub Form_Load()
  766.     Dim objServerGroup As SQLDMO.ServerGroup
  767.     Dim objRegisteredServer As SQLDMO.RegisteredServer
  768.     Dim i As Integer, j As Integer
  769.     For Each objServerGroup In mobjApplication.ServerGroups
  770.         
  771.         For Each objRegisteredServer In objServerGroup.RegisteredServers
  772.             
  773.             cboServers.AddItem objRegisteredServer.Name
  774.             cboServers.ItemData(cboServers.NewIndex) = CStr(objRegisteredServer.UseTrustedConnection)
  775.         
  776.         Next objRegisteredServer
  777.     Next objServerGroup
  778.     ' set options specified on command line
  779.     If Len(gsServerNameSW) > 0 Then
  780.         cboServers.Text = gsServerNameSW
  781.         If Len(gsLoginSW) > 0 Then
  782.             optAuthMode(1).Value = True
  783.             optAuthMode_Click 1
  784.             txtLogin.Text = gsLoginSW
  785.             txtPassword.Text = gsPasswordSW
  786.         Else
  787.             optAuthMode(0).Value = True
  788.             optAuthMode_Click 0
  789.         End If
  790.         If gbDelOldFilesSW Then
  791.             chkDelWeeks.Value = 1
  792.             txtDelWeeks.Text = CStr(giDelWeeksSW)
  793.         Else
  794.             chkDelWeeks.Value = 0
  795.         End If
  796.         txtDirectory.Text = gsDestDirSW
  797.         cmdFetchDBs_Click
  798.         
  799.         ' set main options
  800.         chkScriptDatabase.Value = Abs(CInt(gbScriptDB))
  801.         chkDBOptionTables.Value = Abs(CInt(gbScriptTabs))
  802.         chkDBOptionViews.Value = Abs(CInt(gbScriptViews))
  803.         chkDBOptionSPs.Value = Abs(CInt(gbScriptSPs))
  804.         chkDBOptionRules.Value = Abs(CInt(gbScriptRules))
  805.         chkDBOptionDefaults.Value = Abs(CInt(gbScriptDefs))
  806.         chkDBOptionRoles.Value = Abs(CInt(gbScriptRoles))
  807.         chkDBOptionUsers.Value = Abs(CInt(gbScriptUsers))
  808.         chkDBOptionUDT.Value = Abs(CInt(gbScriptUDTs))
  809.         chkDBOptionFullText.Value = Abs(CInt(gbScriptFTCs))
  810.         chkSingleFile.Value = Abs(CInt(gbObjsInSepFiles))
  811.         chkDSFilePrefix.Value = Abs(CInt(gbDatePrefix))
  812.         chkScriptDropsFirst.Value = Abs(CInt(gbScriptDrops1st))
  813.         
  814.         If gsSelTables <> vbNullString Then
  815.             chkInsertScript.Value = vbChecked
  816.         Else
  817.             chkInsertScript.Value = vbUnchecked
  818.         End If
  819.         
  820.         On Error GoTo ErrHandler
  821.         If gobjServer.Issysadmin Then
  822.             chkOptionDTS.Value = Abs(CInt(gbSaveDTSPackagesSW))
  823.             chkOptionAlerts.Value = Abs(CInt(gbScriptAlertsSW))
  824.             chkOptionJobs.Value = Abs(CInt(gbScriptAgentJobsSW))
  825.             chkOptionLogins.Value = Abs(CInt(gbScriptServerLoginsSW))
  826.             chkOptionBackup.Value = Abs(CInt(gbScriptBackupDevicesSW))
  827.             If Not IsEmpty(gvDatabasesSW) Then
  828.                 For i = 0 To lstDatabases.ListCount - 1
  829.                     For j = 0 To UBound(gvDatabasesSW)
  830.                         If lstDatabases.List(i) = gvDatabasesSW(j) Then
  831.                             lstDatabases.Selected(i) = True
  832.                             Exit For
  833.                         End If
  834.                     Next j
  835.                 Next i
  836.             End If
  837.         End If
  838.     End If
  839.     Exit Sub
  840. ErrHandler:
  841.     MsgBox "Cannot connect to server " & cboServers.Text, vbExclamation, "Error!"
  842. End Sub
  843. Private Sub Form_Unload(Cancel As Integer)
  844.     On Error Resume Next
  845.     gobjServer.Close
  846.     Set gobjServer = Nothing
  847.     Set mobjApplication = Nothing
  848. End Sub
  849. Private Sub optAuthMode_Click(Index As Integer)
  850.     If Index = 0 Then
  851.         With txtLogin
  852.             .BackColor = vbButtonFace
  853.             .Enabled = False
  854.         End With
  855.         With txtPassword
  856.             .BackColor = vbButtonFace
  857.             .Enabled = False
  858.         End With
  859.     Else
  860.         With txtLogin
  861.             .Enabled = True
  862.             .BackColor = vbWindowBackground
  863.         End With
  864.         With txtPassword
  865.             .Enabled = True
  866.             .BackColor = vbWindowBackground
  867.         End With
  868.     End If
  869. End Sub
  870. Private Sub tsPage_Click()
  871.     Select Case tsPage.SelectedItem.Key
  872.         Case "Scripting"
  873.             txtLog.Visible = False
  874.             fraServerOptions.Visible = False
  875.             fraDBOptions.Visible = False
  876.             fraScripting.Visible = True
  877.         Case "Log"
  878.             fraScripting.Visible = False
  879.             fraServerOptions.Visible = False
  880.             fraDBOptions.Visible = False
  881.             txtLog.Visible = True
  882.         Case "Options"
  883.             fraScripting.Visible = False
  884.             txtLog.Visible = False
  885.             fraDBOptions.Visible = True
  886.             fraServerOptions.Visible = True
  887.     End Select
  888. End Sub
  889.