VB Prodigy Tutorial (cont.)--adding Event codes for objects

Source: Internet
Author: User
Tags exit empty modify

This article welcomes the non-commercial use of reprint, but need to indicate from the "Programming Network" and the corresponding URL link.

One, Form1 Load event code:

Private Sub Form_Load ()

  Form1.MS1.ColWidth (0) =
  Form1.MS1.ColWidth (1) = 1000
  Form1.MS1.ColWidth (2) = 2300
  Form1.MS1.ColWidth (3) = 4000
  Form1.Text1.Text = ""
  Form1.Text2.Text = ""
  Form1.Text3.Text = ""
  Form1.Text4.Text = ""
End Sub

The main is to set the width of the MSHFlexGrid control when the system initializes and empty the text box.

Second, the "Add Record" button (Command1) Click event Code:

Private Sub Command1_Click () Dim SC as Integer If text1.text = "" or Text2.text = "" or Text3.text = "Then" that is, site name, web

Station address and Web site description must be filled in to open the database connection write data ' because the system database is designed as the number field is the AutoNumber of access ' and therefore does not receive the numbered data when the record is added. Automatically numbered by Access MsgBox ("Please enter complete Web site information") Else sc = MsgBox (Are you sure you want to add this record?) ", vbOKCancel," hint info ") If sc = 1 Then ' Runtime if the user clicks on the" OK "button of the MsgBox Prompt box, the return value is 1 Dim conn as New ADODB. Connection Dim rs as New ADODB. Recordset Dim Str1 As String Dim Str2 As String Dim Str3 As String Str
      
      1 = "provider=microsoft.jet.oledb.4.0;"
      
      STR2 = "Data source=e:\vb\access_db.mdb;" STR3 = "Jet oledb:database password=" Conn. Open Str1 & Str2 & STR3 strSQL = "SELECT * from Wzdz" Rs. Open strSQL, Conn, 3, 3 Rs. AddNew rs! Website name = Text1.Text rs! website address = text2.text rs! web Site Description = Text3.text Rs. Update Rs. Close Conn. Close MsgBox ("Add record success!") ") ADODC1. Refresh ' Refreshes the data source, and the MSHFlexGrid control refreshes the display data end If ' The following four statements are empty after the operation is completed text1.text = "Text2.text =" "Tex T3. Text = "" Text4.text = "" End If end Sub

Three, modify Record button (COMMAND2) Click event Code:

Private Sub Command2_Click () If not IsNumeric (text4.text) Or Val (Text4.text) = 0 Then ' Number field is automatically numbered in access, for natural number ' so Text4 's Content for validation, if not a value or 0 jump out of Sub procedure MsgBox "record number is more than 0 natural number, please enter the correct number!" "Exit Sub End If Text1.Text =" "or Text2.text =" "or Text3.text =" Then "verifies the contents of three text boxes without receiving null values MsgBox" Please enter the end of the The whole website information! "Exit Sub end If Dim sc as Integer sc = MsgBox (" Do you really want to modify this record?) ", vbOKCancel," hint info ") If sc = 1 Then ' Runtime if the user clicks on the" OK "button of the MsgBox Prompt box, the return value is 1 Dim conn as New ADODB. Connection Dim rs as New ADODB. Recordset Dim Str1 As String Dim Str2 As String Dim Str3 As String Str1 = "PROVIDER=MICR Osoft.
      
   jet.oledb.4.0; "
      
   STR2 = "Data source=e:\vb\access_db.mdb;" STR3 = "Jet oledb:database password=" Conn. Open Str1 & Str2 & STR3 strSQL = "SELECT * from Wzdz where number =" & Val (Text4.text) & "" Rs.
   Open strSQL, Conn, 3, 3 If rs! number = Val (text4.text) Then ' because the system database uses AutoNumber as the primary key ' so the content of the numbered field is the basis for judging
   ' If the rs! number = Val (Text4.text) has this record in the database, ' the contents of the other three fields will be modified ' otherwise give the "no this record" prompt and close the data connection rs! site name = Text1.Text rs! website address = text2.text rs! web Site Description = Text3.text Rs. Update Rs. Close Conn. Close MsgBox ("Modify record succeeded!") Adodc1.refresh ' Refresh the data source, the MSHFlexGrid control will refresh the display data in real time Else MsgBox ("This record does not exist!") ") Text1.Text =" "Text2.text =" "Text3.text =" "Text4.text =" "RS . Close Conn. The effect of the following four statements, close Exit Sub-End If, is to empty the text box after the operation completes Text1.Text = "Text2.text =" "Text3.text =" "Text4.te XT = "" End Sub

Four, Delete Record button (COMMAND3) Click event Code:

Private Sub Command3_Click () If not IsNumeric (text4.text) Or Val (Text4.text) = 0 Then ' Number field is automatically numbered in access, for natural number ' so Text4 's Content for validation, if not a value or 0 jump out of Sub procedure MsgBox "number is more than 0 natural number, please enter the correct number!" "Exit Sub end If Dim sc as Integer sc = MsgBox (" Are you sure you want to delete this record?) ", vbOKCancel," delete confirmation! ") If sc = 1 Then ' Runtime if the user clicks on the" OK "button of the MsgBox prompt, the return value is 1 Dim conn as New ADODB. Connection Dim rs as New ADODB. Recordset Dim Str1 As String Dim Str2 As String Dim Str3 As String Str1 = "Provider=microsof
      
  t.jet.oledb.4.0; "
      
  STR2 = "Data source=e:\vb\access_db.mdb;" STR3 = "Jet oledb:database password=" Conn. Open Str1 & Str2 & STR3 strSQL = "SELECT * from Wzdz where number =" & Val (Text4.text) & "" Rs. Open strSQL, Conn, 3, 3 if rs! number = Val (text4.text) Then ' because the system database uses AutoNumber as the primary key ' so the content of the numbered field as the basis for judgment ' if R s! number = Val (Text4.text) describes this record in the database and deletes it ' otherwise give the "no presence this record" prompt and turn off the data connection Rs. Delete Rs. Close Conn. Close MsgBox ("Delete record succeeded!") "Adodc1.refresh ' Refresh the data source, the MSHFlexGrid control will refresh the display data in real time Else MsgBox ("This record does not exist!") ") Text4.text =" "Rs. Close Conn. The effect of the following four statements, close Exit Sub-End If, is to empty the text box after the operation completes Text1.Text = "Text2.text =" "Text3.text =" "Text4.text =" "End Sub

The Click event code for the Exit System button (COMMAND4):

Private Sub Command4_click ()

Dim SC as Integer

sc = MsgBox (Are you sure you want to exit the system?) ", vbOKCancel," hint info ")

if sc = 1 Then

' Runtime if the user clicks on the" OK "button of the MsgBox Prompt box, the return value is the 1 end-
  
if End
Sub

Six, after adding the event code, we this example to complete, run it!

See a full set of "VB Prodigy Tutorials" (continued)

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.