home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / po7_win / object10 / vbsql.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1994-11-07  |  28.2 KB  |  864 lines

  1. VERSION 2.00
  2. Begin Form frmVBSQL 
  3.    BorderStyle     =   3  'Fixed Double
  4.    Caption         =   "VB*SQL"
  5.    ClientHeight    =   5835
  6.    ClientLeft      =   660
  7.    ClientTop       =   1815
  8.    ClientWidth     =   10800
  9.    Height          =   6525
  10.    Icon            =   VBSQL.FRX:0000
  11.    Left            =   600
  12.    LinkTopic       =   "Form1"
  13.    MaxButton       =   0   'False
  14.    ScaleHeight     =   5835
  15.    ScaleWidth      =   10800
  16.    Top             =   1185
  17.    Width           =   10920
  18.    Begin OraData OraData1 
  19.       AllowMoveLast   =   -1  'True
  20.       AutoBinding     =   -1  'True
  21.       Caption         =   " Previous Record - Next Record"
  22.       Connect         =   ""
  23.       DatabaseName    =   ""
  24.       Height          =   495
  25.       HiddenName      =   "OraData1"
  26.       Left            =   5400
  27.       Options         =   0
  28.       ReadOnly        =   0   'False
  29.       RecordSource    =   ""
  30.       TabIndex        =   6
  31.       Top             =   5280
  32.       TrailingBlanks  =   0   'False
  33.       Width           =   3975
  34.    End
  35.    Begin CommonDialog CMRun 
  36.       Left            =   4920
  37.       Top             =   0
  38.    End
  39.    Begin CommonDialog CMFilePrint 
  40.       Left            =   2160
  41.       Top             =   0
  42.    End
  43.    Begin CommandButton cmdExit 
  44.       Caption         =   "Exit"
  45.       Height          =   495
  46.       Left            =   9480
  47.       TabIndex        =   7
  48.       Top             =   5280
  49.       Width           =   1215
  50.    End
  51.    Begin CommandButton cmdAdd 
  52.       Caption         =   "Add"
  53.       Height          =   495
  54.       Left            =   4080
  55.       TabIndex        =   5
  56.       Top             =   5280
  57.       Width           =   1215
  58.    End
  59.    Begin CommandButton cmdDelete 
  60.       Caption         =   "Delete"
  61.       Height          =   495
  62.       Left            =   2760
  63.       TabIndex        =   4
  64.       Top             =   5280
  65.       Width           =   1215
  66.    End
  67.    Begin TgDemo OutTable 
  68.       AllowArrows     =   -1  'True
  69.       AllowTabs       =   -1  'True
  70.       DataSource      =   "OraData1"
  71.       Editable        =   -1  'True
  72.       EditDropDown    =   -1  'True
  73.       ExposeCellMode  =   0  'Expose upon selection
  74.       FetchMode       =   0  'By cell
  75.       HeadingHeight   =   1
  76.       Height          =   2895
  77.       HorzLines       =   0  'None
  78.       Layout          =   VBSQL.FRX:0302
  79.       LayoutIndex     =   1
  80.       Left            =   120
  81.       LinesPerRow     =   1
  82.       MarqueeUnique   =   -1  'True
  83.       SplitPropsGlobal=   -1  'True
  84.       SplitTabMode    =   0  'Don't tab across splits
  85.       TabCapture      =   0   'False
  86.       TabIndex        =   1
  87.       Top             =   2280
  88.       UseBookmarks    =   -1  'True
  89.       Width           =   10575
  90.       WrapCellPointer =   0   'False
  91.    End
  92.    Begin CommonDialog CMSaveAs 
  93.       Left            =   3960
  94.       Top             =   0
  95.    End
  96.    Begin CommonDialog CMOpen 
  97.       Left            =   4440
  98.       Top             =   0
  99.    End
  100.    Begin CommonDialog CMFont 
  101.       Left            =   2640
  102.       Top             =   0
  103.    End
  104.    Begin TextBox txtConnection 
  105.       Height          =   285
  106.       Left            =   8160
  107.       TabIndex        =   11
  108.       TabStop         =   0   'False
  109.       Top             =   120
  110.       Width           =   2535
  111.    End
  112.    Begin CommandButton cmdClear 
  113.       Caption         =   "Clear"
  114.       Height          =   495
  115.       Left            =   1440
  116.       TabIndex        =   3
  117.       Top             =   5280
  118.       Width           =   1215
  119.    End
  120.    Begin CommandButton cmdExecute 
  121.       Caption         =   "Execute"
  122.       Height          =   495
  123.       Left            =   120
  124.       TabIndex        =   2
  125.       Top             =   5280
  126.       Width           =   1215
  127.    End
  128.    Begin TextBox txtSQL 
  129.       Height          =   1455
  130.       Left            =   120
  131.       MultiLine       =   -1  'True
  132.       ScrollBars      =   2  'Vertical
  133.       TabIndex        =   0
  134.       Text            =   "select * from emp;"
  135.       Top             =   480
  136.       Width           =   10575
  137.    End
  138.    Begin Label Label3 
  139.       AutoSize        =   -1  'True
  140.       Caption         =   "Connection:"
  141.       Height          =   195
  142.       Left            =   7080
  143.       TabIndex        =   8
  144.       Top             =   120
  145.       Width           =   1035
  146.    End
  147.    Begin Label Label2 
  148.       AutoSize        =   -1  'True
  149.       Caption         =   "Dynaset:"
  150.       Height          =   195
  151.       Left            =   120
  152.       TabIndex        =   10
  153.       Top             =   2040
  154.       Width           =   765
  155.    End
  156.    Begin Label Label1 
  157.       AutoSize        =   -1  'True
  158.       Caption         =   "SQL Statement:"
  159.       Height          =   195
  160.       Left            =   120
  161.       TabIndex        =   9
  162.       Top             =   240
  163.       Width           =   1350
  164.    End
  165.    Begin Menu mFile 
  166.       Caption         =   "&File"
  167.       Begin Menu mFilePrint 
  168.          Caption         =   "&Print"
  169.       End
  170.       Begin Menu mFilePrintSetup 
  171.          Caption         =   "P&rint Setup"
  172.       End
  173.       Begin Menu mFileExit 
  174.          Caption         =   "E&xit"
  175.       End
  176.    End
  177.    Begin Menu mSQL 
  178.       Caption         =   "S&QL"
  179.       Begin Menu mSQLOpen 
  180.          Caption         =   "&Open"
  181.       End
  182.       Begin Menu mSQLRun 
  183.          Caption         =   "&Run"
  184.       End
  185.       Begin Menu mSQLSaveAs 
  186.          Caption         =   "Save &As"
  187.       End
  188.    End
  189.    Begin Menu mSession 
  190.       Caption         =   "&Session"
  191.       Begin Menu mSessionBeginTrans 
  192.          Caption         =   "&Begin Transaction"
  193.       End
  194.       Begin Menu mSessionCommit 
  195.          Caption         =   "&Commit"
  196.       End
  197.       Begin Menu mSessionRollback 
  198.          Caption         =   "&Rollback"
  199.       End
  200.    End
  201.    Begin Menu mDynaset 
  202.       Caption         =   "&Dynaset"
  203.       Begin Menu mDynasetFont 
  204.          Caption         =   "&Font"
  205.       End
  206.       Begin Menu mDynasetGraph 
  207.          Caption         =   "&Graph"
  208.       End
  209.       Begin Menu mDynasetHeadings 
  210.          Caption         =   "&Headings"
  211.       End
  212.       Begin Menu mDynasetReadOnly 
  213.          Caption         =   "&ReadOnly"
  214.       End
  215.       Begin Menu mRSetSaveAs 
  216.          Caption         =   "Save &As"
  217.          Begin Menu mDSetCommaDel 
  218.             Caption         =   "&Comma Delimited"
  219.          End
  220.          Begin Menu mDynasetSQLScript 
  221.             Caption         =   "&SQL Script"
  222.          End
  223.          Begin Menu mDSetTabDel 
  224.             Caption         =   "&Tab Delimited"
  225.          End
  226.       End
  227.    End
  228.    Begin Menu mHelp 
  229.       Caption         =   "&Help"
  230.       Begin Menu mHelpContents 
  231.          Caption         =   "&Contents"
  232.       End
  233.       Begin Menu mHelpAbout 
  234.          Caption         =   "&About VB*SQL..."
  235.       End
  236.    End
  237. Option Explicit
  238. Sub cmdAdd_Click ()
  239.  'Add a new record iff editable=true
  240.  If OutTable.Editable = True Then
  241.   OraData1.Recordset.DbAddNew
  242.  Else
  243.   Call RaiseError("Error", "The Dynaset is currently marked READONLY")
  244.  End If
  245. End Sub
  246. Sub cmdClear_Click ()
  247.  txtSQL = ""
  248.  OraData1.RecordSource = "select * from dual where 1=0"
  249.  OraData1.Refresh
  250.  mDynaset.Enabled = False
  251.  cmdDelete.Enabled = False
  252.  cmdAdd.Enabled = False
  253. End Sub
  254. Sub cmdDelete_Click ()
  255.  'Delete's the current record iff editable=true
  256.  If OutTable.Editable = True Then
  257.   'Is there any data? Currently only RecordCount can tell
  258.   'you that but it will retrieve all of the records first.
  259.   If OraData1.Recordset.BOF = True And OraData1.Recordset.EOF = True Then
  260.    Call RaiseError("Error", "No row(s) to delete.")
  261.   Else
  262.    OraData1.Recordset.DbDelete
  263.   End If
  264.  Else
  265.   Call RaiseError("Error", "The Dynaset is currently marked READONLY")
  266.  End If
  267. End Sub
  268. Sub cmdExecute_click ()
  269.  ExecuteSQLStatement (txtSQL)
  270.  txtSQL.SetFocus
  271. End Sub
  272. Sub cmdExit_Click ()
  273.  'Simply call FILE->Exit
  274.  Call mFileExit_click
  275. End Sub
  276. 'Attempt to execute a SQL statement or VB*SQL Command.
  277. 'SELECT will return a dynaset
  278. 'DESC will describe an object(slightly different than SQL*Plus).
  279. Sub ExecuteSQLStatement (stext As String)
  280. Dim SQLStatement$, DescSQL$, ObjectName$, Owner$, ObjectType$
  281. Dim IsTerm%, Verb%
  282. Dim DDesc As Object
  283. Dim en%
  284. Dim et$
  285.  ObjectName$ = ""
  286.  Owner$ = UCase$(Trim$(UserName$))     'Default Owner
  287.  'Strip spaces
  288.  SQLStatement$ = stext
  289.  Call ConvertCRLFtoSpace(SQLStatement$)
  290.  SQLStatement$ = Trim$(SQLStatement)
  291.  If SQLStatement$ = "" Then
  292.   Call RaiseError("Error", "No SQL statement was specified.")
  293.  Else
  294.  'This might take a while
  295.  Screen.MousePointer = HOURGLASS
  296.  On Error GoTo OraError
  297.   'Strip semicolon or slash(side effect)
  298.   IsTerm% = IsTerminated(SQLStatement$)
  299.   'Determine the SQL verb, object and owner
  300.   Verb% = SQLvoo(SQLStatement$, ObjectName$, Owner$)
  301.  Select Case Verb%
  302.    Case SQL_VERB_SELECT
  303.     'A SELECT will return a dynaset
  304.     OraData1.RecordSource = SQLStatement$
  305.     OraData1.Refresh
  306.     mDynaset.Enabled = True
  307.     cmdDelete.Enabled = True
  308.     cmdAdd.Enabled = True
  309.    Case SQL_VERB_DESCRIBE
  310.     DescSQL$ = "Select owner Owner, object_name ObjectName, object_type ObjectType from all_objects where object_name='" + ObjectName$ + "'"
  311.     'Look for this object as owned by User$
  312.     Set DDesc = OraDatabase.DbCreateDynaset(DescSQL$ + " and owner='" + Owner$ + "'", 0&)
  313.     DDesc.DbMoveFirst
  314.     If DDesc.RecordCount = 0 Then
  315.      'Look for this object as owned by anyone
  316.      Set DDesc = OraDatabase.DbCreateDynaset(DescSQL$, 0&)
  317.      DDesc.DbMoveFirst
  318.     End If
  319.     If DDesc.RecordCount = 0 Then
  320.       Call RaiseInfo("Information", "Object " + ObjectName$ + " does not exist.")
  321.       DescSQL$ = ""
  322.     Else
  323.      'Set the new owner and objecttype
  324.      Owner$ = DDesc.Fields("owner").value
  325.      ObjectType$ = DDesc.Fields("objecttype")
  326.      If ObjectType$ = "TABLE" Or ObjectType$ = "VIEW" Then
  327.       DescSQL$ = "Select table_name ""Table"", column_name ""Columns"",nullable ""Null?"" , data_type ""Data Type"", data_length ""Length"" , data_precision ""Precision"", data_scale ""Scale"" from all_tab_columns where table_name='" + ObjectName$ + "' and owner='" + Owner$ + "' order by column_id"
  328.      ElseIf ObjectType$ = "PACKAGE" Or ObjectType$ = "FUNCTION" Or ObjectType$ = "FUNCTION BODY" Or ObjectType$ = "PROCEDURE" Then
  329.       DescSQL$ = "Select text ""Source"" from user_source where type='" + ObjectType$ + "' and name='" + ObjectName$ + "' order by line"
  330.      ElseIf ObjectType$ = "SEQUENCE" Then
  331.       DescSQL$ = "select sequence_name SequenceName, min_value MinValue, max_value MaxValue, increment_by ""Increment"" from all_sequences where sequence_owner='" + Owner$ + "' and sequence_name='" + ObjectName$ + "'"
  332.      ElseIf ObjectType$ = "INDEX" Then
  333.       DescSQL$ = "select index_name IndexName, table_owner TableOwner, table_name TableName , table_type TableType, uniqueness from all_indexes where owner='" + Owner$ + "' and index_name='" + ObjectName$ + "'"
  334.      Else
  335.       Call RaiseInfo("Information", "Object " + ObjectName$ + " is a(n) " + ObjectType$)
  336.       DescSQL$ = ""
  337.      End If
  338.     End If
  339.     If DescSQL$ <> "" Then
  340.      'A DESC will return a dynaset
  341.      OraData1.RecordSource = DescSQL$
  342.      OraData1.Refresh
  343.     End If
  344.     cmdDelete.Enabled = False
  345.     cmdAdd.Enabled = False
  346.    Case Else
  347.     'Any SQL except SELECT will not return anything
  348.     OraDatabase.DbExecuteSQL (SQLStatement$)
  349.     mDynaset.Enabled = False
  350.     cmdDelete.Enabled = False
  351.     cmdAdd.Enabled = False
  352.    End Select
  353.    'Reset the mouse pointer
  354.    Screen.MousePointer = DEFAULT
  355.   End If
  356.  Exit Sub
  357. OraError:
  358.  Screen.MousePointer = DEFAULT
  359.  frmOraError.Show MODAL
  360.  Exit Sub
  361. End Sub
  362. Sub Form_Load ()
  363.  'Initialize Grid settings
  364.  OutTable.SelectMode = 1
  365.  OutTable.Headings = True
  366.  OutTable.Editable = False
  367.  OutTable.MarqueeStyle = 3
  368.  'Values
  369.  '0 - Dotted Cell Border (Default)
  370.  '1 - Solid Cell Border
  371.  '2 - Highlight Cell
  372.  '3 - Highlight Row
  373.  '4 - Highlight Row & Cell
  374.  '5 - None
  375.  'Initialize Menu settings
  376.  mDynasetHeadings.Checked = True
  377.  mDynasetReadonly.Checked = True
  378.  mDynaset.Enabled = False
  379.  mSessionCommit.Enabled = False
  380.  mSessionRollback.Enabled = False
  381.  'Initialize buttons
  382.  cmdAdd.Enabled = False
  383.  cmdDelete.Enabled = False
  384.  Call CenterForm(frmVBSQL)
  385.  'For display purposes
  386.  If DatabaseName$ = "" Then
  387.   txtConnection = UserName$ + "@<local host>"
  388.  Else
  389.   txtConnection = UserName$ + "@" + DatabaseName$
  390.  End If
  391.  OraData1.DatabaseName = DatabaseName$
  392.  OraData1.Connect = Connect$
  393. End Sub
  394. 'Check for a 'terminator' of sorts. In SQL*Plus a statement
  395. 'is terminated(and executed) after a semicolon or forward
  396. 'slash(and a return). This function also has the effect of
  397. 'stripping spaces iff the statement was terminated by a
  398. 'semicolon or forward slash
  399. Function IsTerminated (SQLStatement As String) As Integer
  400.  Dim Temp$
  401.  'Remove any trailing spaces
  402.  Temp$ = RTrim$(SQLStatement$)
  403.  'Check for semicolon or forward slash
  404.  If Right$(Temp$, 1) = ";" Or Right$(Temp$, 1) = "/" Then
  405.   'Strip the semicolon or forward slash and spaces
  406.   SQLStatement$ = Trim$(Left$(Temp$, Len(Temp$) - 1))
  407.   IsTerminated = True
  408.  Else
  409.   IsTerminated = False
  410.  End If
  411. End Function
  412. Sub mDSetCommaDel_Click ()
  413.  Call SaveToFile("Comma Delimited(*.TXT)|*.TXT|All Files(*.*)|*.*", "TXT", ",")
  414. End Sub
  415. Sub mDSetTabDel_Click ()
  416.  Call SaveToFile("Tab Delimited(*.TXT)|*.TXT|All Files(*.*)|*.*", "TXT", Chr(9))
  417. End Sub
  418. Sub mDynasetFont_Click ()
  419.  'Only get the ANSI and Screen Fonts
  420.  CMFont.Flags = CF_ANSIONLY Or CF_SCREENFONTS
  421.  CMFont.Action = DLG_FONT
  422.  'If the user didn't hit cancel and there is a font
  423.  If Err = 0 And CMFont.FontName <> "" Then
  424.   OutTable.FontName = CMFont.FontName
  425.  End If
  426. End Sub
  427. Sub mDynasetGraph_Click ()
  428.  Set GraphDyn = OraData1.Recordset '.DbClone
  429.  frmGraphO.Show MODAL
  430.  Unload frmGraphO
  431. End Sub
  432. Sub mDynasetHeadings_Click ()
  433.  If mDynasetHeadings.Checked = True Then
  434.   OutTable.Headings = False
  435.   mDynasetHeadings.Checked = False
  436.  Else
  437.   OutTable.Headings = True
  438.   mDynasetHeadings.Checked = True
  439.  End If
  440. End Sub
  441. Sub mDynasetReadOnly_Click ()
  442.  'Mark the grid readonly/readwrite
  443.  If OutTable.Editable = True Then
  444.   OutTable.Editable = False
  445.   mDynasetReadonly.Checked = True
  446.  Else
  447.   OutTable.Editable = True
  448.   mDynasetReadonly.Checked = False
  449.  End If
  450. End Sub
  451. Sub mDynasetSQLScript_Click ()
  452.  Call SaveToSQLScript("SQL Script(*.SQL)|*.SQL|All Files(*.*)|*.*", "SQL", ",")
  453. End Sub
  454. Sub mFileExit_click ()
  455.  'Commit and exit
  456.  If mSessionBeginTrans.Checked = True Then
  457.   OraSession.DbCommitTrans
  458.  End If
  459.  Unload frmVBSQL
  460. End Sub
  461. Sub mFilePrint_Click ()
  462.  'Print the current form
  463.  CMFilePrint.Flags = 0
  464.  CMFilePrint.Action = DLG_PRINT
  465.  frmVBSQL.PrintForm
  466. End Sub
  467. Sub mFilePrintSetup_Click ()
  468.  'Display the print setup dialog
  469.  CMFilePrint.Flags = PD_PRINTSETUP
  470.  CMFilePrint.Action = DLG_PRINT
  471. End Sub
  472. Sub mHelpAbout_Click ()
  473.  frmAbout.Show MODAL
  474. End Sub
  475. Sub mHelpContents_Click ()
  476.  Call RaiseInfo("Warning", "Help not yet implemented.")
  477.  'Send an F1 to the app which will cause the help file
  478.  'listed in the project options to be opened.
  479.  'SendKeys "{F1}"
  480. End Sub
  481. Sub mSessionBeginTrans_Click ()
  482.  'Begin a transaction and set menus
  483.  mSessionBeginTrans.Checked = True
  484.  mSessionBeginTrans.Enabled = False
  485.  mSessionCommit.Enabled = True
  486.  mSessionRollback.Enabled = True
  487.  OraSession.DbBeginTrans
  488. End Sub
  489. Sub mSessionCommit_Click ()
  490.  'Commit a transaction and set menus
  491.  mSessionBeginTrans.Checked = False
  492.  mSessionBeginTrans.Enabled = True
  493.  mSessionCommit.Enabled = False
  494.  mSessionRollback.Enabled = False
  495.  OraSession.DbCommitTrans
  496. End Sub
  497. Sub mSessionRollback_Click ()
  498.  'Roolback a transaction and set menus
  499.  mSessionBeginTrans.Checked = False
  500.  mSessionBeginTrans.Enabled = True
  501.  mSessionCommit.Enabled = False
  502.  mSessionRollback.Enabled = False
  503.  OraSession.DbRollback
  504. End Sub
  505. Sub mSQLOpen_Click ()
  506.  Dim TextLine$, Filename$
  507.  Dim FNum%
  508.  'Init Variables
  509.  TextLine$ = ""
  510.  On Error GoTo SQLOpenCancel
  511.  'Initialize the Open file dialog
  512.  CMOpen.Filter = "SQL Scripts(*.SQL)|*.SQL|All Files(*.*)|*.*"
  513.  CMOpen.CancelError = True
  514.  CMOpen.Action = DLG_FILE_OPEN
  515.  Filename$ = CMOpen.Filename
  516.   If Filename$ <> "" And Dir$(Filename$) <> "" Then
  517.     FNum% = FreeFile
  518.     Open Filename$ For Input As FNum%
  519.     txtSQL = ""
  520.     While Not EOF(FNum%)
  521.      Line Input #FNum%, TextLine$  ' Get complete line.
  522.      txtSQL = txtSQL + TextLine$ + Chr$(13) + Chr$(10)
  523.     Wend
  524.     Close FNum% 'Close file.
  525.   End If
  526. SQLOpenCancel:
  527.  Exit Sub
  528. End Sub
  529. Sub mSQLRun_Click ()
  530.  Dim TextLine$, Filename$
  531.  'Init Variables
  532.  TextLine$ = ""
  533.  On Error GoTo SQLRunCancel
  534.  'Initialize the Open file dialog
  535.  CMRun.Filter = "SQL Scripts(*.SQL)|*.SQL|All Files(*.*)|*.*"
  536.  CMRun.DialogTitle = "Run"
  537.  CMRun.CancelError = True
  538.  CMRun.Action = DLG_FILE_OPEN
  539.  Filename$ = CMRun.Filename
  540.  Call RunSQLScript(Filename$)
  541. SQLRunCancel:
  542. Exit Sub
  543. End Sub
  544. Sub mSQLSaveAs_Click ()
  545.  Dim TextLine$, Filename$
  546.  Dim FNum%
  547.  'Init Variables
  548.  TextLine$ = ""
  549.  On Error GoTo SQLSaveAsCancel:
  550.  'Initialize the SaveAs file dialog
  551.  CMSaveAs.Filter = "SQL Scripts(*.SQL)|*.SQL|All Files(*.*)|*.*"
  552.  CMSaveAs.DefaultExt = "SQL"
  553.  CMSaveAs.CancelError = True
  554.  CMSaveAs.Action = DLG_FILE_SAVE
  555.  Filename$ = CMSaveAs.Filename
  556.  'Write the sql to a file
  557.  If Filename$ <> "" Then
  558.    FNum% = FreeFile
  559.    Open Filename$ For Output As FNum%
  560.    'TextLine$ = txtSQL
  561.    Print #FNum%, txtSQL ' Write complete line.
  562.    Close FNum% 'Close file.
  563.  End If
  564. SQLSaveAsCancel:
  565.  Exit Sub
  566. End Sub
  567. Sub OraData1_Error (DataErr As Integer, Response As Integer)
  568.  frmOraError.Show MODAL
  569.  Response = DATA_ERRCONTINUE
  570. End Sub
  571. Sub OutTable_DblClick ()
  572.  If OutTable.Editable = False Then
  573.   Call RaiseError("Error", "The Dynaset is currently marked READONLY")
  574.  End If
  575. End Sub
  576. Sub OutTable_KeyPress (KeyAscii As Integer)
  577.  If KeyAscii = KEY_ESCAPE Then
  578.   OutTable.DataChanged = False
  579.   OutTable.Modified = False ' Nullify user's editing
  580.   OutTable.EditActive = False   ' Exit edit mode
  581.  End If
  582. End Sub
  583. Sub RunSQLScript (Filename As String)
  584.  Dim SQLStatement$, CurrentLine$
  585.  Dim FNum%
  586.  SQLStatement$ = ""
  587.  CurrentLine$ = ""
  588.  On Error GoTo RunSQLError
  589.  If Filename$ <> "" And Dir$(Filename$) <> "" Then
  590.   FNum% = FreeFile
  591.   Open Filename$ For Input As FNum%
  592.   'txtSQL = ""
  593.   While Not EOF(FNum%)
  594.     Line Input #FNum%, CurrentLine$
  595.     SQLStatement$ = SQLStatement$ + Trim(CurrentLine$)
  596.     If Len(SQLStatement$) < 1 Then
  597.      'do nothing
  598.     ElseIf Left$(SQLStatement$, 2) = "--" Or UCase$(Left$(SQLStatement$, 3)) = "REM" Then
  599.      Call RaiseInfo("Info", "Found Remark=" + SQLStatement$)
  600.      SQLStatement$ = ""
  601.     ElseIf Right$(SQLStatement$, 1) = ";" Or Right$(SQLStatement$, 1) = "/" Then
  602.      'Need to strip the ; or /
  603.      SQLStatement$ = Left$(SQLStatement$, Len(SQLStatement$) - 1)
  604.      Call RaiseInfo("Info", "Execute SQL=" + SQLStatement$)
  605.      txtSQL = SQLStatement$ 'I need to reference txtSQL here. I'd rather not.
  606.      ExecuteSQLStatement (SQLStatement$)
  607.      SQLStatement$ = ""
  608.     Else
  609.      SQLStatement$ = SQLStatement$ + " "
  610.     End If
  611.   Wend
  612.   Close FNum% 'Close file.
  613.  End If
  614.  Exit Sub
  615. RunSQLError:
  616.  Call RaiseError("Error", "Error Reading " + Filename$)
  617.  Exit Sub
  618. End Sub
  619. 'Save a Dynaset to a file given a particular file extension and data delimeter
  620. Sub SaveToFile (Filter As String, DefaultExt As String, Delimeter As String)
  621.  Dim TextLine$, Filename$, FieldName$, Spaces$
  622.  Dim FNum%, FieldCount%, i%, NSpaces%
  623.  Dim FieldValue As Variant
  624.  Dim flds() As Object
  625.  'Init/Declare Variables
  626.  TextLine$ = ""
  627.  Dim DSClone As Object
  628.  On Error GoTo SaveToCancel
  629.  'Initialize the SaveAs file dialog
  630.  CMSaveAs.Filter = Filter$
  631.  CMSaveAs.DefaultExt = DefaultExt$
  632.  CMSaveAs.CancelError = True
  633.  CMSaveAs.Action = DLG_FILE_SAVE
  634.  Filename$ = CMSaveAs.Filename
  635.  'On Error GoTo SaveToError
  636.  If Filename$ <> "" Then
  637.    'This might take a while
  638.    Screen.MousePointer = HOURGLASS
  639.    'Find a free file
  640.    FNum% = FreeFile
  641.    Open Filename$ For Output As FNum%
  642.    'Clone the RecordSet since that will prevent the grid or
  643.    'any other control bound to that recordset to receive
  644.    'events while I move through the recordset.
  645.    Set DSClone = OraData1.Recordset.DbClone
  646.    'Move to the first record
  647.    DSClone.DbMoveFirst
  648.    'Get the field count
  649.    FieldCount% = DSClone.Fields.Count
  650.    ReDim flds(0 To FieldCount% - 1)
  651.    For i = 0 To (FieldCount% - 1)
  652.     Set flds(i) = DSClone.Fields(i)
  653.    Next i
  654.    If mDynasetHeadings.Checked = True Then
  655.     'Loop through all the field names in the row
  656.     For i% = 0 To (FieldCount% - 1)
  657.      FieldName$ = flds(i%).Name
  658.      'Quote column headings if it contains a space
  659.       If InStr(" ", FieldName$) Then
  660.        TextLine$ = TextLine$ + """" + FieldName$ + """"
  661.       Else
  662.        TextLine$ = TextLine$ + FieldName$
  663.       End If
  664.      If i% < (FieldCount% - 1) Then
  665.       TextLine$ = TextLine$ + Delimeter$
  666.      End If
  667.     Next i%
  668.     Print #FNum%, TextLine$ ' Write all fields headings
  669.    End If
  670.    'Loop to the end of the recordset
  671.    While DSClone.EOF <> True
  672.     TextLine$ = ""
  673.     'Loop through all the fields values in the row
  674.     For i% = 0 To (FieldCount% - 1)
  675.      'Unfortunately we don't yet know the Oracle column types.
  676.      'If we did, we could accurately quote strings and dates
  677.      'dates and leave numbers. Now, I'll just use IsNumber,
  678.      'IsDate and look for spaces. Yes, I could look into the
  679.      'table user_tab_columns. Go ahead...
  680.      FieldValue = flds(i%).value
  681.      If Not IsNull(FieldValue) Then 'Check for NULLs
  682.       If IsDate(FieldValue) Or Not IsNumeric(FieldValue) Or InStr(" ", DSClone.Fields(i%).value) Then
  683.        TextLine$ = TextLine$ + """" + FieldValue + """"
  684.       Else
  685.        TextLine$ = TextLine$ + FieldValue
  686.       End If
  687.      End If
  688.      'Add the delimeter except after the last column
  689.      If i% < (FieldCount% - 1) Then
  690.       'I was thinking about saving a file in column format
  691.       'NSpaces% = (OutTable.ColumnWidth(i%) - Len(FieldValue))
  692.       'If NSpaces% > 0 Then
  693.       ' Spaces$ = String(NSpaces%, " ")
  694.       'Else
  695.       ' Spaces$ = ""
  696.       'End If
  697.       'TextLine$ = TextLine$ + Spaces$
  698.       TextLine$ = TextLine$ + Delimeter$
  699.      End If
  700.     Next i%
  701.     'Print the row
  702.     Print #FNum%, TextLine$
  703.     'Advance to the next record
  704.     DSClone.DbMoveNext
  705.    Wend
  706.    'Close file
  707.    Close FNum%
  708.    'Restore table to track record movement
  709.    OutTable.Active = True
  710.    'Restore the cursor
  711.    Screen.MousePointer = DEFAULT
  712.  End If
  713. SaveToCancel:
  714.  Exit Sub
  715. SaveToError:
  716.  Screen.MousePointer = DEFAULT
  717.  Call RaiseError("Error", "An error occurred while writing " + Filename$)
  718.  Exit Sub
  719. End Sub
  720. 'Write a SQL script capable of being able to recreate a table and insert values from
  721. 'a select statement and a dynaset. This routine will only work for select statements
  722. 'with ONE object. I'll leave multiple objects up to someone else.
  723. Sub SaveToSQLScript (Filter As String, DefaultExt As String, Delimeter As String)
  724.  Dim SQLStatement$, Filename$, XObject$, CreateText$, DataType$, TextLine$, Temp$
  725.  Dim FNum%, fpos%, spos%, i%, FieldCount%
  726.  Dim FieldValue As Variant
  727.  Dim flds() As Object
  728.  'Init Variables
  729.  SQLStatement$ = txtSQL
  730.  i% = IsTerminated(SQLStatement$)
  731.  Dim DSClone As Object  'Original Dynaset Clone
  732.  Dim DSDesc As Object   'Dynaset describing a tables' columns
  733.  On Error GoTo SaveToSQLCancel
  734.  'Initialize the SaveAs file dialog
  735.  CMSaveAs.Filter = Filter$
  736.  CMSaveAs.DefaultExt = DefaultExt$
  737.  CMSaveAs.CancelError = True
  738.  CMSaveAs.Action = DLG_FILE_SAVE
  739.  Filename$ = CMSaveAs.Filename
  740.  If Filename$ <> "" And Err = 0 Then
  741.    'This might take a while
  742.    Screen.MousePointer = HOURGLASS
  743.    On Error GoTo FileError
  744.    'Find a free file
  745.    FNum% = FreeFile
  746.    Open Filename$ For Output As FNum%
  747. 'Build a CREATE TABLE statement from the columns descriptions in USER_TAB_COLUMNS
  748. 'Constraints are not checked. Try looking at USER_CONS_COLUMNS or USER_CONSTRAINTS
  749.    'Determine the object to describe
  750.    'Add ability to get SCOTT.EMP, but error on emp,dept for now
  751.    fpos% = InStr(1, SQLStatement$, " FROM ", 1)  'Look for the FROM
  752.    spos% = InStr(fpos% + 6, SQLStatement$, " ")  'Look for a space after the object
  753.    If spos = 0 Then
  754.     XObject$ = Mid$(SQLStatement$, fpos% + 6, (fpos% + 6))   'No space, object name at end
  755.    Else
  756.     XObject$ = Mid$(SQLStatement$, fpos% + 6, spos% - (fpos% + 6))    'space, object name in middle
  757.    End If
  758.    'Describe the columns so I can recreate the CREATE statement.
  759.    Set DSDesc = OraDatabase.DbCreateDynaset("Select * from user_tab_columns where table_name='" + UCase$(XObject$) + "'", 0&)
  760.    DSDesc.DbMoveFirst
  761.    'Initialize the CREATE statement
  762.    CreateText$ = "Create table " + XObject$ + "( "
  763.    'Loop through and create the create statement
  764.    For i% = 1 To DSDesc.RecordCount
  765.     'Add column name and data type
  766.     DataType$ = DSDesc.Fields("Data_Type").value
  767.     CreateText$ = CreateText$ + DSDesc.Fields("column_name").value + " "
  768.     CreateText$ = CreateText$ + DataType$
  769.     Select Case DataType$
  770.       'Precision and Scale must be added to numbers
  771.       Case "NUMBER"
  772.     If Not IsNull(DSDesc.Fields("data_precision").value) Then
  773.      CreateText$ = CreateText$ + "(" + DSDesc.Fields("data_precision").value
  774.      If DSDesc.Fields("data_scale").value > 0 Then
  775.       CreateText$ = CreateText$ + "," + DSDesc.Fields("data_scale").value
  776.      End If
  777.      CreateText$ = CreateText$ + ")"
  778.     End If
  779.       'Size must be added to varchar2, raw and char
  780.       Case "VARCHAR2", "RAW", "CHAR"
  781.     CreateText$ = CreateText$ + "(" + DSDesc.Fields("data_length").value + ")"
  782.     End Select
  783.     'Allow NULLS?
  784.     If DSDesc.Fields("nullable").value = "N" Then
  785.      CreateText$ = CreateText$ + " NOT NULL"
  786.     End If
  787.      'Add the delimeter except after the last column
  788.     If i% < DSDesc.RecordCount Then
  789.       CreateText$ = CreateText$ + ","
  790.     End If
  791.     DSDesc.DbMoveNext
  792.    Next i%
  793.    'Finish off the CREATE statement
  794.    CreateText$ = CreateText$ + " );"
  795.     'Write the CREATE Statement to the file
  796.     Print #FNum%, CreateText$
  797.  'Clone the RecordSet since that will prevent the grid or any
  798.  'other control bound to that recordset to receive events
  799.  'while I move through the recordset.
  800.  Set DSClone = OraData1.Recordset.DbClone
  801.    'Move to the first record
  802.    DSClone.DbMoveFirst
  803.    FieldCount% = DSClone.Fields.Count
  804.    ReDim flds(0 To FieldCount% - 1)
  805.    For i% = 0 To (FieldCount% - 1)
  806.     Set flds(i%) = DSClone.Fields(i%)
  807.    Next i%
  808.    'Loop to the end of the recordset
  809.    While DSClone.EOF <> True
  810.     TextLine$ = "Insert into " + XObject$ + " values ("
  811.     'Loop through all the fields values in the row
  812.     For i% = 1 To DSClone.Fields.Count
  813.      'Unfortunately we don't yet know the Oracle column types.
  814.      'If we did, we could accurately quote strings and dates
  815.      'dates and leave numbers. Now, I'll just use IsNumber,
  816.      'IsDate and look for spaces.
  817.      FieldValue = flds(i%).value
  818.      If IsNull(FieldValue) Then 'Check for NULLs
  819.        TextLine$ = TextLine$ + "NULL"
  820.      Else
  821.       If IsDate(FieldValue) Or Not IsNumeric(FieldValue) Or InStr(" ", flds(i%).value) Then
  822.        TextLine$ = TextLine$ + "'" + FieldValue + "'"
  823.       Else
  824.        TextLine$ = TextLine$ + FieldValue
  825.       End If
  826.      End If
  827.      'Add the delimeter except after the last column
  828.      If i% < FieldCount% Then
  829.       TextLine$ = TextLine$ + Delimeter$
  830.      End If
  831.     Next i%
  832.     TextLine$ = TextLine$ + ");"
  833.     'Print the row
  834.     Print #FNum%, TextLine$
  835.     'Advance to the next record
  836.     DSClone.DbMoveNext
  837.    Wend
  838.    'Close file
  839.    Close FNum%
  840.    'Restore the cursor
  841.  End If
  842. SaveToSQLCancel:
  843.  Screen.MousePointer = DEFAULT
  844.  Exit Sub
  845. FileError:
  846.   Screen.MousePointer = DEFAULT
  847.   If OraSession.LastServerErr <> 0 Then
  848.    frmOraError.Show MODAL
  849.   Else
  850.    Call RaiseError("Error", "An error occurred while writing " + Filename$)
  851.   End If
  852.   Exit Sub
  853. End Sub
  854. Sub sqltext_KeyPress (KeyAscii As Integer)
  855.  Dim foo$
  856.  If KeyAscii = KEY_RETURN Then
  857.   foo$ = txtSQL
  858.   If IsTerminated(foo$) Then
  859.    ExecuteSQLStatement (foo$)
  860.    KeyAscii = 0
  861.   End If
  862.  End If
  863. End Sub
  864.