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

📄 adddeletebound.frm

📁 VB6数据库开发指南》的配套源程序
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   BackColor       =   &H00C0C0C0&
   Caption         =   "Bound Add / Delete / Update"
   ClientHeight    =   2460
   ClientLeft      =   1440
   ClientTop       =   2325
   ClientWidth     =   6420
   BeginProperty Font 
      Name            =   "MS Sans Serif"
      Size            =   8.25
      Charset         =   0
      Weight          =   700
      Underline       =   0   'False
      Italic          =   0   'False
      Strikethrough   =   0   'False
   EndProperty
   LinkTopic       =   "Form1"
   PaletteMode     =   1  'UseZOrder
   ScaleHeight     =   2460
   ScaleWidth      =   6420
   Begin VB.TextBox txtBirthDate 
      DataField       =   "BirthDate"
      DataSource      =   "datEmployees"
      Height          =   285
      Left            =   1800
      TabIndex        =   6
      Top             =   720
      Width           =   2055
   End
   Begin VB.TextBox txtEmployeeId 
      DataField       =   "EmployeeID"
      DataSource      =   "datEmployees"
      Enabled         =   0   'False
      Height          =   285
      Left            =   1800
      TabIndex        =   4
      Top             =   1200
      Width           =   1935
   End
   Begin VB.TextBox txtEmpFirstName 
      DataField       =   "FirstName"
      DataSource      =   "datEmployees"
      Height          =   285
      Left            =   3960
      TabIndex        =   1
      Top             =   240
      Width           =   2055
   End
   Begin VB.TextBox txtEmpLastName 
      DataField       =   "LastName"
      DataSource      =   "datEmployees"
      Height          =   285
      Left            =   1800
      MultiLine       =   -1  'True
      TabIndex        =   0
      Top             =   240
      Width           =   2055
   End
   Begin VB.Data datEmployees 
      Caption         =   "Employees"
      Connect         =   "Access"
      DatabaseName    =   "D:\Program Files\Microsoft Visual Studio\VB6\Nwind.mdb"
      DefaultCursorType=   0  'DefaultCursor
      DefaultType     =   2  'UseODBC
      Exclusive       =   0   'False
      Height          =   465
      Left            =   360
      Options         =   0
      ReadOnly        =   0   'False
      RecordsetType   =   1  'Dynaset
      RecordSource    =   "Employees"
      Top             =   1800
      Width           =   2535
   End
   Begin VB.Label Label2 
      Caption         =   "Birth Date:"
      Height          =   255
      Left            =   360
      TabIndex        =   5
      Top             =   720
      Width           =   975
   End
   Begin VB.Label Label3 
      Caption         =   "Employee ID:"
      Height          =   255
      Left            =   360
      TabIndex        =   3
      Top             =   1200
      Width           =   1215
   End
   Begin VB.Label Label1 
      AutoSize        =   -1  'True
      BackColor       =   &H00C0C0C0&
      Caption         =   "Employee:"
      Height          =   195
      Left            =   360
      TabIndex        =   2
      Top             =   240
      Width           =   885
   End
   Begin VB.Menu mnuFile 
      Caption         =   "&File"
      Begin VB.Menu mnuFileExit 
         Caption         =   "E&xit"
      End
   End
   Begin VB.Menu mnuEdit 
      Caption         =   "&Edit"
      Begin VB.Menu mnuEditUndo 
         Caption         =   "&Undo"
         Shortcut        =   ^Z
      End
   End
   Begin VB.Menu mnuData 
      Caption         =   "&Data"
      Begin VB.Menu mnuDataAdd 
         Caption         =   "&Add Record"
      End
      Begin VB.Menu mnuDataDelete 
         Caption         =   "&Delete Record"
      End
      Begin VB.Menu mnuDataSave 
         Caption         =   "&Save Record"
         Enabled         =   0   'False
      End
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Private Utility As New clsUtility
Private mblnValidationFailed As Boolean

Private Sub datEmployees_Validate(Action As Integer, Save As Integer)
    Dim strMsg As String
    Dim enumMsgResult As VbMsgBoxResult
        
    If Save = True Or Action = vbDataActionUpdate _
    Or Action = vbDataActionUnload _
    Or Action = vbDataActionAddNew Then
        ' One or more bound controls has changed or a the form
        ' is being unloaded, so verify that all fields have
        ' legal entries. If a field has an incorrect value,
        ' append a string explaining the error to strMsg and
        ' set the focus to that field to facilitate correcting
        ' the error. We explain all errors encountered in a
        ' single message box.
        strMsg = ""
        If txtEmpLastName.Text = "" Then
             Utility.AddToMsg strMsg, "You must enter a last name."
             txtEmpLastName.SetFocus
        End If
        If txtEmpFirstName.Text = "" Then
             Utility.AddToMsg strMsg, "You must enter a first name."
             txtEmpFirstName.SetFocus
        End If
        If Not IsDate(txtBirthDate.Text) Then
             Utility.AddToMsg strMsg, "You must enter a birth date."
             txtBirthDate.SetFocus
        Else
            If CDate(txtBirthDate.Text) >= Date Then
                 Utility.AddToMsg strMsg, "Birth date must be in the past."
                 txtBirthDate.SetFocus
            End If
        End If
        
        If strMsg <> "" Then
             ' We have something in the variable strMsg, which means that
             ' an error has occurred. Display the message. The focus is in
             ' the last text box where an error was found
             enumMsgResult = MsgBox(strMsg, vbExclamation + vbOKCancel + _
                     vbDefaultButton1)
             
             If enumMsgResult = vbCancel Then
                 'Restore the data to previous values using the data control
                 datEmployees.UpdateControls
                 mblnValidationFailed = False
             Else
                 ' Cancel the Validate event
                 Action = vbDataActionCancel
                 ' Deny form Unload until fields are corrected
                 mblnValidationFailed = True
             End If
         Else
             ' Allow form unload
             mblnValidationFailed = False
             ' Disable the save menu
             mnuDataSave.Enabled = False
         End If
    End If

End Sub

Private Sub Form_Unload(Cancel As Integer)
    
    ' Don't allow the unload until the data is validate or the
    ' update is cancelled
    If mblnValidationFailed Then Cancel = True
    
End Sub

Private Sub mnuDataAdd_Click()

    ' Reset all controls to the default for a new record
    ' and make space for the record in the recordset copy
    ' buffer.
    datEmployees.Recordset.AddNew
    
    'Enable the save menu choice
    mnuDataSave.Enabled = True
    
    ' Set the focus to the first control on the form
    txtEmpLastName.SetFocus
End Sub

Private Sub mnuDataDelete_Click()
    Dim strMsg As String
    
    'Verify the deletion.
    strMsg = "Are you sure you want to delete " _
            & IIf(txtEmpLastName.Text <> "", txtEmpLastName.Text, _
                "this record") & "?"
    If MsgBox(strMsg, vbQuestion + vbYesNo + vbDefaultButton2) = vbYes Then
        
        ' We really want to delete
        datEmployees.Recordset.Delete
        
        ' Make a valid record the current record and update the display.
        datEmployees.Recordset.MoveNext
        
        ' If we deleted the last record, move to the new last record
        ' because the current record pointer is not defined after
        ' deleting the last record, even though EOF is defined.
        If datEmployees.Recordset.EOF Then datEmployees.Recordset.MoveLast
    End If
End Sub

Private Sub mnuDataSave_Click()

    ' Invoke the update method to copy control contents to
    ' recordset fields and update the underlying table
    datEmployees.Recordset.Update
    If datEmployees.Recordset.EditMode <> dbEditAdd Then
        
        ' If we added move to the new record
        datEmployees.Recordset.MoveLast
   End If
    
End Sub

Private Sub mnuEditUndo_Click()
    
    ' Undo all pending changes from form by copying recordset values
    ' to form controls
    datEmployees.UpdateControls
    
    If datEmployees.Recordset.EditMode = dbEditAdd Then
        
        ' Disable the menu save and cancel the update
        datEmployees.Recordset.CancelUpdate
        mnuDataSave.Enabled = False
    End If
    
End Sub

Private Sub mnuFileExit_Click()
    
    Unload Me

End Sub

⌨️ 快捷键说明

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