⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 042.htm

📁 delphi教程
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<p>finally</p><p>Screen.Cursor := crDefault;</p><p>end;</p><p>end;</p><p> </p><p>procedure TQueryForm.BitBtn2Click(Sender: TObject);</p><p>var</p><p>strAlias, { Alias name selected by the user }</p><p>strTable, { Table name selected by the user }</p><p>strField, { Field name selected by the user }</p><p>strValue, { Field Value entered by the user }</p><p>strWhere, { WHERE clause for the user's query }</p><p>strQuote, { Holds quotes is the query field is text }</p><p>strQuery: string; { String used to construct the query }</p><p>frmQuery: TResultForm; { The Results form }</p><p>type</p><p> </p><p>{ The following type is used with the Type drop-down</p><p>list. The text values corresponding with each item is</p><p>described in comments, along with the relevant SQL operators. }</p><p> </p><p>etSQLOps = (soNoCondition, { not field conditions: no WHERE clause }</p><p>soEqual, { equals: = }</p><p>soNotEqual, { is not equal to: &lt;&gt; }</p><p>soLessThan, { is less than: &lt; }</p><p>soLessEqual, { is less than or equal to: &lt;= }</p><p>soMoreThan, { is greater than: &gt; }</p><p>soMoreEqual, { is greater than or equal to: &gt;= }</p><p>soStartsWith, { starts with: LIKE xx% }</p><p>soNoStartsWith, { doesn't start with: NOT LIKE xx% }</p><p>soEndsWith, { ends with: LIKE %xx }</p><p>soNoEndsWith, { doesn't end with: NOT LIKE %xx }</p><p>soContains, { contains: LIKE %xx% }</p><p>soNoContains, { doesn't contain: NOT LIKE %xx% }</p><p>soBlank, { is blank: }</p><p>soNotBlank, { is not blank: }</p><p>soInside, { contains only: IN ( xx, yy, zz ) }</p><p>soOutside); { doesn't contain: NOT IN (xx, yy, zz) }</p><p>begin</p><p> </p><p>{ Initialize the variables needed to run the query }</p><p> </p><p>with ListBox1 do</p><p>if ItemIndex = -1 then</p><p>raise Exception.Create('Can''t Run Query: No Alias Selected')</p><p>else</p><p>strAlias := Items.Strings[ItemIndex];</p><p> </p><p>with ListBox2 do</p><p>if ItemIndex = -1 then</p><p>raise Exception.Create('Can''t Run Query: No Table Selected')</p><p>else</p><p>strTable := Items.Strings[ItemIndex];</p><p> </p><p>with ListBox3 do</p><p>if ItemIndex = -1 then</p><p>begin</p><p>if ComboBox1.ItemIndex &gt; Ord(soNocondition) then</p><p>raise Exception.Create('Can''t Run Query: No Field Selected')</p><p>else</p><p>strField := '';</p><p>end</p><p>else</p><p>strField := Items.Strings[ItemIndex];</p><p> </p><p>if (Edit1.Text = '') and</p><p>(ComboBox1.ItemIndex &gt; Ord(soNoCondition)) and</p><p>(ComboBox1.ItemIndex &lt; Ord(soBlank)) then</p><p>raise Exception.create('Can''t Run Query: No Search Value Entered')</p><p>else</p><p>strValue := Edit1.Text;</p><p> </p><p>{ See if the field being search is a string field. If so, then pad the</p><p>quote string with quotation marks; otherwise, set it to a null value. }</p><p> </p><p>if strField &lt;&gt; '' then</p><p>with Table1.FieldByName(strField) do</p><p>if (DataType = ftString) or (DataType = ftMemo) then</p><p>strQuote := '&quot;' else</p><p>strQuote := '';</p><p> </p><p>{ Construct the WHERE clause of the query based on the user's choice</p><p>in Type. }</p><p> </p><p>case etSQLOps(ComboBox1.ItemIndex) of</p><p>soNoCondition: strWhere := '';</p><p>soEqual: strWhere := strField + ' = ' + strQuote + strValue+ strQuote;</p><p>soNotEqual: strWhere := strField + ' &lt;&gt; ' + strQuote + strValue +</p><p>strQuote;</p><p>soLessThan: strWhere := strField + ' &lt; ' + strQuote + strValue +</p><p>strQuote;</p><p>soLessEqual: strWhere := strField + ' &lt;= ' + strQuote + strValue +</p><p>strQuote;</p><p>soMoreThan: strWhere := strField + ' &gt; ' + strQuote + strValue +</p><p>strQuote;</p><p>soMoreEqual: strWhere := strField + ' &gt;= ' + strQuote + strValue +</p><p>strQuote;</p><p>soStartsWith: strWhere := strField + ' LIKE ' + strQuote +</p><p>strValue + '%' + strQuote;</p><p>soNoStartsWith: strWhere := strField + ' NOT LIKE ' + strQuote +</p><p>strValue + '%' + strQuote;</p><p>soEndsWith: strWhere := strField + ' LIKE ' + strQuote +</p><p>'%' + strValue + strQuote;</p><p>soNoEndsWith: strWhere := strField + ' NOT LIKE ' +</p><p>strQuote + '%' + strValue + strQuote;</p><p>soContains: strWhere := strField + ' LIKE '+ strQuote+'%'+ strValue</p><p>+ '%' + strQuote;</p><p>soNoContains: strWhere := strField + ' NOT LIKE ' + strQuote + '%'</p><p>+ strValue + '%' + strQuote;</p><p>soBlank: strWhere := strField + ' IS NULL';</p><p>soNotBlank: strWhere := strField + ' IS NOT NULL';</p><p>end;</p><p> </p><p>if ComboBox1.ItemIndex = Ord(soNoCondition) then</p><p>strQuery := 'SELECT * FROM &quot;' + strTable + '&quot;'</p><p>else if Table1.FieldByName(strField).DataType = ftString then</p><p>strQuery := 'SELECT * FROM &quot;' + strTable + '&quot; t WHERE t.' + strWhere</p><p>else</p><p>strQuery := 'SELECT * FROM &quot;' + strTable + '&quot; t WHERE t.' + strWhere;</p><p> </p><p>{ Create an instance of the browser form. }</p><p>frmQuery := TResultForm.Create(Application);</p><p> </p><p>{ Use a resource protection block in case an exception is raised. This</p><p>ensures that the memory allocated for the Results form is released. }</p><p>try</p><p>with frmQuery do</p><p>begin</p><p>Screen.Cursor := crHourglass;</p><p>if Query1.Active then Query1.Close;</p><p>Query1.DatabaseName := strAlias; {set the alias the query poitns to}</p><p>Query1.SQL.clear; { empty existing SQL in the query }</p><p>Query1.SQL.Add(strQuery); { add query string to query object }</p><p>Query1.Active := True; { try to run the query }</p><p>Screen.Cursor := crDefault;</p><p> </p><p>if Query1.Active then</p><p>begin</p><p>{ If the query didn't return any records, there's no point in</p><p>displaying the form. In that event, raise an exception. }</p><p>if Query1.RecordCount &lt; 1 then</p><p>raise Exception.create('No records matched your criteria. </p><p>Please try again.' );</p><p> </p><p>{ write a message to the browse form's status line }</p><p>if strField = '' then</p><p>Panel3.Caption := 'Now showing all records from ' + strTable </p><p>+ '...'</p><p>else</p><p>Panel3.Caption := 'Now showing '+ strTable +' where '+ strField</p><p>+' contains values equal to '+ strValue + '...';</p><p> </p><p>{ show the form }</p><p>ShowModal;</p><p>end;</p><p>end;</p><p>finally</p><p>frmQuery.Free;</p><p>end;</p><p>end;</p><p> </p><p>end.</p><p> </p><p> </p><p>unit RSLTFORM;</p><p> </p><p>interface</p><p> </p><p>uses</p><p>SysUtils, Windows, Messages, Classes, Graphics, Controls, StdCtrls, DB,</p><p>Forms, DBCtrls, DBGrids, DBTables, Buttons, Grids, ExtCtrls, Dialogs;</p><p> </p><p>type</p><p>TResultForm = class(TForm)</p><p>DBGrid1: TDBGrid;</p><p>DBNavigator: TDBNavigator;</p><p>Panel1: TPanel;</p><p>DataSource1: TDataSource;</p><p>Panel2: TPanel;</p><p>Panel3: TPanel;</p><p>Query1: TQuery;</p><p>SpeedButton2: TSpeedButton;</p><p>Panel4: TPanel;</p><p>SpeedButton1: TSpeedButton;</p><p>procedure SpeedButton1Click(Sender: TObject);</p><p>procedure SpeedButton2Click(Sender: TObject);</p><p>end;</p><p> </p><p>var</p><p>ResultForm: TResultForm;</p><p> </p><p>implementation</p><p> </p><p>{$R *.DFM}</p><p> </p><p>procedure TResultForm.SpeedButton1Click(Sender: TObject);</p><p>begin</p><p>Close;</p><p>end;</p><p> </p><p>procedure TResultForm.SpeedButton2Click(Sender: TObject);</p><p>var</p><p>strText: string; { Variable to hold display text }</p><p>iCounter: Integer; { Loop counter variable }</p><p>begin</p><p> </p><p>{ Build a string containing the query }</p><p> </p><p>strText := '';</p><p>for iCounter := 0 to Query1.SQL.Count - 1 do</p><p>strText := strText + Query1.SQL[iCounter];</p><p> </p><p>{ Display the query text }</p><p> </p><p>MessageDlg('The underlying query is: ' + #10 + #10 + strText,</p><p>mtInformation, [mbOK], 0 );</p><p>end;</p><p> </p><p>end.</p><p> </p><hr  width="94%"></TD><TD CLASS="tt3" VALIGN="bottom" width="8%"  ><strong><A HREF="043.htm"><FONT style="FONT-SIZE: 9pt">后一页</font></A><BR><A HREF="041.htm"><FONT style="FONT-SIZE: 9pt">前一页</font></A><BR><A HREF="index.html"><FONT style="FONT-SIZE: 9pt">回目录</font></A><BR></strong></TD></TR></table></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -