frmupdatecategory.frm

来自「数据库课程设计」· FRM 代码 · 共 233 行

FRM
233
字号
VERSION 5.00
Begin VB.Form frmUpdateCat 
   Caption         =   "类别-修改删除"
   ClientHeight    =   2895
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6495
   LinkTopic       =   "Form1"
   ScaleHeight     =   2895
   ScaleWidth      =   6495
   StartUpPosition =   3  'Windows Default
   Begin VB.Frame Frame2 
      Caption         =   "类别信息"
      Height          =   1575
      Left            =   120
      TabIndex        =   3
      Top             =   1200
      Width           =   6255
      Begin VB.TextBox txtDescription 
         Height          =   645
         Left            =   1200
         MultiLine       =   -1  'True
         TabIndex        =   8
         Top             =   720
         Width           =   4815
      End
      Begin VB.TextBox txtName 
         Height          =   285
         Left            =   3360
         TabIndex        =   7
         Text            =   "Text2"
         Top             =   360
         Width           =   2655
      End
      Begin VB.TextBox txtNo 
         Enabled         =   0   'False
         Height          =   285
         Left            =   1200
         TabIndex        =   5
         Text            =   "Text1"
         Top             =   360
         Width           =   855
      End
      Begin VB.Label Label4 
         Caption         =   "类别描述:"
         Height          =   255
         Left            =   240
         TabIndex        =   9
         Top             =   720
         Width           =   975
      End
      Begin VB.Label Label3 
         Caption         =   "类别名称:"
         Height          =   255
         Left            =   2400
         TabIndex        =   6
         Top             =   360
         Width           =   975
      End
      Begin VB.Label Label2 
         Caption         =   "类别编号:"
         Height          =   255
         Left            =   240
         TabIndex        =   4
         Top             =   360
         Width           =   1095
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "选择"
      Height          =   975
      Left            =   120
      TabIndex        =   0
      Top             =   120
      Width           =   6255
      Begin VB.CommandButton cmdCancel 
         Caption         =   "返回(&C)"
         Height          =   375
         Left            =   4920
         TabIndex        =   12
         Top             =   360
         Width           =   975
      End
      Begin VB.CommandButton cmdDelete 
         Caption         =   "删除(&D)"
         Height          =   375
         Left            =   3840
         TabIndex        =   11
         Top             =   360
         Width           =   975
      End
      Begin VB.CommandButton cmdUpdate 
         Caption         =   "修改(&U)"
         Height          =   375
         Left            =   2760
         TabIndex        =   10
         Top             =   360
         Width           =   975
      End
      Begin VB.ComboBox cboName 
         Height          =   315
         Left            =   1320
         TabIndex        =   1
         Text            =   "Combo1"
         Top             =   360
         Width           =   1215
      End
      Begin VB.Label Label1 
         Caption         =   "类别名称:"
         Height          =   255
         Left            =   360
         TabIndex        =   2
         Top             =   360
         Width           =   975
      End
   End
End
Attribute VB_Name = "frmUpdateCat"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

Public sqlStr As String
Public msgText As String

Private Sub cboName_Click()

'从数据库中读取类别信息并显示出来
getCatInfo

End Sub

Private Sub cmdCancel_Click()
Unload Me

End Sub

Private Sub cmdDelete_Click()
   Dim conn As ADODB.Connection
   
   sqlStr = "delete from categories where [no]=" & Trim(txtNo.Text)
   On Error GoTo exitSub
   Set conn = New ADODB.Connection
   conn.Open connStr
   
   conn.Execute sqlStr
   MsgBox "成功删除数据!!"
   initCategories
   
exitSub:
   
   conn.Close

End Sub

Private Sub cmdUpdate_Click()
   Dim conn As ADODB.Connection
   
   '组合得到完成数据修改的SQL语句
   sqlStr = "update categories set [name]='" & txtName.Text _
          & "',[description]='" & txtDescription.Text _
          & "' where [no]=" & Trim(txtNo.Text)
   On Error GoTo exitSub
   Set conn = New ADODB.Connection
   conn.Open connStr
   
   '执行SQL语句
   conn.Execute sqlStr
   MsgBox "成功修改数据!!"
   initCategories
   
exitSub:
   
   conn.Close

End Sub

Private Sub Form_Load()

'窗体居中显示
Me.Top = (Screen.Height - Me.Height) \ 2
Me.Left = (Screen.Width - Me.Width) \ 2

initCategories
End Sub
Sub initCategories()
Dim rstCategory As ADODB.Recordset

    '从数据库中读取所有类别并添加到组合列表框中
    sqlStr = "select name from categories"
    Set rstCategory = ExecuteSQL(sqlStr, msgText)
    cboName.Clear
    
    If Not rstCategory.EOF Then
        
            Do While Not rstCategory.EOF
                cboName.AddItem Trim(rstCategory.Fields(0))
                rstCategory.MoveNext
            Loop
            cboName.ListIndex = 0
        
    Else
        MsgBox "没有类别信息,请添加!", vbOKOnly + vbExclamation, "警告"
        
        Exit Sub
    End If
    rstCategory.Close

End Sub

Sub getCatInfo()
Dim rstCategory As ADODB.Recordset

    '根据类别名称从数据库中查找类别信息
    sqlStr = "select * from categories where name='" & cboName.Text & "'"
    Set rstCategory = ExecuteSQL(sqlStr, msgText)
    
    If Not rstCategory.EOF Then
        txtNo.Text = rstCategory.Fields("no")
        txtName.Text = Trim(rstCategory.Fields("name"))
        txtDescription.Text = Trim(rstCategory.Fields("description"))
        
    Else
        MsgBox "没有类别信息,请添加!", vbOKOnly + vbExclamation, "警告"
        
        Exit Sub
    End If
    rstCategory.Close

End Sub

⌨️ 快捷键说明

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