Look at the case study VFP: Create a query with a SELECT statement

Source: Internet
Author: User

In VFP, you can use SQL statements to manipulate tables, and you do not need to open the database when you use SQL statements to manipulate tables. This example describes the Select query statement (data retrieval), and the format of the SELECT statement refer to the Select SQL command or the SQL language tutorial.

In the case of VFP: to locate and find the records in the table, we use the locate statement as a record location and use the EOF () function, if the function returns the True value, the data table does not exist the query records and give the hint information. This example overwrites this section with an SQL statement. The difference with the previous example is roughly as follows:

1. In this example, the table control is still used as the data display control, and the Site information table is set as the data source for the table control when the form is init. When you click the Find button, output the query results as a temporary table (Cursor) (and, of course, you can output the query results as a table, and ask the reader to try it yourself).

2. This example does not use locate positioning, instead, determine the number of records in the command button's Click event code for a temporary table that is output from the query, and if the number of records in the table is >0 to indicate that the queried record exists in the database, set the temporary table as the data source for the table control and refresh the table; Indicates that the queried record does not exist in the database, the prompt is first given, and then the Click event code of the Restore button is invoked using Thisform.command2.click () (that is, a method in VFP that can also invoke another object in the method program of an object). To achieve the purpose of simplifying the code) resets the data source for the table control and refreshes the display.

This example runs the interface as shown below:

This example uses the "Data 1" database "Web site information table", about the database is already looking at the case of VFP: sample database in the article, the following is the production steps:

First, create a new form Form1 and set its Caption property to "Create a query with a SELECT statement", set the Width property value to the 290,height property value set to the 165,autocenter property value set to. T, and save it as " Create a query with a SELECT statement. Scx ".

Add a Grid control to the form, set its ReadOnly property to. T, and its recordsourcetype value with the default "1-alias", and in the form designer, width and height, and then roughly lower it, Specific attribute values we will set them in the form's Init event code.

Third, add two label controls below the grid control, line them in one row, and set their Caption property to "Select Find how" and "Enter what you want to find."

Four, add a combo box control below the label control Combo1 and a text box control Text1, the text box control's property value is the default, the combo box control Combo1 's RowSourceType property value is set to "N", and the RowSource property value is set to " Number, website name, website url "so that at runtime we can select the fields to query in the combo box.

Five, add two command buttons Command1 and Command2 on the form, and set their Caption property to find and redo. Adjust the position of the controls on the form appropriately, as shown in the following illustration:

Vi. Add event code:

(i) The Unload event code for the form Form1:

Close data

(ii) The Init event code for the form Form1:

Use Web site information table
this.combo1.value= "number" with
thisform.grid1
  . width=290. height=100 left=0.
  top=0
  . Recordsource= "website Information table"
  . Deletemark=.f.
  visible=.t..
  readonly=.t..
  Columncount=3
  . column1.header1.caption= "Number"
  . Column1.header1.backcolor=rgb (255,255,190)
  . Column2.header1.backcolor=rgb (255,255,190)
  . column2.header1.caption= "website name"
  . Column3.header1.backcolor=rgb (255,255,190)
  . column3.header1.caption= "website url"
  . Column1.width=75
  . Column2.width=80
  . column3.width=150
endwith
thisform.grid1.Setall ("Dynamicbackcolor", "RGB (224,225,255)", "Column")

(iii) Click event code for the Find button (Command1):

If empty (thisform. Text1.value) =.f. Go top a=thisform. Combo1.value B=alltrim (thisform.
     Text1.value) Local C As Integer if a= "numbered" SELECT * from Web site information table where number ==b into cursor temporary web site information table Sele Temporary web site information table
       C=reccount () If c<1 use MessageBox ("The record you want to query does not exist in the database", 16, "system hint") Thisform.command2.click () 
     Return endif endif if a= "site name" SELECT * from site information table where site name ==b into cursor temporary web site information table Sele Temporary web site information table C=reccount () If c<1 use MessageBox ("The record you want to query does not exist in the database", 16, "system hint") Thisform.command2.click () return endif endif if a= "website url" select * From site information table where Web site URL ==b into cursor temporary web site information table Sele Temporary network Station Information Table C=reccount () If c<1 use MessageBox ("The record you want to query does not exist in the database", 16, "system hint") thisform.command2.c Lick () return endif endif with Thisform.grid1. width=350. height=100. left=10
    E= "Temporary web site Information table". Deletemark=.f.
    . visible=.t. . Readonly=.t. . Columncount=3. column1.header1.caption= "Number". Column1.header1.backcolor=rgb (255,255,190). Column2.header1.backcolor=rgb (255,255,190). column2.header1.caption= "website name". Column3.header1.backcolor=rgb (255,255,190). column3.header1.caption= "website url". Column1.width=75. Column2.width=80.
  column3.width=150 endwith thisform.grid1.Setall ("Dynamicbackcolor", "RGB (224,225,255)", "Column") Thisform.grid1.setfocus Else MessageBox ("Please enter the content you want to find!") ", 16," system hint ") thisform. Text1.value= "" Thisform. Text1.setfocus endif return

(iv) Click event code for the Restore button (Command2):

Sele website Information table go top
thisform. Text1.value= ""
thisform. Text1.setfocus with
thisform.grid1.
  width=290
  top=0
  . left=0 "Site Information table"
  . visible=.t.
  readonly=.t..
  Columncount=3
  . column1.header1.caption= "Number"
  . Column1.header1.backcolor=rgb (255,255,190)
  . Column2.header1.backcolor=rgb (255,255,190)
  . column2.header1.caption= "website name"
  . Column3.header1.backcolor=rgb (255,255,190)
  . column3.header1.caption= "website url"
  . Column1.width=75
  . Column2.width=80
  . column3.width=150
endwith
thisform.grid1.Setall ("Dynamicbackcolor", "RGB (224,225,255)", "Column")
Thisform.refresh

Run "Create query with SELECT statement, Scx", you can achieve the effect of this example.

Resources:

The basic course of VFP: http://bianceng.cnhttp://www.bianceng.cn/vfpjc/index0.htm

VFP Primary Course: http://bianceng.cnhttp://www.bianceng.cn/cc/index.htm

VFP Intermediate Course: http://bianceng.cnhttp://www.bianceng.cn/mcc/mcc.htm

VFP Advanced Course: http://bianceng.cnhttp://www.bianceng.cn/hcc/hcc.htm

VFP Network Development: http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpwlkf.htm

VFP Call API function: http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpapi.htm

VFP Report Print: http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpreport.htm

VFP Common technique: http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpcyjs.htm

VFP Experience Summary: http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpjyhz.htm

VFP Control use: http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpkjsy.htm

VFP Data processing: http://bianceng.cnhttp://www.bianceng.cn/VFPwz/vfpsjcl.htm

This example code is debugged in the win2003+vfp6.0 environment.

See the full set of "rookie also learn VFP" tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.