This example in "Look at the case of VFP: To add records to the data table and verify that the input data is legitimate" on the basis of improvements, to achieve the addition of records can not only complete the verification of input data, but also has the function of automatically generating numbers. The code that automatically generates the number is added to the form's Init event and click in the Add button, which is invoked when the form is first started or when the record is added, to implement the automatic numbering feature. The running interface is as follows:
This example uses the "Data 1" database "Web site information table", about the situation of the database is already looking at the case of VFP: sample database is given in the article, here is no longer detailed, the following is the production steps:
First, create a new form Form1 and set its Caption property value to "Automatically generate number when adding records to Datasheet", Widht property value to 290,height property value set to. T., save it as " Automatically generates a number when you add a record to a datasheet. 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.
Three, add three label controls to the form, the property values are set to number, site name, and site URL, and the three controls are listed in one column, and three text box controls are added to the right of the column label control, and the property values are not set by default, and they are lined up in a row , add two command buttons Command1 and Command2 to the right of the first text box, and the Caption property values are set to add and exit in turn. Adjust the size and position of each control appropriately, as shown in the following illustration of the form designer:
Iv. Add event code:
(i) Unload events for the form:
Set Exact off
Close data
(ii) The Init event of the form:
Set exact on
set talk off use
Web site information table this
. Text1.value= "" This
. Text2.value= "" This
. Text3.value= "" With This.grid1 width=290. left=0. top=0
" 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
this.grid1.Setall ("Dynamicbackcolor", "RGB (224,225,255)", "Column")
Calculate max (number) to MAXBH && find the record with the largest number in the datasheet
Szbh=val (Right (maxbh,3) && Remove the digital part If
szbh=0 then && determine if the digital portion is 0
zdbh= ' wz001 '
else zdbh= ' wz ' +padl alltrim (
str (szbh+ 1), 3, ' 0 ') && generate AutoNumber endif this
. TEXT1.VALUE=ZDBH this
. text1.readonly=.t.
This. Text2.setfocus
(iii) Click event code for the Add button (Command1):
B=alltrim (thisform. Text2.value) C=alltrim (thisform).
Text3.value) if empty (b) && This condition indicates that the text box is empty messagebox ("Please enter site name", 16, "System Prompt") return to locate for site name =b
If. not.eof () && This condition indicates that the content to be added and the table has a duplicate go top MessageBox ("Do not allow duplicate site names", 16, "system prompts") return endif
endif if Empty (c) MessageBox ("Please enter Web site URL", 16, "System Prompt") return else locate the for Web site URL =c if. not.eof () go MessageBox ("Do not allow duplicate website url", 16, "system hint") return endif endif msg=messagebox (' OK to add record? ', 32+4, ' system hint ') if msg=6 append blank go bottom Replace Web site information table. Number with Alltrim (thisform.
Text1.value),; Web site Information table-site name with Alltrim (thisform.
Text2.value),; Site Information table-Web site with Alltrim (thisform. Text3.value) MessageBox (' Save record Success! ', 64, ' system hint ') Thisform.grid1.refresh calculate max (number) to Maxbh && Find out the largest number of records in the datasheet szbh=val (right (maxbh,3)) && Remove the digital part if Szbh=0 then && determine if the number portion is 0 zdbh= ' wz001 ' else zdbh= ' wz ' +padl (str (ALLTRIM)), 3, ' 0 ') && Generate automatic numbering endif thisform. TEXT1.VALUE=ZDBH thisform. Text2.value= "" Thisform. Text3.value= "" Thisform. Text2.setfocus Thisform.refresh endif
(iv) Click event code for the Exit button (Command2): Thisform.release
Run "Automatically generate number when adding records to datasheet. Scx".
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