frmupdatecourse.frm

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

FRM
299
字号
VERSION 5.00
Begin VB.Form frmUpdateCourse 
   Caption         =   "课程-修改删除"
   ClientHeight    =   3240
   ClientLeft      =   60
   ClientTop       =   345
   ClientWidth     =   6615
   LinkTopic       =   "Form1"
   ScaleHeight     =   3240
   ScaleWidth      =   6615
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton cmdCancel 
      Caption         =   "返回(&C)"
      Height          =   375
      Left            =   4200
      TabIndex        =   19
      Top             =   2760
      Width           =   1215
   End
   Begin VB.CommandButton cmdDelete 
      Caption         =   "删除(&D)"
      Height          =   375
      Left            =   2640
      TabIndex        =   18
      Top             =   2760
      Width           =   1215
   End
   Begin VB.CommandButton cmdUpdate 
      Caption         =   "修改(&U)"
      Height          =   375
      Left            =   1080
      TabIndex        =   17
      Top             =   2760
      Width           =   1215
   End
   Begin VB.Frame Frame2 
      Caption         =   "查询"
      Height          =   855
      Left            =   120
      TabIndex        =   12
      Top             =   120
      Width           =   6375
      Begin VB.CommandButton cmdQuery 
         Caption         =   "查询(&Q)"
         Height          =   375
         Left            =   4560
         TabIndex        =   15
         Top             =   360
         Width           =   1215
      End
      Begin VB.TextBox txtNoInput 
         Height          =   285
         Left            =   2040
         TabIndex        =   14
         Top             =   360
         Width           =   2055
      End
      Begin VB.Label Label7 
         Caption         =   "输入课程编号:"
         Height          =   255
         Left            =   720
         TabIndex        =   13
         Top             =   360
         Width           =   1335
      End
   End
   Begin VB.Frame Frame1 
      Caption         =   "课程基本信息"
      Height          =   1575
      Left            =   120
      TabIndex        =   0
      Top             =   1080
      Width           =   6375
      Begin VB.TextBox txtName 
         Height          =   285
         Left            =   1200
         TabIndex        =   16
         Top             =   360
         Width           =   1815
      End
      Begin VB.TextBox txtScore 
         Height          =   285
         Left            =   4320
         TabIndex        =   5
         Top             =   1080
         Width           =   1815
      End
      Begin VB.ComboBox cboType 
         Height          =   315
         Left            =   1200
         TabIndex        =   4
         Top             =   720
         Width           =   1815
      End
      Begin VB.TextBox txtHours 
         Height          =   285
         Left            =   1200
         TabIndex        =   3
         Top             =   1080
         Width           =   1815
      End
      Begin VB.ComboBox cboTeacher 
         Height          =   315
         Left            =   4320
         TabIndex        =   2
         Top             =   720
         Width           =   1815
      End
      Begin VB.TextBox txtNo 
         Enabled         =   0   'False
         Height          =   285
         Left            =   4320
         TabIndex        =   1
         Top             =   360
         Width           =   1815
      End
      Begin VB.Label Label6 
         Caption         =   "学        分:"
         Height          =   255
         Left            =   3360
         TabIndex        =   11
         Top             =   1080
         Width           =   1095
      End
      Begin VB.Label Label5 
         Caption         =   "学  时  数:"
         Height          =   255
         Left            =   240
         TabIndex        =   10
         Top             =   1080
         Width           =   1095
      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            =   240
         TabIndex        =   8
         Top             =   360
         Width           =   975
      End
      Begin VB.Label Label2 
         Caption         =   "任课教师:"
         Height          =   255
         Left            =   3360
         TabIndex        =   7
         Top             =   720
         Width           =   1095
      End
      Begin VB.Label Label1 
         Caption         =   "课程编号:"
         Height          =   255
         Left            =   3360
         TabIndex        =   6
         Top             =   360
         Width           =   975
      End
   End
End
Attribute VB_Name = "frmUpdateCourse"
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 cmdCancel_Click()
Unload Me

End Sub

Private Sub cmdDelete_Click()
   Dim conn As ADODB.Connection
   
   sqlStr = "delete from courseInfo where course_no='" & txtNo.Text & "'"
   On Error GoTo exitSub
   Set conn = New ADODB.Connection
   conn.Open connStr
   
   conn.Execute sqlStr
   MsgBox "成功删除数据!!"
   initTeacher
   initCourseType
   initTextbox
exitSub:
   
   conn.Close

End Sub

Private Sub cmdQuery_Click()
getCourseInfo
End Sub

Private Sub cmdUpdate_Click()
   Dim conn As ADODB.Connection
   
   sqlStr = "Update courseInfo set course_name='" & txtName.Text _
          & "',course_type='" & cboType.Text & "',teacher='" _
          & cboTeacher.Text & "',course_hours=" & txtHours.Text _
          & ",score=" & txtScore.Text & " where course_no='" & txtNo.Text & "'"
   On Error GoTo exitSub
   Set conn = New ADODB.Connection
   conn.Open connStr
   
   conn.Execute sqlStr
   MsgBox "成功修改数据!!"
   initTeacher
   initCourseType
   initTextbox
exitSub:
   
   conn.Close

End Sub

Private Sub Form_Load()

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

initTeacher
initCourseType
End Sub

Sub initTeacher()
Dim rstTeacher As ADODB.Recordset

    '从数据库中读取所有教师姓名并添加到组合列表框中
    '方便录入时进行选择
    sqlStr = "select name from teacherInfo"
    Set rstTeacher = ExecuteSQL(sqlStr, msgText)
    cboTeacher.Clear
    
    If Not rstTeacher.EOF Then
        
            Do While Not rstTeacher.EOF
                cboTeacher.AddItem Trim(rstTeacher.Fields(0))
                rstTeacher.MoveNext
            Loop
        
    Else
        MsgBox "没有教师信息,请添加!", vbOKOnly + vbExclamation, "警告"
        
        Exit Sub
    End If
    rstTeacher.Close

End Sub

Sub initCourseType()
    cboType.Clear
    cboType.AddItem "选修课"
    cboType.AddItem "必修课"
    
End Sub

Sub getCourseInfo()
Dim rstCourse As ADODB.Recordset

    '从数据库中读取课程信息并添加到窗体中的输入框
    sqlStr = "select * from courseInfo where course_no='" & txtNoInput.Text & "'"
    Set rstCourse = ExecuteSQL(sqlStr, msgText)
    
    If Not rstCourse.EOF Then
        
                txtNo = Trim(rstCourse.Fields("course_no"))
                txtName = Trim(rstCourse.Fields("course_name"))
                cboType.Text = Trim(rstCourse.Fields("course_type"))
                cboTeacher.Text = Trim(rstCourse.Fields("teacher"))
                txtHours.Text = Trim(rstCourse.Fields("course_hours"))
                txtScore.Text = Trim(rstCourse.Fields("score"))
    Else
        MsgBox "没找到符合条件的数据!", vbOKOnly + vbExclamation, "警告"
        
        Exit Sub
    End If
    rstCourse.Close

End Sub

Sub initTextbox()
txtName = ""
txtNo = ""
txtHours = ""
txtScore = ""
End Sub

⌨️ 快捷键说明

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