home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l406 / 4.ddi / QUERY.FR_ / QUERY.bin (.txt)
Encoding:
Visual Basic Form  |  1992-10-21  |  17.2 KB  |  581 lines

  1. VERSION 2.00
  2. Begin Form fQuery 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Query Builder"
  5.    Height          =   5535
  6.    Icon            =   QUERY.FRX:0000
  7.    KeyPreview      =   -1  'True
  8.    Left            =   1170
  9.    LinkTopic       =   "Form1"
  10.    MaxButton       =   0   'False
  11.    MDIChild        =   -1  'True
  12.    ScaleHeight     =   5112
  13.    ScaleMode       =   0  'User
  14.    ScaleWidth      =   7116
  15.    Top             =   810
  16.    Width           =   7215
  17.    Begin CommandButton JoinButton 
  18.       Caption         =   "Set Table &Joins"
  19.       Height          =   256
  20.       Left            =   4427
  21.       TabIndex        =   27
  22.       Top             =   2529
  23.       Width           =   2528
  24.    End
  25.    Begin ListBox cJoinFields 
  26.       Height          =   421
  27.       Left            =   4427
  28.       TabIndex        =   26
  29.       Top             =   2770
  30.       Width           =   2528
  31.    End
  32.    Begin CommandButton CopySQLButton 
  33.       Caption         =   "&Copy SQL"
  34.       Height          =   376
  35.       Left            =   2991
  36.       TabIndex        =   25
  37.       Top             =   4696
  38.       Width           =   1092
  39.    End
  40.    Begin ComboBox cOrderByField 
  41.       BackColor       =   &H00FFFFFF&
  42.       Height          =   301
  43.       Left            =   4427
  44.       Style           =   2  'Dropdown List
  45.       TabIndex        =   23
  46.       Top             =   2168
  47.       Width           =   2528
  48.    End
  49.    Begin ComboBox cGroupByField 
  50.       BackColor       =   &H00FFFFFF&
  51.       Height          =   301
  52.       Left            =   4427
  53.       Style           =   2  'Dropdown List
  54.       TabIndex        =   21
  55.       Top             =   1565
  56.       Width           =   2528
  57.    End
  58.    Begin Frame ExpressionBox 
  59.       BackColor       =   &H00C0C0C0&
  60.       Caption         =   "(Expressions)"
  61.       Height          =   1305
  62.       Left            =   120
  63.       TabIndex        =   10
  64.       Top             =   0
  65.       Width           =   6835
  66.       Begin PictureBox Filler 
  67.          BackColor       =   &H00C0C0C0&
  68.          BorderStyle     =   0  'None
  69.          Height          =   288
  70.          Left            =   6370
  71.          ScaleHeight     =   288
  72.          ScaleMode       =   0  'User
  73.          ScaleWidth      =   108
  74.          TabIndex        =   20
  75.          Top             =   600
  76.          Width           =   108
  77.       End
  78.       Begin CommandButton ANDButton 
  79.          Caption         =   "&And into Criteria"
  80.          Height          =   252
  81.          Left            =   120
  82.          TabIndex        =   19
  83.          Top             =   960
  84.          Width           =   1812
  85.       End
  86.       Begin CommandButton ORButton 
  87.          Caption         =   "&Or into Criteria"
  88.          Height          =   252
  89.          Left            =   2040
  90.          TabIndex        =   18
  91.          Top             =   960
  92.          Width           =   1812
  93.       End
  94.       Begin ComboBox cField 
  95.          BackColor       =   &H00FFFFFF&
  96.          Height          =   288
  97.          Left            =   120
  98.          Style           =   2  'Dropdown List
  99.          TabIndex        =   14
  100.          Top             =   600
  101.          Width           =   2652
  102.       End
  103.       Begin ComboBox cOperator 
  104.          Height          =   288
  105.          Left            =   2880
  106.          Style           =   2  'Dropdown List
  107.          TabIndex        =   13
  108.          Top             =   600
  109.          Width           =   1092
  110.       End
  111.       Begin ComboBox cValue 
  112.          Height          =   288
  113.          Left            =   4080
  114.          Sorted          =   -1  'True
  115.          TabIndex        =   12
  116.          Text            =   "cValue"
  117.          Top             =   600
  118.          Width           =   2652
  119.       End
  120.       Begin CommandButton GetValuesButton 
  121.          Caption         =   "List Possible &Values"
  122.          Height          =   252
  123.          Left            =   4200
  124.          TabIndex        =   11
  125.          Top             =   960
  126.          Width           =   2292
  127.       End
  128.       Begin Label FieldNameLabel 
  129.          BackColor       =   &H00C0C0C0&
  130.          Caption         =   "Field Name:"
  131.          Height          =   252
  132.          Left            =   120
  133.          TabIndex        =   17
  134.          Top             =   360
  135.          Width           =   1332
  136.       End
  137.       Begin Label ValueLabel 
  138.          BackColor       =   &H00C0C0C0&
  139.          Caption         =   "Value:"
  140.          Height          =   252
  141.          Left            =   4080
  142.          TabIndex        =   16
  143.          Top             =   360
  144.          Width           =   1452
  145.       End
  146.       Begin Label OperatorLabel 
  147.          BackColor       =   &H00C0C0C0&
  148.          Caption         =   "Operator:"
  149.          Height          =   252
  150.          Left            =   2880
  151.          TabIndex        =   15
  152.          Top             =   360
  153.          Width           =   972
  154.       End
  155.    End
  156.    Begin ListBox cTableList 
  157.       Height          =   1596
  158.       Left            =   120
  159.       MultiSelect     =   1  'Simple
  160.       TabIndex        =   9
  161.       Top             =   1565
  162.       Width           =   1570
  163.    End
  164.    Begin CommandButton ShowSQLButton 
  165.       Caption         =   "&Show SQL"
  166.       Height          =   376
  167.       Left            =   1675
  168.       TabIndex        =   8
  169.       Top             =   4696
  170.       Width           =   1092
  171.    End
  172.    Begin ListBox cShowFields 
  173.       Height          =   1596
  174.       Left            =   1795
  175.       MultiSelect     =   1  'Simple
  176.       TabIndex        =   5
  177.       Top             =   1565
  178.       Width           =   2528
  179.    End
  180.    Begin CommandButton CloseButton 
  181.       Caption         =   "Close"
  182.       Height          =   376
  183.       Left            =   5623
  184.       TabIndex        =   2
  185.       Top             =   4696
  186.       Width           =   1092
  187.    End
  188.    Begin CommandButton RunQueryButton 
  189.       Caption         =   "&Run Query"
  190.       Height          =   376
  191.       Left            =   359
  192.       TabIndex        =   1
  193.       Top             =   4696
  194.       Width           =   1092
  195.    End
  196.    Begin CommandButton ClearButton 
  197.       Caption         =   "C&lear All"
  198.       Height          =   376
  199.       Left            =   4307
  200.       TabIndex        =   0
  201.       Top             =   4696
  202.       Width           =   1092
  203.    End
  204.    Begin TextBox cCriteria 
  205.       Height          =   1219
  206.       Left            =   120
  207.       MultiLine       =   -1  'True
  208.       ScrollBars      =   2  'Vertical
  209.       TabIndex        =   3
  210.       Top             =   3372
  211.       Width           =   6835
  212.    End
  213.    Begin Label OrberByFieldLabel 
  214.       BackColor       =   &H00C0C0C0&
  215.       Caption         =   "Order By Field:"
  216.       Height          =   256
  217.       Left            =   4427
  218.       TabIndex        =   24
  219.       Top             =   1927
  220.       Width           =   2049
  221.    End
  222.    Begin Label GroupByFieldLabel 
  223.       BackColor       =   &H00C0C0C0&
  224.       Caption         =   "Group By Field:"
  225.       Height          =   256
  226.       Left            =   4427
  227.       TabIndex        =   22
  228.       Top             =   1355
  229.       Width           =   2049
  230.    End
  231.    Begin Label TableListLabel 
  232.       BackColor       =   &H00C0C0C0&
  233.       Caption         =   "Select Tables:"
  234.       Height          =   256
  235.       Left            =   120
  236.       TabIndex        =   7
  237.       Top             =   1355
  238.       Width           =   1451
  239.    End
  240.    Begin Label ShowFieldsLabel 
  241.       BackColor       =   &H00C0C0C0&
  242.       Caption         =   "Select Fields to Show:"
  243.       Height          =   256
  244.       Left            =   1795
  245.       TabIndex        =   6
  246.       Top             =   1355
  247.       Width           =   2049
  248.    End
  249.    Begin Label CriteriaLabel 
  250.       BackColor       =   &H00C0C0C0&
  251.       Caption         =   "Criteria:"
  252.       Height          =   256
  253.       Left            =   120
  254.       TabIndex        =   4
  255.       Top             =   3161
  256.       Width           =   1331
  257.    End
  258. Dim FShowSQL As Integer
  259. Dim FCopySQL As Integer
  260. Sub ANDButton_Click ()
  261.   Dim f As Field
  262.   If cField = "" Then Exit Sub
  263.   Set f = gCurrentDB.TableDefs(stSTF((cField), 0)).Fields(stSTF((cField), 1))
  264.   If cCriteria <> "" Then
  265.     cCriteria = cCriteria + Chr(13) + Chr(10) + "And "
  266.   End If
  267.   If f.Type = FT_STRING Or f.Type = FT_MEMO Or f.Type = FT_DATETIME Then
  268.     cCriteria = cCriteria + cField + " " + cOperator + " '" + cValue + "'"
  269.   Else
  270.     cCriteria = cCriteria + cField + " " + cOperator + " " + cValue
  271.   End If
  272.   cField.SetFocus
  273. End Sub
  274. Sub cField_Click ()
  275.   cValue.Clear
  276. End Sub
  277. Sub ClearButton_Click ()
  278.   cCriteria = ""
  279. End Sub
  280. Sub CloseButton_Click ()
  281.   Unload Me
  282. End Sub
  283. Sub CopySQLButton_Click ()
  284.   FCopySQL = True
  285.   Call RunQueryButton_Click
  286.   FCopySQL = False
  287. End Sub
  288. Sub cTableList_Click ()
  289.   Dim i As Integer, ii As Integer
  290.   Dim t As TableDef
  291.   Dim st As String
  292.   MsgBar "Updating Form Fields", True
  293.   cField.Clear
  294.   cShowFields.Clear
  295.   cGroupByField.Clear
  296.   cOrderByField.Clear
  297.   cValue.Clear
  298.   cGroupByField.AddItem "(none)"
  299.   cOrderByField.AddItem "(none)"
  300.   For ii = 0 To cTableList.ListCount - 1
  301.     If cTableList.Selected(ii) Then
  302.       Set t = gCurrentDB.TableDefs(cTableList.List(ii))
  303.       t.Fields.Refresh
  304.       For i = 0 To t.Fields.Count - 1
  305.         st = cTableList.List(ii) + "." + t.Fields(i).Name
  306.         cField.AddItem st
  307.         cShowFields.AddItem st
  308.         cGroupByField.AddItem st
  309.         cOrderByField.AddItem st
  310.       Next
  311.     End If
  312.   Next
  313.   If cField.List(0) <> "" Then
  314.     cField.ListIndex = 0
  315.     cGroupByField.ListIndex = 0
  316.     cOrderByField.ListIndex = 0
  317.   End If
  318.   MsgBar "", False
  319. End Sub
  320. Sub Form_Load ()
  321.    On Local Error GoTo FLErr
  322.    Dim ds As DynaSet
  323.    Dim i As Integer
  324.    Dim t As TableDef
  325.    'Clear listbox
  326.    cCriteria = ""
  327.    'Fill the Operator combo
  328.    cOperator.AddItem "="
  329.    cOperator.AddItem "<>"
  330.    cOperator.AddItem ">"
  331.    cOperator.AddItem ">="
  332.    cOperator.AddItem "<"
  333.    cOperator.AddItem "<="
  334.    cOperator.AddItem "Like"
  335.    cOperator.ListIndex = 0
  336.    'fill the table list
  337.    For i = 0 To fTables.cTableList.ListCount - 1
  338.      cTableList.AddItem fTables.cTableList.List(i)
  339.    Next
  340.    cTableList.ListIndex = 0
  341.    cValue = ""
  342.   GoTo FLEnd
  343. FLErr:
  344.   ShowError
  345.   Resume FLEnd
  346. FLEnd:
  347.   Height = 5520
  348.   Width = 7224
  349.   Left = (VDMDI.Width - Width) / 2
  350.   Top = 0
  351. End Sub
  352. Sub Form_Resize ()
  353.   If WindowState <> 1 Then
  354.     Height = 5520
  355.     Width = 7224
  356.   End If
  357. End Sub
  358. Sub GetValuesButton_Click ()
  359.   Dim ds As DynaSet
  360.   On Error GoTo GVErr
  361.   MsgBar "Getting Possible Values", True
  362.   SetHourGlass Me
  363.   Set ds = gCurrentDB.CreateDynaset("select Distinct " + (cField) + " from " + stSTF((cField), 0))
  364.   Do While ds.EOF = False
  365.     If Trim(ds(0)) <> "" Then
  366.       cValue.AddItem ds(0).Value
  367.     End If
  368.     ds.MoveNext
  369.   Loop
  370.   ds.Close
  371.   cValue = cValue.List(0)
  372.   cValue.SetFocus
  373.   GoTo GVEnd
  374. GVErr:
  375.   cValue = ""
  376.   Resume GVEnd
  377. GVEnd:
  378.   ResetMouse Me
  379.   MsgBar "", False
  380. End Sub
  381. Sub JoinButton_Click ()
  382.   Dim i As Integer
  383.   Dim c As Integer
  384.   For i = 0 To cTableList.ListCount - 1
  385.     If cTableList.Selected(i) = True Then
  386.       c = c + 1
  387.     End If
  388.   Next
  389.   If c < 2 Then
  390.     Beep
  391.     MsgBox "You Must Have at Least 2 Tables Selected!", 48
  392.   Else
  393.     MsgBar "Choose Joins", False
  394.     fJoin.Show MODAL
  395.     MsgBar "", False
  396.   End If
  397. End Sub
  398. Sub ORButton_Click ()
  399.   Dim f As Field
  400.   If cField = "" Then Exit Sub
  401.   Set f = gCurrentDB.TableDefs(stSTF((cField), 0)).Fields(stSTF((cField), 1))
  402.   If cCriteria <> "" Then
  403.     cCriteria = cCriteria + Chr(13) + Chr(10) + " Or "
  404.   End If
  405.   If f.Type = FT_STRING Or f.Type = FT_MEMO Or f.Type = FT_DATETIME Then
  406.     cCriteria = cCriteria + cField + " " + cOperator + " '" + cValue + "'"
  407.   Else
  408.     cCriteria = cCriteria + cField + " " + cOperator + " " + cValue
  409.   End If
  410.   cField.SetFocus
  411. End Sub
  412. Sub RunQueryButton_Click ()
  413.   On Error GoTo OKErr
  414.      Dim ds As DynaSet
  415.      Dim fs As String
  416.      Dim ts As String
  417.      Dim i As Integer
  418.     MsgBar "Building Query", True
  419.      If cCriteria <> "" Then
  420.        stWhere$ = "AND " + LTrim(cCriteria)
  421.        'strip CRLFs
  422.        For i = 1 To Len(stWhere$)
  423.          If Mid(stWhere$, i, 1) = Chr$(13) Then
  424.            stTmp$ = stTmp$ + " "
  425.          ElseIf Mid(stWhere$, i, 1) = Chr$(10) Then
  426.            'do nothing
  427.          Else
  428.            stTmp$ = stTmp$ + Mid(stWhere$, i, 1)
  429.          End If
  430.        Next
  431.        stWhere$ = stTmp$
  432.        stWhere$ = RTrim(stWhere$)
  433.      
  434.        'Add parens to stWhere$
  435.         stTmpWhere$ = stWhere$
  436.         Do
  437.           stTmp$ = stGetToken(stTmpWhere$, " ")
  438.           If fMatchParen% = False And UCase(stTmp$) = "AND" Then
  439.             stNewWhere$ = stNewWhere$ + stTmp$ + " ("
  440.             fMatchParen% = True
  441.           ElseIf fMatchParen% = True And UCase(stTmp$) = "AND" Then
  442.             stNewWhere$ = stNewWhere$ + ") " + stTmp$ + " ("
  443.             'fMatchParen% = False
  444.           Else
  445.             If UCase(stTmp$) = "OR" Or UCase(stTmp$) = "IN" Or UCase(stTmp$) = "LIKE" Then
  446.               stNewWhere$ = stNewWhere$ + " " + stTmp$ + " "
  447.             Else
  448.               stNewWhere$ = stNewWhere$ + stTmp$
  449.             End If
  450.           End If
  451.         Loop Until stTmpWhere$ = ""
  452.         stWhere$ = stNewWhere$ + ")"
  453.        'Build DynaSet string:
  454.        'Peel off leading AND/OR
  455.        If Mid(stWhere$, 2, 2) = "OR" Then
  456.          stWhere$ = Mid(stWhere$, 5, Len(stWhere$) - 5)
  457.        Else
  458.          stTmp$ = stGetToken(stWhere$, " ")
  459.        End If
  460.        If stWhere$ <> "" Then
  461.          stWhere$ = " Where " + stWhere$
  462.        End If
  463.      End If
  464.      'check for join condition
  465.      If cJoinFields.ListCount > 0 Then
  466.        If stWhere$ = "" Then
  467.          stWhere$ = stWhere$ + " Where "
  468.        Else
  469.          stWhere$ = stWhere$ + " And "
  470.        End If
  471.        For i = 0 To cJoinFields.ListCount - 1
  472.          stWhere$ = stWhere$ + cJoinFields.List(i) + " And "
  473.        Next
  474.        stWhere$ = Mid(stWhere$, 1, Len(stWhere$) - 5)
  475.      End If
  476.      
  477.      'check for group by field
  478.      If cGroupByField <> "(none)" Then
  479.        stWhere$ = stWhere$ + " Group By " + cGroupByField
  480.      End If
  481.      'check for order by field
  482.      If cOrderByField <> "(none)" Then
  483.        stWhere$ = stWhere$ + " Order By " + cOrderByField
  484.      End If
  485.      'get show field names
  486.      For i% = 0 To cShowFields.ListCount - 1
  487.        If cShowFields.Selected(i%) Then
  488.          fs = fs + cShowFields.List(i%) + ","
  489.        End If
  490.      Next
  491.      If fs = "" Then
  492.        For i% = 0 To cTableList.ListCount - 1
  493.          If cTableList.Selected(i%) Then
  494.            fs = fs + cTableList.List(i%) + ".*,"
  495.          End If
  496.        Next
  497.        If fs = "" Then
  498.          fs = "*"
  499.        Else
  500.          fs = Mid(fs, 1, Len(fs) - 1)     'take off the last ","
  501.        End If
  502.      Else
  503.        fs = Mid(fs, 1, Len(fs) - 1)
  504.      End If
  505.      'get table names
  506.      For i% = 0 To cTableList.ListCount - 1
  507.        If cTableList.Selected(i%) Then
  508.          ts = ts + cTableList.List(i%) + ","
  509.        End If
  510.      Next
  511.      ts = Mid(ts, 1, Len(ts) - 1)
  512.      gstDynaString = "Select " + fs + " From " + ts + stWhere$
  513.          
  514.      If FShowSQL = False And FCopySQL = False Then
  515.        MsgBar "Running Query", True
  516.        gfFromSQL = True
  517.        'create a new dynaset form
  518.        If VDMDI.cSingleRecord = True Then
  519.          Dim dsform1 As New fDynaset
  520.          dsform1.Show
  521.        Else
  522.          Dim dsform2 As New fGridFrm
  523.          dsform2.Show
  524.        End If
  525.      ElseIf FShowSQL = True Then
  526.        MsgBar "", False
  527.        MsgBox gstDynaString, 0, "SQL Query"
  528.      ElseIf FCopySQL = True Then
  529.        fSQL.cSQLStatement = gstDynaString
  530.      End If
  531.   GoTo OKEnd
  532. OKErr:
  533.   If Err = 364 Then Resume OKEnd   'catch unloaded form
  534.   ShowError
  535.   Resume OKEnd
  536. OKEnd:
  537.   MsgBar "", False
  538. End Sub
  539. Sub ShowSQLButton_Click ()
  540.   FShowSQL = True
  541.   Call RunQueryButton_Click
  542.   FShowSQL = False
  543. End Sub
  544. Function stGetToken (stLn$, stDelim$) As String
  545.     On Error GoTo GetTokenError
  546.     iOpenQuote% = InStr(1, stLn$, """")
  547.     iDelim% = InStr(1, stLn$, stDelim$)
  548.     If (iOpenQuote% > 0) And (iOpenQuote% < iDelim%) Then
  549.          iCloseQuote% = InStr(iOpenQuote% + 1, stLn$, """")
  550.          iDelim% = InStr(iCloseQuote% + 1, stLn$, stDelim$)
  551.     End If
  552.     If (iDelim% <> 0) Then
  553.          stToken$ = LTrim$(RTrim$(Mid$(stLn$, 1, iDelim% - 1)))
  554.          stLn$ = Mid$(stLn$, iDelim% + 1)
  555.     Else
  556.          stToken$ = LTrim$(RTrim$(Mid$(stLn$, 1)))
  557.          stLn$ = ""
  558.     End If
  559.     If (Len(stToken$) > 0) Then
  560.          If (Mid$(stToken$, 1, 1) = """") Then
  561.               stToken$ = Mid$(stToken$, 2)
  562.          End If
  563.          If (Mid$(stToken$, Len(stToken$), 1) = """") Then
  564.               stToken$ = Mid$(stToken$, 1, Len(stToken$) - 1)
  565.          End If
  566.     End If
  567.     stGetToken = stToken$
  568. GetTokenExit:
  569.     Exit Function
  570. GetTokenError:
  571.     Resume GetTokenExit
  572. End Function
  573. 'function to split the table and the field from a tbl.fld pair
  574. Function stSTF (tf As String, part As Integer) As String
  575.   If part = 0 Then
  576.     stSTF = Mid(tf, 1, InStr(1, tf, ".") - 1)
  577.   Else
  578.     stSTF = Mid(tf, InStr(1, tf, ".") + 1, Len(tf))
  579.   End If
  580. End Function
  581.