home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / po7_win / object10 / vbsql.frm < prev    next >
Text File  |  1994-11-07  |  29KB  |  1,094 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. End
  238. Option Explicit
  239.  
  240. Sub cmdAdd_Click ()
  241.  
  242.  'Add a new record iff editable=true
  243.  If OutTable.Editable = True Then
  244.   OraData1.Recordset.DbAddNew
  245.  Else
  246.   Call RaiseError("Error", "The Dynaset is currently marked READONLY")
  247.  End If
  248.  
  249. End Sub
  250.  
  251. Sub cmdClear_Click ()
  252.  
  253.  txtSQL = ""
  254.  OraData1.RecordSource = "select * from dual where 1=0"
  255.  OraData1.Refresh
  256.  
  257.  mDynaset.Enabled = False
  258.  cmdDelete.Enabled = False
  259.  cmdAdd.Enabled = False
  260.  
  261. End Sub
  262.  
  263. Sub cmdDelete_Click ()
  264.  
  265.  'Delete's the current record iff editable=true
  266.  If OutTable.Editable = True Then
  267.   'Is there any data? Currently only RecordCount can tell
  268.   'you that but it will retrieve all of the records first.
  269.   If OraData1.Recordset.BOF = True And OraData1.Recordset.EOF = True Then
  270.    Call RaiseError("Error", "No row(s) to delete.")
  271.   Else
  272.    OraData1.Recordset.DbDelete
  273.   End If
  274.  Else
  275.   Call RaiseError("Error", "The Dynaset is currently marked READONLY")
  276.  End If
  277.  
  278. End Sub
  279.  
  280. Sub cmdExecute_click ()
  281.  
  282.  ExecuteSQLStatement (txtSQL)
  283.  txtSQL.SetFocus
  284.  
  285. End Sub
  286.  
  287. Sub cmdExit_Click ()
  288.  
  289.  'Simply call FILE->Exit
  290.  Call mFileExit_click
  291.  
  292. End Sub
  293.  
  294. 'Attempt to execute a SQL statement or VB*SQL Command.
  295. 'SELECT will return a dynaset
  296. 'DESC will describe an object(slightly different than SQL*Plus).
  297. Sub ExecuteSQLStatement (stext As String)
  298.  
  299. Dim SQLStatement$, DescSQL$, ObjectName$, Owner$, ObjectType$
  300. Dim IsTerm%, Verb%
  301. Dim DDesc As Object
  302. Dim en%
  303. Dim et$
  304.  
  305.  ObjectName$ = ""
  306.  Owner$ = UCase$(Trim$(UserName$))     'Default Owner
  307.  
  308.  'Strip spaces
  309.  SQLStatement$ = stext
  310.  Call ConvertCRLFtoSpace(SQLStatement$)
  311.  SQLStatement$ = Trim$(SQLStatement)
  312.  
  313.  If SQLStatement$ = "" Then
  314.   Call RaiseError("Error", "No SQL statement was specified.")
  315.  Else
  316.  'This might take a while
  317.  Screen.MousePointer = HOURGLASS
  318.  
  319.  On Error GoTo OraError
  320.  
  321.   'Strip semicolon or slash(side effect)
  322.   IsTerm% = IsTerminated(SQLStatement$)
  323.  
  324.   'Determine the SQL verb, object and owner
  325.   Verb% = SQLvoo(SQLStatement$, ObjectName$, Owner$)
  326.    
  327.  Select Case Verb%
  328.  
  329.    Case SQL_VERB_SELECT
  330.     'A SELECT will return a dynaset
  331.     OraData1.RecordSource = SQLStatement$
  332.     OraData1.Refresh
  333.     mDynaset.Enabled = True
  334.     cmdDelete.Enabled = True
  335.     cmdAdd.Enabled = True
  336.  
  337.    Case SQL_VERB_DESCRIBE
  338.  
  339.     DescSQL$ = "Select owner Owner, object_name ObjectName, object_type ObjectType from all_objects where object_name='" + ObjectName$ + "'"
  340.     
  341.     'Look for this object as owned by User$
  342.     Set DDesc = OraDatabase.DbCreateDynaset(DescSQL$ + " and owner='" + Owner$ + "'", 0&)
  343.     DDesc.DbMoveFirst
  344.     If DDesc.RecordCount = 0 Then
  345.      'Look for this object as owned by anyone
  346.      Set DDesc = OraDatabase.DbCreateDynaset(DescSQL$, 0&)
  347.      DDesc.DbMoveFirst
  348.     End If
  349.  
  350.     If DDesc.RecordCount = 0 Then
  351.       Call RaiseInfo("Information", "Object " + ObjectName$ + " does not exist.")
  352.       DescSQL$ = ""
  353.     Else
  354.      'Set the new owner and objecttype
  355.      Owner$ = DDesc.Fields("owner").value
  356.      ObjectType$ = DDesc.Fields("objecttype")
  357.  
  358.      If ObjectType$ = "TABLE" Or ObjectType$ = "VIEW" Then
  359.       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"
  360.      ElseIf ObjectType$ = "PACKAGE" Or ObjectType$ = "FUNCTION" Or ObjectType$ = "FUNCTION BODY" Or ObjectType$ = "PROCEDURE" Then
  361.       DescSQL$ = "Select text ""Source"" from user_source where type='" + ObjectType$ + "' and name='" + ObjectName$ + "' order by line"
  362.      ElseIf ObjectType$ = "SEQUENCE" Then
  363.       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$ + "'"
  364.      ElseIf ObjectType$ = "INDEX" Then
  365.       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$ + "'"
  366.      Else
  367.       Call RaiseInfo("Information", "Object " + ObjectName$ + " is a(n) " + ObjectType$)
  368.       DescSQL$ = ""
  369.      End If
  370.  
  371.     End If
  372.  
  373.     If DescSQL$ <> "" Then
  374.      'A DESC will return a dynaset
  375.      OraData1.RecordSource = DescSQL$
  376.      OraData1.Refresh
  377.     End If
  378.  
  379.     cmdDelete.Enabled = False
  380.     cmdAdd.Enabled = False
  381.  
  382.    Case Else
  383.     'Any SQL except SELECT will not return anything
  384.     OraDatabase.DbExecuteSQL (SQLStatement$)
  385.     mDynaset.Enabled = False
  386.     cmdDelete.Enabled = False
  387.     cmdAdd.Enabled = False
  388.    End Select
  389.  
  390.    'Reset the mouse pointer
  391.    Screen.MousePointer = DEFAULT
  392.   End If
  393.  
  394.  Exit Sub
  395. OraError:
  396.  Screen.MousePointer = DEFAULT
  397.  frmOraError.Show MODAL
  398.  Exit Sub
  399.  
  400. End Sub
  401.  
  402. Sub Form_Load ()
  403.  
  404.  'Initialize Grid settings
  405.  OutTable.SelectMode = 1
  406.  OutTable.Headings = True
  407.  OutTable.Editable = False
  408.  OutTable.MarqueeStyle = 3
  409.  'Values
  410.  '0 - Dotted Cell Border (Default)
  411.  '1 - Solid Cell Border
  412.  '2 - Highlight Cell
  413.  '3 - Highlight Row
  414.  '4 - Highlight Row & Cell
  415.  '5 - None
  416.  
  417.  'Initialize Menu settings
  418.  mDynasetHeadings.Checked = True
  419.  mDynasetReadonly.Checked = True
  420.  mDynaset.Enabled = False
  421.  mSessionCommit.Enabled = False
  422.  mSessionRollback.Enabled = False
  423.  
  424.  'Initialize buttons
  425.  cmdAdd.Enabled = False
  426.  cmdDelete.Enabled = False
  427.  
  428.  Call CenterForm(frmVBSQL)
  429.  
  430.  'For display purposes
  431.  If DatabaseName$ = "" Then
  432.   txtConnection = UserName$ + "@<local host>"
  433.  Else
  434.   txtConnection = UserName$ + "@" + DatabaseName$
  435.  End If
  436.  
  437.  
  438.  OraData1.DatabaseName = DatabaseName$
  439.  OraData1.Connect = Connect$
  440.  
  441. End Sub
  442.  
  443. 'Check for a 'terminator' of sorts. In SQL*Plus a statement
  444. 'is terminated(and executed) after a semicolon or forward
  445. 'slash(and a return). This function also has the effect of
  446. 'stripping spaces iff the statement was terminated by a
  447. 'semicolon or forward slash
  448. Function IsTerminated (SQLStatement As String) As Integer
  449.  
  450.  Dim Temp$
  451.  
  452.  'Remove any trailing spaces
  453.  Temp$ = RTrim$(SQLStatement$)
  454.  
  455.  'Check for semicolon or forward slash
  456.  If Right$(Temp$, 1) = ";" Or Right$(Temp$, 1) = "/" Then
  457.   'Strip the semicolon or forward slash and spaces
  458.   SQLStatement$ = Trim$(Left$(Temp$, Len(Temp$) - 1))
  459.   IsTerminated = True
  460.  Else
  461.   IsTerminated = False
  462.  End If
  463.  
  464. End Function
  465.  
  466. Sub mDSetCommaDel_Click ()
  467.  
  468.  Call SaveToFile("Comma Delimited(*.TXT)|*.TXT|All Files(*.*)|*.*", "TXT", ",")
  469.  
  470. End Sub
  471.  
  472. Sub mDSetTabDel_Click ()
  473.  
  474.  Call SaveToFile("Tab Delimited(*.TXT)|*.TXT|All Files(*.*)|*.*", "TXT", Chr(9))
  475.  
  476. End Sub
  477.  
  478. Sub mDynasetFont_Click ()
  479.  
  480.  'Only get the ANSI and Screen Fonts
  481.  CMFont.Flags = CF_ANSIONLY Or CF_SCREENFONTS
  482.  CMFont.Action = DLG_FONT
  483.  
  484.  'If the user didn't hit cancel and there is a font
  485.  If Err = 0 And CMFont.FontName <> "" Then
  486.   OutTable.FontName = CMFont.FontName
  487.  End If
  488.  
  489. End Sub
  490.  
  491. Sub mDynasetGraph_Click ()
  492.  
  493.  Set GraphDyn = OraData1.Recordset '.DbClone
  494.  frmGraphO.Show MODAL
  495.  Unload frmGraphO
  496. End Sub
  497.  
  498. Sub mDynasetHeadings_Click ()
  499.  
  500.  If mDynasetHeadings.Checked = True Then
  501.   OutTable.Headings = False
  502.   mDynasetHeadings.Checked = False
  503.  Else
  504.   OutTable.Headings = True
  505.   mDynasetHeadings.Checked = True
  506.  End If
  507.  
  508. End Sub
  509.  
  510. Sub mDynasetReadOnly_Click ()
  511.  
  512.  'Mark the grid readonly/readwrite
  513.  If OutTable.Editable = True Then
  514.   OutTable.Editable = False
  515.   mDynasetReadonly.Checked = True
  516.  Else
  517.   OutTable.Editable = True
  518.   mDynasetReadonly.Checked = False
  519.  End If
  520.  
  521. End Sub
  522.  
  523. Sub mDynasetSQLScript_Click ()
  524.  
  525.  Call SaveToSQLScript("SQL Script(*.SQL)|*.SQL|All Files(*.*)|*.*", "SQL", ",")
  526.  
  527. End Sub
  528.  
  529. Sub mFileExit_click ()
  530.  
  531.  'Commit and exit
  532.  If mSessionBeginTrans.Checked = True Then
  533.   OraSession.DbCommitTrans
  534.  End If
  535.  
  536.  Unload frmVBSQL
  537.  End
  538.  
  539. End Sub
  540.  
  541. Sub mFilePrint_Click ()
  542.  
  543.  'Print the current form
  544.  CMFilePrint.Flags = 0
  545.  CMFilePrint.Action = DLG_PRINT
  546.  frmVBSQL.PrintForm
  547.  
  548. End Sub
  549.  
  550. Sub mFilePrintSetup_Click ()
  551.  
  552.  'Display the print setup dialog
  553.  CMFilePrint.Flags = PD_PRINTSETUP
  554.  CMFilePrint.Action = DLG_PRINT
  555.  
  556. End Sub
  557.  
  558. Sub mHelpAbout_Click ()
  559.  frmAbout.Show MODAL
  560. End Sub
  561.  
  562. Sub mHelpContents_Click ()
  563.  
  564.  Call RaiseInfo("Warning", "Help not yet implemented.")
  565.  'Send an F1 to the app which will cause the help file
  566.  'listed in the project options to be opened.
  567.  'SendKeys "{F1}"
  568.  
  569. End Sub
  570.  
  571. Sub mSessionBeginTrans_Click ()
  572.  
  573.  'Begin a transaction and set menus
  574.  mSessionBeginTrans.Checked = True
  575.  mSessionBeginTrans.Enabled = False
  576.  mSessionCommit.Enabled = True
  577.  mSessionRollback.Enabled = True
  578.  OraSession.DbBeginTrans
  579.  
  580. End Sub
  581.  
  582. Sub mSessionCommit_Click ()
  583.  
  584.  'Commit a transaction and set menus
  585.  mSessionBeginTrans.Checked = False
  586.  mSessionBeginTrans.Enabled = True
  587.  mSessionCommit.Enabled = False
  588.  mSessionRollback.Enabled = False
  589.  OraSession.DbCommitTrans
  590.  
  591. End Sub
  592.  
  593. Sub mSessionRollback_Click ()
  594.  
  595.  'Roolback a transaction and set menus
  596.  mSessionBeginTrans.Checked = False
  597.  mSessionBeginTrans.Enabled = True
  598.  mSessionCommit.Enabled = False
  599.  mSessionRollback.Enabled = False
  600.  OraSession.DbRollback
  601.  
  602. End Sub
  603.  
  604. Sub mSQLOpen_Click ()
  605.  
  606.  Dim TextLine$, Filename$
  607.  Dim FNum%
  608.  
  609.  'Init Variables
  610.  TextLine$ = ""
  611.  
  612.  On Error GoTo SQLOpenCancel
  613.  
  614.  'Initialize the Open file dialog
  615.  CMOpen.Filter = "SQL Scripts(*.SQL)|*.SQL|All Files(*.*)|*.*"
  616.  CMOpen.CancelError = True
  617.  CMOpen.Action = DLG_FILE_OPEN
  618.  
  619.  Filename$ = CMOpen.Filename
  620.   
  621.   If Filename$ <> "" And Dir$(Filename$) <> "" Then
  622.     FNum% = FreeFile
  623.     Open Filename$ For Input As FNum%
  624.   
  625.     txtSQL = ""
  626.    
  627.     While Not EOF(FNum%)
  628.      Line Input #FNum%, TextLine$  ' Get complete line.
  629.      txtSQL = txtSQL + TextLine$ + Chr$(13) + Chr$(10)
  630.     Wend
  631.     Close FNum% 'Close file.
  632.   End If
  633.  
  634. SQLOpenCancel:
  635.  Exit Sub
  636.  
  637. End Sub
  638.  
  639. Sub mSQLRun_Click ()
  640.  
  641.  Dim TextLine$, Filename$
  642.  
  643.  'Init Variables
  644.  TextLine$ = ""
  645.  
  646.  On Error GoTo SQLRunCancel
  647.  
  648.  'Initialize the Open file dialog
  649.  CMRun.Filter = "SQL Scripts(*.SQL)|*.SQL|All Files(*.*)|*.*"
  650.  CMRun.DialogTitle = "Run"
  651.  CMRun.CancelError = True
  652.  CMRun.Action = DLG_FILE_OPEN
  653.  
  654.  Filename$ = CMRun.Filename
  655.  Call RunSQLScript(Filename$)
  656.  
  657. SQLRunCancel:
  658. Exit Sub
  659.  
  660. End Sub
  661.  
  662. Sub mSQLSaveAs_Click ()
  663.  
  664.  Dim TextLine$, Filename$
  665.  Dim FNum%
  666.  
  667.  'Init Variables
  668.  TextLine$ = ""
  669.  
  670.  On Error GoTo SQLSaveAsCancel:
  671.  
  672.  'Initialize the SaveAs file dialog
  673.  CMSaveAs.Filter = "SQL Scripts(*.SQL)|*.SQL|All Files(*.*)|*.*"
  674.  CMSaveAs.DefaultExt = "SQL"
  675.  CMSaveAs.CancelError = True
  676.  CMSaveAs.Action = DLG_FILE_SAVE
  677.  Filename$ = CMSaveAs.Filename
  678.  
  679.  'Write the sql to a file
  680.  If Filename$ <> "" Then
  681.    FNum% = FreeFile
  682.    Open Filename$ For Output As FNum%
  683.    'TextLine$ = txtSQL
  684.    Print #FNum%, txtSQL ' Write complete line.
  685.    Close FNum% 'Close file.
  686.  End If
  687.  
  688. SQLSaveAsCancel:
  689.  Exit Sub
  690.  
  691. End Sub
  692.  
  693. Sub OraData1_Error (DataErr As Integer, Response As Integer)
  694.  
  695.  frmOraError.Show MODAL
  696.  Response = DATA_ERRCONTINUE
  697.  
  698. End Sub
  699.  
  700. Sub OutTable_DblClick ()
  701.  
  702.  If OutTable.Editable = False Then
  703.   Call RaiseError("Error", "The Dynaset is currently marked READONLY")
  704.  End If
  705.  
  706. End Sub
  707.  
  708. Sub OutTable_KeyPress (KeyAscii As Integer)
  709.  
  710.  If KeyAscii = KEY_ESCAPE Then
  711.   OutTable.DataChanged = False
  712.   OutTable.Modified = False ' Nullify user's editing
  713.   OutTable.EditActive = False   ' Exit edit mode
  714.  End If
  715.  
  716. End Sub
  717.  
  718. Sub RunSQLScript (Filename As String)
  719.  
  720.  Dim SQLStatement$, CurrentLine$
  721.  Dim FNum%
  722.  
  723.  SQLStatement$ = ""
  724.  CurrentLine$ = ""
  725.  
  726.  On Error GoTo RunSQLError
  727.  
  728.  If Filename$ <> "" And Dir$(Filename$) <> "" Then
  729.   FNum% = FreeFile
  730.   Open Filename$ For Input As FNum%
  731.   
  732.   'txtSQL = ""
  733.    
  734.   While Not EOF(FNum%)
  735.     Line Input #FNum%, CurrentLine$
  736.     SQLStatement$ = SQLStatement$ + Trim(CurrentLine$)
  737.  
  738.     If Len(SQLStatement$) < 1 Then
  739.      'do nothing
  740.     ElseIf Left$(SQLStatement$, 2) = "--" Or UCase$(Left$(SQLStatement$, 3)) = "REM" Then
  741.      Call RaiseInfo("Info", "Found Remark=" + SQLStatement$)
  742.      SQLStatement$ = ""
  743.     ElseIf Right$(SQLStatement$, 1) = ";" Or Right$(SQLStatement$, 1) = "/" Then
  744.      'Need to strip the ; or /
  745.      SQLStatement$ = Left$(SQLStatement$, Len(SQLStatement$) - 1)
  746.      Call RaiseInfo("Info", "Execute SQL=" + SQLStatement$)
  747.      txtSQL = SQLStatement$ 'I need to reference txtSQL here. I'd rather not.
  748.      ExecuteSQLStatement (SQLStatement$)
  749.      SQLStatement$ = ""
  750.     Else
  751.      SQLStatement$ = SQLStatement$ + " "
  752.     End If
  753.  
  754.   Wend
  755.   
  756.   Close FNum% 'Close file.
  757.  
  758.  End If
  759.  
  760.  Exit Sub
  761.  
  762. RunSQLError:
  763.  Call RaiseError("Error", "Error Reading " + Filename$)
  764.  Exit Sub
  765.  
  766. End Sub
  767.  
  768. 'Save a Dynaset to a file given a particular file extension and data delimeter
  769. Sub SaveToFile (Filter As String, DefaultExt As String, Delimeter As String)
  770.  
  771.  Dim TextLine$, Filename$, FieldName$, Spaces$
  772.  Dim FNum%, FieldCount%, i%, NSpaces%
  773.  Dim FieldValue As Variant
  774.  Dim flds() As Object
  775.  
  776.  'Init/Declare Variables
  777.  TextLine$ = ""
  778.  Dim DSClone As Object
  779.  
  780.  On Error GoTo SaveToCancel
  781.  'Initialize the SaveAs file dialog
  782.  CMSaveAs.Filter = Filter$
  783.  CMSaveAs.DefaultExt = DefaultExt$
  784.  CMSaveAs.CancelError = True
  785.  
  786.  CMSaveAs.Action = DLG_FILE_SAVE
  787.  
  788.  Filename$ = CMSaveAs.Filename
  789.  
  790.  'On Error GoTo SaveToError
  791.  
  792.  If Filename$ <> "" Then
  793.    'This might take a while
  794.    Screen.MousePointer = HOURGLASS
  795.  
  796.    'Find a free file
  797.    FNum% = FreeFile
  798.  
  799.    Open Filename$ For Output As FNum%
  800.  
  801.    'Clone the RecordSet since that will prevent the grid or
  802.    'any other control bound to that recordset to receive
  803.    'events while I move through the recordset.
  804.    Set DSClone = OraData1.Recordset.DbClone
  805.  
  806.    'Move to the first record
  807.    DSClone.DbMoveFirst
  808.  
  809.    'Get the field count
  810.    FieldCount% = DSClone.Fields.Count
  811.  
  812.    ReDim flds(0 To FieldCount% - 1)
  813.    For i = 0 To (FieldCount% - 1)
  814.     Set flds(i) = DSClone.Fields(i)
  815.    Next i
  816.    
  817.    If mDynasetHeadings.Checked = True Then
  818.     'Loop through all the field names in the row
  819.     For i% = 0 To (FieldCount% - 1)
  820.  
  821.      FieldName$ = flds(i%).Name
  822.      'Quote column headings if it contains a space
  823.       If InStr(" ", FieldName$) Then
  824.        TextLine$ = TextLine$ + """" + FieldName$ + """"
  825.       Else
  826.        TextLine$ = TextLine$ + FieldName$
  827.       End If
  828.  
  829.      If i% < (FieldCount% - 1) Then
  830.       TextLine$ = TextLine$ + Delimeter$
  831.      End If
  832.     Next i%
  833.     Print #FNum%, TextLine$ ' Write all fields headings
  834.    End If
  835.  
  836.    'Loop to the end of the recordset
  837.    While DSClone.EOF <> True
  838.     TextLine$ = ""
  839.  
  840.     'Loop through all the fields values in the row
  841.     For i% = 0 To (FieldCount% - 1)
  842.  
  843.      'Unfortunately we don't yet know the Oracle column types.
  844.      'If we did, we could accurately quote strings and dates
  845.      'dates and leave numbers. Now, I'll just use IsNumber,
  846.      'IsDate and look for spaces. Yes, I could look into the
  847.      'table user_tab_columns. Go ahead...
  848.      FieldValue = flds(i%).value
  849.      If Not IsNull(FieldValue) Then 'Check for NULLs
  850.  
  851.       If IsDate(FieldValue) Or Not IsNumeric(FieldValue) Or InStr(" ", DSClone.Fields(i%).value) Then
  852.        TextLine$ = TextLine$ + """" + FieldValue + """"
  853.       Else
  854.        TextLine$ = TextLine$ + FieldValue
  855.       End If
  856.  
  857.      End If
  858.  
  859.      'Add the delimeter except after the last column
  860.      If i% < (FieldCount% - 1) Then
  861.  
  862.       'I was thinking about saving a file in column format
  863.       'NSpaces% = (OutTable.ColumnWidth(i%) - Len(FieldValue))
  864.       'If NSpaces% > 0 Then
  865.       ' Spaces$ = String(NSpaces%, " ")
  866.       'Else
  867.       ' Spaces$ = ""
  868.       'End If
  869.       'TextLine$ = TextLine$ + Spaces$
  870.       TextLine$ = TextLine$ + Delimeter$
  871.      End If
  872.  
  873.     Next i%
  874.  
  875.     'Print the row
  876.     Print #FNum%, TextLine$
  877.  
  878.     'Advance to the next record
  879.     DSClone.DbMoveNext
  880.    Wend
  881.  
  882.    'Close file
  883.    Close FNum%
  884.  
  885.    'Restore table to track record movement
  886.    OutTable.Active = True
  887.  
  888.    'Restore the cursor
  889.    Screen.MousePointer = DEFAULT
  890.  End If
  891.  
  892. SaveToCancel:
  893.  Exit Sub
  894.  
  895. SaveToError:
  896.  Screen.MousePointer = DEFAULT
  897.  Call RaiseError("Error", "An error occurred while writing " + Filename$)
  898.  Exit Sub
  899.  
  900. End Sub
  901.  
  902. 'Write a SQL script capable of being able to recreate a table and insert values from
  903. 'a select statement and a dynaset. This routine will only work for select statements
  904. 'with ONE object. I'll leave multiple objects up to someone else.
  905. Sub SaveToSQLScript (Filter As String, DefaultExt As String, Delimeter As String)
  906.  
  907.  Dim SQLStatement$, Filename$, XObject$, CreateText$, DataType$, TextLine$, Temp$
  908.  Dim FNum%, fpos%, spos%, i%, FieldCount%
  909.  Dim FieldValue As Variant
  910.  Dim flds() As Object
  911.  
  912.  'Init Variables
  913.  SQLStatement$ = txtSQL
  914.  i% = IsTerminated(SQLStatement$)
  915.  
  916.  Dim DSClone As Object  'Original Dynaset Clone
  917.  Dim DSDesc As Object   'Dynaset describing a tables' columns
  918.  
  919.  On Error GoTo SaveToSQLCancel
  920.  
  921.  'Initialize the SaveAs file dialog
  922.  CMSaveAs.Filter = Filter$
  923.  CMSaveAs.DefaultExt = DefaultExt$
  924.  CMSaveAs.CancelError = True
  925.  CMSaveAs.Action = DLG_FILE_SAVE
  926.  
  927.  Filename$ = CMSaveAs.Filename
  928.  
  929.  If Filename$ <> "" And Err = 0 Then
  930.    'This might take a while
  931.    Screen.MousePointer = HOURGLASS
  932.  
  933.    On Error GoTo FileError
  934.  
  935.    'Find a free file
  936.    FNum% = FreeFile
  937.  
  938.    Open Filename$ For Output As FNum%
  939.  
  940. 'Build a CREATE TABLE statement from the columns descriptions in USER_TAB_COLUMNS
  941. 'Constraints are not checked. Try looking at USER_CONS_COLUMNS or USER_CONSTRAINTS
  942.  
  943.    'Determine the object to describe
  944.    'Add ability to get SCOTT.EMP, but error on emp,dept for now
  945.    fpos% = InStr(1, SQLStatement$, " FROM ", 1)  'Look for the FROM
  946.    spos% = InStr(fpos% + 6, SQLStatement$, " ")  'Look for a space after the object
  947.    If spos = 0 Then
  948.     XObject$ = Mid$(SQLStatement$, fpos% + 6, (fpos% + 6))   'No space, object name at end
  949.    Else
  950.     XObject$ = Mid$(SQLStatement$, fpos% + 6, spos% - (fpos% + 6))    'space, object name in middle
  951.    End If
  952.  
  953.    'Describe the columns so I can recreate the CREATE statement.
  954.    Set DSDesc = OraDatabase.DbCreateDynaset("Select * from user_tab_columns where table_name='" + UCase$(XObject$) + "'", 0&)
  955.    DSDesc.DbMoveFirst
  956.  
  957.    'Initialize the CREATE statement
  958.    CreateText$ = "Create table " + XObject$ + "( "
  959.  
  960.    'Loop through and create the create statement
  961.    For i% = 1 To DSDesc.RecordCount
  962.  
  963.     'Add column name and data type
  964.     DataType$ = DSDesc.Fields("Data_Type").value
  965.     CreateText$ = CreateText$ + DSDesc.Fields("column_name").value + " "
  966.     CreateText$ = CreateText$ + DataType$
  967.  
  968.     Select Case DataType$
  969.       'Precision and Scale must be added to numbers
  970.       Case "NUMBER"
  971.     If Not IsNull(DSDesc.Fields("data_precision").value) Then
  972.      CreateText$ = CreateText$ + "(" + DSDesc.Fields("data_precision").value
  973.      If DSDesc.Fields("data_scale").value > 0 Then
  974.       CreateText$ = CreateText$ + "," + DSDesc.Fields("data_scale").value
  975.      End If
  976.      CreateText$ = CreateText$ + ")"
  977.     End If
  978.  
  979.       'Size must be added to varchar2, raw and char
  980.       Case "VARCHAR2", "RAW", "CHAR"
  981.     CreateText$ = CreateText$ + "(" + DSDesc.Fields("data_length").value + ")"
  982.  
  983.     End Select
  984.  
  985.     'Allow NULLS?
  986.     If DSDesc.Fields("nullable").value = "N" Then
  987.      CreateText$ = CreateText$ + " NOT NULL"
  988.     End If
  989.  
  990.      'Add the delimeter except after the last column
  991.     If i% < DSDesc.RecordCount Then
  992.       CreateText$ = CreateText$ + ","
  993.     End If
  994.  
  995.     DSDesc.DbMoveNext
  996.  
  997.    Next i%
  998.  
  999.    'Finish off the CREATE statement
  1000.    CreateText$ = CreateText$ + " );"
  1001.  
  1002.     'Write the CREATE Statement to the file
  1003.     Print #FNum%, CreateText$
  1004.  
  1005.  'Clone the RecordSet since that will prevent the grid or any
  1006.  'other control bound to that recordset to receive events
  1007.  'while I move through the recordset.
  1008.  Set DSClone = OraData1.Recordset.DbClone
  1009.  
  1010.    'Move to the first record
  1011.    DSClone.DbMoveFirst
  1012.  
  1013.    FieldCount% = DSClone.Fields.Count
  1014.  
  1015.    ReDim flds(0 To FieldCount% - 1)
  1016.    For i% = 0 To (FieldCount% - 1)
  1017.     Set flds(i%) = DSClone.Fields(i%)
  1018.    Next i%
  1019.  
  1020.  
  1021.    'Loop to the end of the recordset
  1022.    While DSClone.EOF <> True
  1023.     TextLine$ = "Insert into " + XObject$ + " values ("
  1024.  
  1025.     'Loop through all the fields values in the row
  1026.     For i% = 1 To DSClone.Fields.Count
  1027.  
  1028.      'Unfortunately we don't yet know the Oracle column types.
  1029.      'If we did, we could accurately quote strings and dates
  1030.      'dates and leave numbers. Now, I'll just use IsNumber,
  1031.      'IsDate and look for spaces.
  1032.      FieldValue = flds(i%).value
  1033.      If IsNull(FieldValue) Then 'Check for NULLs
  1034.        TextLine$ = TextLine$ + "NULL"
  1035.      Else
  1036.       If IsDate(FieldValue) Or Not IsNumeric(FieldValue) Or InStr(" ", flds(i%).value) Then
  1037.        TextLine$ = TextLine$ + "'" + FieldValue + "'"
  1038.       Else
  1039.        TextLine$ = TextLine$ + FieldValue
  1040.       End If
  1041.  
  1042.      End If
  1043.  
  1044.      'Add the delimeter except after the last column
  1045.      If i% < FieldCount% Then
  1046.       TextLine$ = TextLine$ + Delimeter$
  1047.      End If
  1048.  
  1049.     Next i%
  1050.  
  1051.     TextLine$ = TextLine$ + ");"
  1052.     'Print the row
  1053.     Print #FNum%, TextLine$
  1054.  
  1055.     'Advance to the next record
  1056.     DSClone.DbMoveNext
  1057.    Wend
  1058.  
  1059.    'Close file
  1060.    Close FNum%
  1061.  
  1062.    'Restore the cursor
  1063.  End If
  1064.  
  1065. SaveToSQLCancel:
  1066.  Screen.MousePointer = DEFAULT
  1067.  Exit Sub
  1068.  
  1069. FileError:
  1070.   Screen.MousePointer = DEFAULT
  1071.   If OraSession.LastServerErr <> 0 Then
  1072.    frmOraError.Show MODAL
  1073.   Else
  1074.    Call RaiseError("Error", "An error occurred while writing " + Filename$)
  1075.   End If
  1076.   Exit Sub
  1077.  
  1078. End Sub
  1079.  
  1080. Sub sqltext_KeyPress (KeyAscii As Integer)
  1081.  
  1082.  Dim foo$
  1083.  
  1084.  If KeyAscii = KEY_RETURN Then
  1085.   foo$ = txtSQL
  1086.   If IsTerminated(foo$) Then
  1087.    ExecuteSQLStatement (foo$)
  1088.    KeyAscii = 0
  1089.   End If
  1090.  End If
  1091.  
  1092. End Sub
  1093.  
  1094.