home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / mquery / mquery.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-05-02  |  26.5 KB  |  900 lines

  1. VERSION 2.00
  2. Begin Form fQuery 
  3.    AutoRedraw      =   -1  'True
  4.    BackColor       =   &H00C0C0C0&
  5.    BorderStyle     =   1  'Fixed Single
  6.    Caption         =   "Query Builder"
  7.    ClientHeight    =   5730
  8.    ClientLeft      =   1425
  9.    ClientTop       =   2145
  10.    ClientWidth     =   9195
  11.    ClipControls    =   0   'False
  12.    ControlBox      =   0   'False
  13.    Height          =   6135
  14.    Icon            =   MQUERY.FRX:0000
  15.    KeyPreview      =   -1  'True
  16.    Left            =   1365
  17.    LinkTopic       =   "Form1"
  18.    MaxButton       =   0   'False
  19.    MinButton       =   0   'False
  20.    ScaleHeight     =   5709.895
  21.    ScaleMode       =   0  'User
  22.    ScaleWidth      =   9625.69
  23.    Top             =   1800
  24.    Width           =   9315
  25.    Begin SSPanel PnlHelp 
  26.       Alignment       =   1  'Left Justify - MIDDLE
  27.       AutoSize        =   1  'AutoSize Panel Width To Caption
  28.       BackColor       =   &H0000FFFF&
  29.       BevelOuter      =   0  'None
  30.       BorderWidth     =   1
  31.       Font3D          =   0  'None
  32.       ForeColor       =   &H00FF0000&
  33.       Height          =   315
  34.       Left            =   3705
  35.       TabIndex        =   32
  36.       Top             =   1320
  37.       Visible         =   0   'False
  38.       Width           =   1965
  39.    End
  40.    Begin ListBox cColOrder 
  41.       BackColor       =   &H00C0C0C0&
  42.       Height          =   420
  43.       Left            =   6000
  44.       TabIndex        =   31
  45.       Top             =   1590
  46.       Width           =   3075
  47.    End
  48.    Begin CommandButton RunSaveQryButton 
  49.       Caption         =   "&Load Query"
  50.       Height          =   375
  51.       Left            =   4440
  52.       TabIndex        =   0
  53.       Top             =   4815
  54.       Width           =   1290
  55.    End
  56.    Begin CommandButton ExecSqlButton 
  57.       Caption         =   "&ExecSQL"
  58.       Height          =   375
  59.       Left            =   5835
  60.       TabIndex        =   1
  61.       Top             =   4815
  62.       Width           =   960
  63.    End
  64.    Begin SSPanel Panel3D1 
  65.       Align           =   2  'Align Bottom
  66.       Alignment       =   1  'Left Justify - MIDDLE
  67.       BackColor       =   &H0000FFFF&
  68.       BevelInner      =   1  'Inset
  69.       Font3D          =   0  'None
  70.       FontBold        =   -1  'True
  71.       FontItalic      =   0   'False
  72.       FontName        =   "Arial"
  73.       FontSize        =   9.75
  74.       FontStrikethru  =   0   'False
  75.       FontUnderline   =   0   'False
  76.       ForeColor       =   &H00FF0000&
  77.       Height          =   435
  78.       Left            =   0
  79.       TabIndex        =   29
  80.       Top             =   5295
  81.       Width           =   9195
  82.    End
  83.    Begin Frame Frame1 
  84.       BackColor       =   &H00C0C0C0&
  85.       Caption         =   "Display"
  86.       Height          =   495
  87.       Left            =   120
  88.       TabIndex        =   28
  89.       Top             =   4740
  90.       Width           =   3075
  91.       Begin OptionButton Option1 
  92.          BackColor       =   &H00C0C0C0&
  93.          Caption         =   "Grid"
  94.          Height          =   195
  95.          Index           =   0
  96.          Left            =   2100
  97.          TabIndex        =   19
  98.          Top             =   180
  99.          Value           =   -1  'True
  100.          Width           =   915
  101.       End
  102.       Begin OptionButton Option1 
  103.          BackColor       =   &H00C0C0C0&
  104.          Caption         =   "Record"
  105.          Height          =   195
  106.          Index           =   1
  107.          Left            =   840
  108.          TabIndex        =   18
  109.          Top             =   180
  110.          Width           =   975
  111.       End
  112.    End
  113.    Begin PictureBox ExpressionBox 
  114.       BackColor       =   &H00C0C0C0&
  115.       Height          =   1095
  116.       Left            =   120
  117.       ScaleHeight     =   1065
  118.       ScaleWidth      =   9705
  119.       TabIndex        =   24
  120.       Tag             =   "OL"
  121.       Top             =   240
  122.       Width           =   9735
  123.       Begin CommandButton GetValuesButton 
  124.          Caption         =   "List Possible &Values"
  125.          Height          =   252
  126.          Left            =   5955
  127.          TabIndex        =   17
  128.          Top             =   720
  129.          Width           =   2292
  130.       End
  131.       Begin ComboBox cValue 
  132.          BackColor       =   &H00C0C0C0&
  133.          Height          =   300
  134.          Left            =   5580
  135.          Sorted          =   -1  'True
  136.          TabIndex        =   14
  137.          Text            =   "cValue"
  138.          Top             =   375
  139.          Width           =   3330
  140.       End
  141.       Begin ComboBox cOperator 
  142.          BackColor       =   &H00C0C0C0&
  143.          Height          =   300
  144.          Left            =   4320
  145.          Style           =   2  'Dropdown List
  146.          TabIndex        =   13
  147.          Top             =   360
  148.          Width           =   1095
  149.       End
  150.       Begin ComboBox cField 
  151.          BackColor       =   &H00C0C0C0&
  152.          Height          =   300
  153.          Left            =   120
  154.          Style           =   2  'Dropdown List
  155.          TabIndex        =   12
  156.          Top             =   360
  157.          Width           =   4095
  158.       End
  159.       Begin CommandButton ORButton 
  160.          Caption         =   "&Or into Criteria"
  161.          Height          =   252
  162.          Left            =   2040
  163.          TabIndex        =   16
  164.          Top             =   720
  165.          Width           =   1812
  166.       End
  167.       Begin CommandButton ANDButton 
  168.          Caption         =   "&And into Criteria"
  169.          Height          =   252
  170.          Left            =   120
  171.          TabIndex        =   15
  172.          Tag             =   "Are you paying attention!!!!"
  173.          Top             =   720
  174.          Width           =   1812
  175.       End
  176.       Begin Label OperatorLabel 
  177.          BackColor       =   &H00C0C0C0&
  178.          Caption         =   "Operator:"
  179.          Height          =   195
  180.          Left            =   4320
  181.          TabIndex        =   27
  182.          Top             =   120
  183.          Width           =   975
  184.       End
  185.       Begin Label ValueLabel 
  186.          BackColor       =   &H00C0C0C0&
  187.          Caption         =   "Value:"
  188.          Height          =   195
  189.          Left            =   5520
  190.          TabIndex        =   26
  191.          Top             =   120
  192.          Width           =   1455
  193.       End
  194.       Begin Label FieldNameLabel 
  195.          BackColor       =   &H00C0C0C0&
  196.          Caption         =   "Field Name:"
  197.          Height          =   192
  198.          Left            =   120
  199.          TabIndex        =   25
  200.          Top             =   120
  201.          Width           =   1332
  202.       End
  203.    End
  204.    Begin CommandButton JoinButton 
  205.       Caption         =   "Set Table &Joins"
  206.       Height          =   255
  207.       Left            =   6240
  208.       TabIndex        =   10
  209.       Top             =   2670
  210.       Width           =   2535
  211.    End
  212.    Begin ListBox cJoinFields 
  213.       BackColor       =   &H00C0C0C0&
  214.       Height          =   420
  215.       Left            =   6000
  216.       TabIndex        =   11
  217.       Tag             =   "OL"
  218.       Top             =   2970
  219.       Width           =   3135
  220.    End
  221.    Begin ComboBox cOrderByField 
  222.       BackColor       =   &H00C0C0C0&
  223.       Height          =   300
  224.       Left            =   6000
  225.       Style           =   2  'Dropdown List
  226.       TabIndex        =   9
  227.       Tag             =   "OL"
  228.       Top             =   2310
  229.       Width           =   3135
  230.    End
  231.    Begin ListBox cTableList 
  232.       BackColor       =   &H00C0C0C0&
  233.       Height          =   1590
  234.       Left            =   120
  235.       MultiSelect     =   1  'Simple
  236.       TabIndex        =   6
  237.       Tag             =   "OL"
  238.       Top             =   1680
  239.       Width           =   2175
  240.    End
  241.    Begin ListBox cShowFields 
  242.       BackColor       =   &H00C0C0C0&
  243.       Height          =   1590
  244.       Left            =   2415
  245.       MultiSelect     =   1  'Simple
  246.       TabIndex        =   7
  247.       Tag             =   "OL"
  248.       Top             =   1650
  249.       Width           =   3510
  250.    End
  251.    Begin CommandButton CloseButton 
  252.       Cancel          =   -1  'True
  253.       Caption         =   "&Exit"
  254.       Height          =   375
  255.       Left            =   8070
  256.       TabIndex        =   3
  257.       Top             =   4815
  258.       Width           =   915
  259.    End
  260.    Begin CommandButton RunQueryButton 
  261.       Caption         =   "&Run Query"
  262.       Height          =   375
  263.       Left            =   3300
  264.       TabIndex        =   4
  265.       Top             =   4815
  266.       Width           =   1035
  267.    End
  268.    Begin CommandButton ClearButton 
  269.       Caption         =   "R&eset"
  270.       Height          =   375
  271.       Left            =   6900
  272.       TabIndex        =   2
  273.       Top             =   4815
  274.       Width           =   1095
  275.    End
  276.    Begin TextBox cCriteria 
  277.       BackColor       =   &H00C0C0C0&
  278.       Height          =   1215
  279.       Left            =   45
  280.       MultiLine       =   -1  'True
  281.       ScrollBars      =   2  'Vertical
  282.       TabIndex        =   5
  283.       Tag             =   "OL"
  284.       Top             =   3480
  285.       Width           =   9105
  286.    End
  287.    Begin Label lFilter 
  288.       AutoSize        =   -1  'True
  289.       BackColor       =   &H00C0C0C0&
  290.       Caption         =   "Select Filter"
  291.       Height          =   195
  292.       Left            =   120
  293.       TabIndex        =   30
  294.       Top             =   0
  295.       Width           =   1035
  296.    End
  297.    Begin Label OrberByFieldLabel 
  298.       BackColor       =   &H00C0C0C0&
  299.       Caption         =   "Order By Field:"
  300.       Height          =   195
  301.       Left            =   6000
  302.       TabIndex        =   8
  303.       Top             =   2070
  304.       Width           =   2055
  305.    End
  306.    Begin Label ColOrderLabel 
  307.       BackColor       =   &H00C0C0C0&
  308.       Caption         =   "Column Order:"
  309.       Height          =   195
  310.       Left            =   6000
  311.       TabIndex        =   23
  312.       Top             =   1380
  313.       Width           =   2055
  314.    End
  315.    Begin Label TableListLabel 
  316.       BackColor       =   &H00C0C0C0&
  317.       Caption         =   "Select Tables:"
  318.       Height          =   195
  319.       Left            =   120
  320.       TabIndex        =   22
  321.       Top             =   1440
  322.       Width           =   1455
  323.    End
  324.    Begin Label ShowFieldsLabel 
  325.       BackColor       =   &H00C0C0C0&
  326.       Caption         =   "Select Fields to Show:"
  327.       Height          =   195
  328.       Left            =   2400
  329.       TabIndex        =   21
  330.       Top             =   1440
  331.       Width           =   2055
  332.    End
  333.    Begin Label CriteriaLabel 
  334.       BackColor       =   &H00C0C0C0&
  335.       Caption         =   "Criteria:"
  336.       Height          =   180
  337.       Left            =   120
  338.       TabIndex        =   20
  339.       Top             =   3270
  340.       Width           =   1335
  341.    End
  342. Sub ANDButton_Click ()
  343.   ShowHelp ANDButton, 0, 0
  344.   Dim f As field
  345.   Dim ns As Integer
  346.   Dim nsflds As String
  347.   Dim nt As Integer
  348.   Dim ntflds As String
  349.   If cField = "" Then Exit Sub
  350.     If UCase(Left(cField, 4)) = "DBO." Then
  351.     nsflds = Mid(cField, 5, Len(cField))
  352.     Else
  353.     nsflds = cField
  354.     End If
  355.   Set f = gCurrentDB.TableDefs(stSTF((nsflds), 0)).Fields(stSTF((nsflds), 1))
  356.  If cCriteria <> "" Then
  357.     cCriteria = cCriteria + Chr(13) + Chr(10) + "And "
  358.   End If
  359.   If f.Type = FT_STRING Or f.Type = FT_MEMO Then
  360.      ns = InStr(1, cField, ".")
  361.      nsflds = Mid(cField, ns + 1, Len(cField))
  362.      ntflds = Left(cField, ns - 1)
  363.         nt = InStr(1, ntflds, " ")
  364.         If nt > 0 Then
  365.         ntflds = "[" + ntflds + "]"
  366.         nsflds = ntflds + "." + "[" + nsflds + "]"
  367.         Else
  368.         nsflds = "[" + nsflds + "]"
  369.         nsflds = Left(cField, ns) + nsflds
  370.         End If
  371.       nt = InStr(1, cValue, "'")
  372.       If nt > 0 Then
  373.       ntflds = Chr(34) + cValue + Chr(34)
  374.      cCriteria = cCriteria + "((" + nsflds + " " + cOperator + " " + ntflds + "))"
  375.      Else
  376.     cCriteria = cCriteria + nsflds + " " + cOperator + " '" + cValue + "'"
  377.      End If
  378.   Else
  379.      'If f.Type = FT_DATETIME Then
  380.          ns = InStr(1, cField, ".")
  381.          nsflds = Mid(cField, ns + 1, Len(cField))
  382.          ntflds = Left(cField, ns - 1)
  383.          nt = InStr(1, ntflds, " ")
  384.         If nt > 0 Then
  385.         ntflds = "[" + ntflds + "]"
  386.         nsflds = ntflds + "." + "[" + nsflds + "]"
  387.         Else
  388.         nsflds = "[" + nsflds + "]"
  389.         nsflds = Left(cField, ns) + nsflds
  390.         End If
  391.         If f.Type = FT_DATETIME Then
  392.         cValue = "#" + cValue + "#"
  393.         End If
  394.     'End If
  395.     cCriteria = cCriteria + nsflds + " " + cOperator + " " + cValue
  396.   End If
  397.   cField.SetFocus
  398. End Sub
  399. Sub ANDButton_MouseMove (Button As Integer, Shift As Integer, x As Single, y As Single)
  400.     ShowHelp ANDButton, x, y
  401. End Sub
  402. Sub cCriteria_GotFocus ()
  403. ExecSqlButton.Enabled = True
  404. RunSaveQryButton.Enabled = False
  405. End Sub
  406. Sub cField_Click ()
  407.   cValue.Clear
  408. End Sub
  409. Sub ClearButton_Click ()
  410. resetdefault
  411. End Sub
  412. Sub CloseButton_Click ()
  413. End Sub
  414. Sub cShowFields_Click ()
  415. If cShowfields.ListCount = 0 Then
  416. CriteriaLabel.Caption = "SQL Statement"
  417. Exit Sub
  418. End If
  419. ' which item was clicked on
  420. ' is it already in the colorder box if so remove it
  421. For j% = 0 To cColOrder.ListCount - 1
  422. If cColOrder.List(j%) = cShowfields.List(cShowfields.ListIndex) Then
  423. cColOrder.RemoveItem (j%)
  424. removed% = True
  425. Exit For
  426. End If
  427. Next j%
  428. If Not removed% Then ' must be an add not a remove
  429. addit$ = cShowfields.List(cShowfields.ListIndex)
  430. cColOrder.AddItem addit$
  431. removed% = False
  432. End If
  433. CriteriaLabel.Caption = "Criteria:"
  434. For h% = 0 To cShowfields.ListCount - 1
  435. If cShowfields.Selected(h%) Then
  436.     RunQueryButton.Enabled = True
  437.     ExecSqlButton.Enabled = False
  438.    If RunSaveQryButton.Caption = "&Load Query" Then
  439.     RunSaveQryButton.Enabled = False
  440.    End If
  441. Exit Sub
  442. End If
  443. RunQueryButton.Enabled = False
  444. ExecSqlButton.Enabled = True
  445. End Sub
  446. Sub cTableList_Click ()
  447.     On Error GoTo errtrap
  448.     If deselect > 0 Then
  449.     deselect = 0
  450.     Exit Sub
  451.     End If
  452.   Dim I As Integer, ii As Integer
  453.   Dim t As TableDef
  454.   Dim q As QueryDef
  455.   Dim st As String
  456.   Dim trap As Integer
  457.   cCriteria.Text = ""
  458.   cField.Clear
  459.   cShowfields.Clear
  460.   cColOrder.Clear
  461.   cOrderByField.Clear
  462.   cValue.Clear
  463.   If RunQueryButton.Enabled = True Then
  464.       RunSaveQryButton.Enabled = True
  465.       RunQueryButton.Enabled = False
  466.       ExecSqlButton.Enabled = True
  467.       CriteriaLabel.Caption = "SQL Statement"
  468.   End If
  469.   gStoredFlag = False
  470.   cOrderByField.AddItem "(none)"
  471.   For ii = 0 To cTableList.ListCount - 1
  472.     If cTableList.Selected(ii) Then
  473.     'RunQueryButton.Enabled = True
  474.       Set t = gCurrentDB.TableDefs(cTableList.List(ii))
  475.       For I = 0 To t.Fields.Count - 1
  476.     st = cTableList.List(ii) + "." + t.Fields(I).Name
  477.         If UCase(Left(st, 4)) = "DBO." Then
  478.         st = Mid(st, 5, Len(st))
  479.         End If
  480.     cField.AddItem st
  481.     cShowfields.AddItem st
  482.     'cColOrder.AddItem st
  483.     cOrderByField.AddItem st
  484.       Next
  485.     End If
  486.   Next
  487.   If cField.List(0) <> "" Then
  488.     cField.ListIndex = 0
  489.     'cColOrder.ListIndex = 0
  490.     cOrderByField.ListIndex = 0
  491.   End If
  492. exitit:
  493. Exit Sub
  494. errtrap:
  495.   trap = MsgBox("Cannot use this file", 0, "Query")
  496.   Resume exitit
  497. End Sub
  498. Sub ExecSqlButton_Click ()
  499. ExecSql
  500. If Not gfFROMSQL And Not gStoredFlag Then
  501.  MsgBox "No SQL Statement to Execute!", 48
  502.  NoCritflag = False
  503. End If
  504. End Sub
  505. Sub Form_Load ()
  506.    fQuery.Left = (screen.Width - fQuery.Width) / 2
  507.    fQuery.Top = (screen.Height - fQuery.Height) / 2
  508.    On Local Error GoTo FLErr
  509.    Dim ds As DynaSet
  510.    Dim I As Integer
  511.    Dim t As TableDef
  512.    Dim q As QueryDef
  513.    'Clear listbox
  514.    cCriteria = ""
  515.    'Fill the Operator combo
  516.    cOperator.AddItem "="
  517.    cOperator.AddItem "<>"
  518.    cOperator.AddItem ">"
  519.    cOperator.AddItem ">="
  520.    cOperator.AddItem "<"
  521.    cOperator.AddItem "<="
  522.    cOperator.AddItem "Like"
  523.    cOperator.ListIndex = 0
  524.    cTableList.ListIndex = 0
  525.    CriteriaLabel.Caption = "SQL Statement"
  526.    RunSaveQryButton.Caption = "&Load Query"
  527.    RunQueryButton.Enabled = False
  528.    cValue = ""
  529.   GoTo FLEnd
  530. FLErr:
  531.   ShowError
  532.   Resume FLEnd
  533. FLEnd:
  534. Me.Show
  535. End Sub
  536. Sub Form_Paint ()
  537.   Outlines Me
  538.   PicOutlines ExpressionBox, cField
  539.   PicOutlines ExpressionBox, cOperator
  540.   PicOutlines ExpressionBox, cValue
  541. End Sub
  542. Sub Form_Resize ()
  543.   On Error Resume Next
  544.   If WindowState <> 1 Then
  545.     Height = 6050
  546.     'Width = 7224
  547.     Width = 9315'250
  548.   End If
  549. End Sub
  550. Sub GetValuesButton_Click ()
  551.   Dim ds As DynaSet
  552.   Dim dsString As String
  553.   Dim ns As Integer
  554.   Dim fldn As String
  555.   Dim nv As String
  556.   cValue.Clear
  557.     ' search for sql .dbo and strip
  558.     If UCase(Left(cField, 4)) = "DBO." Then
  559.     fldn = Mid(cField, 5, Len(cField))
  560.     ns = InStr(1, fldn, ".")
  561.     nv = Left(fldn, ns - 1)
  562.     fldn = "[" + Mid(fldn, ns + 1, Len(fldn)) + "]"
  563.     fldn = nv + "." + fldn
  564.     dsString = "select Distinct " + fldn + " from "
  565.     Else
  566.     ns = InStr(1, cField, ".")
  567.     fldn = Mid(cField, ns + 1, Len(cField))
  568.     nv = Left(cField, ns - 1)
  569.     nt% = InStr(1, nv, " ")
  570.     If nt% > 0 Then
  571.      fldn = "[" + nv + "]" + "." + "[" + fldn + "]"
  572.     Else
  573.     fldn = Left(cField, ns) + "[" + fldn + "]"
  574.     End If
  575.     dsString = "select Distinct " + fldn + " from "
  576.     End If
  577.   On Error GoTo GVErr
  578.   MsgBar "Getting Possible Values", True
  579.   SetHourGlass Me
  580.   Set ds = gCurrentDB.CreateDynaset(dsString + stSTF((fldn), 0))
  581.   Do While ds.EOF = False
  582.     If Trim(ds(0)) <> "" Then
  583.       cValue.AddItem ds(0).Value
  584.     End If
  585.     ds.MoveNext
  586.   Loop
  587.   ds.Close
  588.   cValue = cValue.List(0)
  589.   cValue.SetFocus
  590.   GoTo GVEnd
  591. GVErr:
  592.   cValue = ""
  593.   Resume GVEnd
  594. GVEnd:
  595.   ResetMouse Me
  596.   MsgBar "", False
  597. End Sub
  598. Sub JoinButton_Click ()
  599.   Dim I As Integer
  600.   Dim c As Integer
  601.   For I = 0 To cTableList.ListCount - 1
  602.     If cTableList.Selected(I) = True Then
  603.       c = c + 1
  604.     End If
  605.   Next
  606.   If c < 2 Then
  607.     Beep
  608.     MsgBox "You Must Have at Least 2 Tables Selected!", 48
  609.   Else
  610.     MsgBar "Choose Joins", False
  611.     fJoin.Show MODAL
  612.     MsgBar "", False
  613.   End If
  614. End Sub
  615. Sub ORButton_Click ()
  616.   Dim f As field
  617.   Dim ns As Integer
  618.   Dim nsflds As String
  619.   Dim nt As Integer
  620.   Dim ntflds As String
  621.   If cField = "" Then Exit Sub
  622.     If UCase(Left(cField, 4)) = "DBO." Then
  623.     nsflds = Mid(cField, 5, Len(cField))
  624.     Else
  625.     nsflds = cField
  626.     End If
  627.   Set f = gCurrentDB.TableDefs(stSTF((cField), 0)).Fields(stSTF((cField), 1))
  628.   If cCriteria <> "" Then
  629.     cCriteria = cCriteria + Chr(13) + Chr(10) + " Or "
  630.   End If
  631.   If f.Type = FT_STRING Or f.Type = FT_MEMO Then
  632.      ns = InStr(1, cField, ".")
  633.      nsflds = Mid(cField, ns + 1, Len(cField))
  634.      ntflds = Left(cField, ns - 1)
  635.         nt = InStr(1, ntflds, " ")
  636.         If nt > 0 Then
  637.         ntflds = "[" + ntflds + "]"
  638.         nsflds = ntflds + "." + "[" + nsflds + "]"
  639.         Else
  640.         nsflds = "[" + nsflds + "]"
  641.         nsflds = Left(cField, ns) + nsflds
  642.         End If
  643.     cCriteria = cCriteria + nsflds + " " + cOperator + " '" + cValue + "'"
  644.   Else
  645.      If f.Type = FT_DATETIME Then
  646.          ns = InStr(1, cField, ".")
  647.          nsflds = Mid(cField, ns + 1, Len(cField))
  648.          ntflds = Left(cField, ns - 1)
  649.          nt = InStr(1, ntflds, " ")
  650.         If nt > 0 Then
  651.         ntflds = "[" + ntflds + "]"
  652.         nsflds = ntflds + "." + "[" + nsflds + "]"
  653.         Else
  654.         nsflds = "[" + nsflds + "]"
  655.         nsflds = Left(cField, ns) + nsflds
  656.         End If
  657.         cValue = "#" + cValue + "#"
  658.     End If
  659.     cCriteria = cCriteria + nsflds + " " + cOperator + " " + cValue
  660.   End If
  661.   cField.SetFocus
  662. End Sub
  663. Sub RunQueryButton_Click ()
  664.   On Error GoTo OKErr
  665.      Dim ds As DynaSet
  666.      Dim fs As String
  667.      Dim ts As String
  668.      Dim I As Integer
  669.      Dim ns As Integer
  670.      Dim nsflds As String
  671.      Dim nt As Integer
  672.      Dim ntflds As String
  673.      Dim listnu As Integer
  674.      Dim joins As String
  675.      joins = ""
  676.      listnu = 0
  677.     MsgBar "Building Query", True
  678.        For I% = 0 To cTableList.ListCount - 1
  679.     If cTableList.Selected(I%) Then
  680.     listnu = listnu + 1
  681.     End If
  682.       Next I
  683.         If listnu > 1 And cJoinFields.ListCount = 0 Then
  684.         MsgBox "You Must Have a Join for more than 1 Table Selected!", 48
  685.         Exit Sub
  686.         End If
  687.      'check for join condition
  688.      If cJoinFields.ListCount > 0 Then
  689.        For I = 0 To cJoinFields.ListCount - 1
  690.      joins = joins + cJoinFields.List(I) + ","
  691.        Next
  692.        'get rid of last ,
  693.        joins = " " + Left(joins, Len(joins) - 1)
  694.        End If
  695.      If cCriteria <> "" Then
  696.       StWhere$ = "AND " + LTrim(cCriteria)
  697.        'strip CRLFs
  698.        For I = 1 To Len(StWhere$)
  699.      If Mid(StWhere$, I, 1) = Chr$(13) Then
  700.        stTmp$ = stTmp$ + " "
  701.      ElseIf Mid(StWhere$, I, 1) = Chr$(10) Then
  702.        'do nothing
  703.      Else
  704.        stTmp$ = stTmp$ + Mid(StWhere$, I, 1)
  705.      End If
  706.        Next
  707.        StWhere$ = stTmp$
  708.        StWhere$ = RTrim(StWhere$)
  709.      
  710.        'Add parens to stWhere$
  711.     stTmpWhere$ = StWhere$
  712.       stTmp$ = stGetToken(stTmpWhere$, " ")
  713.       If fMatchParen% = False And UCase(stTmp$) = "AND" Then
  714.         stNewWhere$ = stNewWhere$ + stTmp$ + " ("
  715.         fMatchParen% = True
  716.       ElseIf fMatchParen% = True And UCase(stTmp$) = "AND" Then
  717.         stNewWhere$ = stNewWhere$ + ") " + stTmp$ + " ("
  718.         'fMatchParen% = False
  719.       Else
  720.         If UCase(stTmp$) = "OR" Or UCase(stTmp$) = "IN" Or UCase(stTmp$) = "LIKE" Then
  721.           stNewWhere$ = stNewWhere$ + " " + stTmp$ + " "
  722.         Else
  723.           stNewWhere$ = stNewWhere$ + stTmp$
  724.         End If
  725.       End If
  726.     Loop Until stTmpWhere$ = ""
  727.     StWhere$ = stNewWhere$ + ")"
  728.        'Build DynaSet string:
  729.        'Peel off leading AND/OR
  730.        If Mid(StWhere$, 2, 2) = "OR" Then
  731.      StWhere$ = Mid(StWhere$, 5, Len(StWhere$) - 5)
  732.        Else
  733.      stTmp$ = stGetToken(StWhere$, " ")
  734.        End If
  735.        If StWhere$ <> "" Then
  736.      StWhere$ = " Where " + StWhere$
  737.      Else
  738.      StWhere$ = " Where " + cCriteria
  739.        End If
  740.      End If
  741.      ' get rid of brackets
  742.      ' check for more brackets until nomore
  743.      Do
  744.      ns = InStr(1, StWhere$, "'[")
  745.      If ns <> 0 Then
  746.      nsflds = Mid(StWhere$, 1, ns)
  747.      nv$ = Mid$(StWhere$, ns + 2, Len(StWhere$))
  748.      nsflds = nsflds + nv$
  749.      ns = InStr(1, nsflds, "]'")
  750.      nv$ = Mid$(nsflds, ns + 1, Len(nsflds))
  751.      nv$ = Mid(nsflds, 1, ns - 1) + nv$
  752.      StWhere$ = nv$
  753.      End If
  754.      Loop Until ns = 0
  755.      'check for join condition
  756.      If joins <> "" Then
  757.      StWhere$ = "," + joins + " " + StWhere$
  758.      End If
  759.      'check
  760.      
  761.      'check for order by field
  762.      If cOrderByField <> "(none)" Then
  763.     '  check for dbo. in field
  764.     If UCase(Left(cOrderByField, 4)) = "DBO." Then
  765.         nsflds = Mid(cOrderByField, 5, Len(cOrderByField))
  766.         ns = InStr(1, nsflds, ".")
  767.         nsflds = "[" + Mid(nsflds, ns + 1, Len(nsflds)) + "]"
  768.         nv$ = Mid(cOrderByField, 5, ns) + nsflds
  769.         StWhere$ = StWhere$ + " Order By " + nv$
  770.     Else
  771.         ns = InStr(1, cOrderByField, ".")
  772.         nsflds = "[" + Mid(cOrderByField, ns + 1, Len(cOrderByField)) + "]"
  773.         nv$ = Left(cOrderByField, ns) + nsflds
  774.         StWhere$ = StWhere$ + " Order By " + nv$
  775.     End If
  776.      End If
  777.      'get show field names and strip out sql servers dbo. field preface
  778.      For I% = 0 To cColOrder.ListCount - 1
  779.        If UCase(Left(cColOrder.List(I%), 4)) = "DBO." Then
  780.              nsflds = Mid(cColOrder.List(I%), 5, Len(cColOrder.List(I%)))
  781.              ns = InStr(1, nsflds, ".")
  782.              nsflds = Mid(nsflds, ns + 1, Len(nsflds))
  783.              nsflds = "[" + nsflds + "]"
  784.              nsflds = Left(Mid(cColOrder.List(I%), 5, Len(cColOrder.List(I%))), ns) + nsflds
  785.              fs = fs + nsflds + ","
  786.         Else
  787.         
  788.              ns = InStr(1, cColOrder.List(I%), ".")
  789.              nsflds = Mid(cColOrder.List(I%), ns + 1, Len(cColOrder.List(I%)))
  790.              ntflds = Left(cColOrder.List(I%), ns - 1)
  791.              nt = InStr(1, ntflds, " ")
  792.              If nt > 0 Then
  793.             ntflds = "[" + ntflds + "]"
  794.             nsflds = ntflds + "." + "[" + nsflds + "]"
  795.              Else
  796.              nsflds = "[" + nsflds + "]"
  797.              nsflds = Left(cColOrder.List(I%), ns) + nsflds
  798.              End If
  799.              
  800.              fs = fs + nsflds + ","
  801.        End If
  802.        Next
  803.      If fs = "" Then
  804.        For I% = 0 To cTableList.ListCount - 1
  805.     If cTableList.Selected(I%) Then
  806.         If UCase(Left(cTableList.Selected(I%), 4)) = ".DBO" Then
  807.         fs = fs + Mid(cTableList.Selected(I%), 5, Len(cTableList.Selected(I%)))
  808.          Else
  809.         fs = fs + cTableList.List(I%) + ".*,"
  810.         End If
  811.      End If
  812.        Next
  813.        If fs = "" Then
  814.      fs = "*"
  815.        Else
  816.      fs = Mid(fs, 1, Len(fs) - 1)     'take off the last ","
  817.        End If
  818.      Else
  819.        fs = Mid(fs, 1, Len(fs) - 1)
  820.      End If
  821.      'get table names
  822.      For I% = 0 To cTableList.ListCount - 1
  823.        If cTableList.Selected(I%) Then
  824.         If UCase(Left(cTableList.List(I%), 4)) = "DBO." Then
  825.         ts = ts + Mid(cTableList.List(I%), 5, Len(cTableList.List(I%))) + ","
  826.          Else
  827.         ts = ts + cTableList.List(I%) + ","
  828.         End If
  829.        End If
  830.      Next
  831.      ts = Mid(ts, 1, Len(ts) - 1)
  832.      nt = InStr(1, ts, " ")
  833.      If nt > 0 Then
  834.      ts = "[" + ts + "]"
  835.      End If
  836.      gstDynaString = "Select " + fs + " From " + ts + StWhere$
  837.      gfFROMSQL = False ' not a SQL statement
  838. If Option1(0) = False Then
  839.      Dim dsform1 As New fDynaset
  840.      dsform1.Show
  841.        Else
  842.      Dim dsform2 As New fGridFrm
  843.      dsform2.Show
  844.        End If
  845.      GoTo OKEnd
  846. OKErr:
  847.   If Err = 364 Then Resume OKEnd   'catch unloaded form
  848.   ShowError
  849.   Resume OKEnd
  850. OKEnd:
  851.   MsgBar "", False
  852. End Sub
  853. Sub RunSaveQryButton_Click ()
  854. fStoreQry.Show 1
  855. If gstDynaString <> "" Then
  856. cCriteria.Text = gstDynaString
  857. MsgBar "Stored Query is Loaded", False
  858. Me.Tag = gstDynaString
  859. gStoredFlag = True
  860. End If
  861. End Sub
  862. Function stGetToken (stLn$, stDelim$) As String
  863.     On Error GoTo GetTokenError
  864.     iOpenQuote% = InStr(1, stLn$, """")
  865.     iDelim% = InStr(1, stLn$, stDelim$)
  866.     iBracket% = InStr(1, stLn$, "[")
  867.     If (iOpenQuote% > 0) And (iOpenQuote% < iDelim%) Then
  868.      iCloseQuote% = InStr(iOpenQuote% + 1, stLn$, """")
  869.      iDelim% = InStr(iCloseQuote% + 1, stLn$, stDelim$)
  870.     End If
  871.     If (iDelim% <> 0) And (iDelim% < iBracket%) Then
  872.      stToken$ = LTrim$(RTrim$(Mid$(stLn$, 1, iDelim% - 1)))
  873.      stLn$ = Mid$(stLn$, iDelim% + 1)
  874.     Else
  875.      stToken$ = LTrim$(RTrim$(Mid$(stLn$, 1)))
  876.      stLn$ = ""
  877.     End If
  878.     If (Len(stToken$) > 0) Then
  879.      If (Mid$(stToken$, 1, 1) = """") Then
  880.           stToken$ = Mid$(stToken$, 2)
  881.      End If
  882.      If (Mid$(stToken$, Len(stToken$), 1) = """") Then
  883.           stToken$ = Mid$(stToken$, 1, Len(stToken$) - 1)
  884.      End If
  885.     End If
  886.     stGetToken = stToken$
  887. GetTokenExit:
  888.     Exit Function
  889. GetTokenError:
  890.     Resume GetTokenExit
  891. End Function
  892. 'function to split the table and the field from a tbl.fld pair
  893. Function stSTF (tf As String, part As Integer) As String
  894.   If part = 0 Then
  895.     stSTF = Mid(tf, 1, InStr(1, tf, ".") - 1)
  896.   Else
  897.     stSTF = Mid(tf, InStr(1, tf, ".") + 1, Len(tf))
  898.   End If
  899. End Function
  900.