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

📄 学院管理.frm

📁 初学vb,做的第一个系统.一个星期内看vb并做完这个系统的.
💻 FRM
字号:
VERSION 5.00
Object = "{67397AA1-7FB1-11D0-B148-00A0C922E820}#6.0#0"; "MSADODC.OCX"
Begin VB.Form 学院管理 
   BorderStyle     =   1  'Fixed Single
   Caption         =   "学院管理"
   ClientHeight    =   2985
   ClientLeft      =   4245
   ClientTop       =   4005
   ClientWidth     =   6300
   LinkTopic       =   "Form1"
   MaxButton       =   0   'False
   ScaleHeight     =   2985
   ScaleWidth      =   6300
   Begin MSAdodcLib.Adodc Adodc1 
      Height          =   330
      Left            =   240
      Top             =   2880
      Visible         =   0   'False
      Width           =   2415
      _ExtentX        =   4260
      _ExtentY        =   582
      ConnectMode     =   0
      CursorLocation  =   3
      IsolationLevel  =   -1
      ConnectionTimeout=   15
      CommandTimeout  =   30
      CursorType      =   3
      LockType        =   3
      CommandType     =   8
      CursorOptions   =   0
      CacheSize       =   50
      MaxRecords      =   0
      BOFAction       =   0
      EOFAction       =   0
      ConnectStringType=   1
      Appearance      =   1
      BackColor       =   -2147483643
      ForeColor       =   -2147483640
      Orientation     =   0
      Enabled         =   -1
      Connect         =   "Provider=MSDASQL.1;Persist Security Info=False;Data Source=stu_manage"
      OLEDBString     =   "Provider=MSDASQL.1;Persist Security Info=False;Data Source=stu_manage"
      OLEDBFile       =   ""
      DataSourceName  =   ""
      OtherAttributes =   ""
      UserName        =   "sa"
      Password        =   "manager"
      RecordSource    =   ""
      Caption         =   "Adodc1"
      BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851} 
         Name            =   "宋体"
         Size            =   9
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      _Version        =   393216
   End
   Begin VB.CommandButton Command2 
      Caption         =   "删除"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   10.5
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   4560
      TabIndex        =   5
      Top             =   2280
      Width           =   975
   End
   Begin VB.CommandButton Command1 
      Caption         =   "添加"
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   10.5
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   1320
      TabIndex        =   3
      Top             =   2280
      Width           =   975
   End
   Begin VB.ComboBox Combo1 
      Height          =   300
      Left            =   3840
      Style           =   2  'Dropdown List
      TabIndex        =   4
      Top             =   960
      Width           =   2295
   End
   Begin VB.TextBox nametxt 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   10.5
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   1080
      TabIndex        =   2
      Top             =   1560
      Width           =   2295
   End
   Begin VB.TextBox idtxt 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   10.5
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   375
      Left            =   1080
      TabIndex        =   1
      Top             =   960
      Width           =   2295
   End
   Begin VB.Line Line1 
      X1              =   3600
      X2              =   3600
      Y1              =   480
      Y2              =   2760
   End
   Begin VB.Label labely 
      Height          =   495
      Left            =   3960
      TabIndex        =   9
      Top             =   1440
      Width           =   2175
   End
   Begin VB.Label Label4 
      Caption         =   "请选择要删除的学院名称"
      Height          =   255
      Left            =   3840
      TabIndex        =   8
      Top             =   600
      Width           =   2055
   End
   Begin VB.Label Label3 
      Caption         =   "学院名称"
      Height          =   255
      Left            =   240
      TabIndex        =   7
      Top             =   1680
      Width           =   735
   End
   Begin VB.Label Label2 
      Caption         =   "学院编号"
      Height          =   255
      Left            =   240
      TabIndex        =   6
      Top             =   1080
      Width           =   975
   End
   Begin VB.Label lablex 
      BeginProperty Font 
         Name            =   "宋体"
         Size            =   12
         Charset         =   134
         Weight          =   400
         Underline       =   0   'False
         Italic          =   0   'False
         Strikethrough   =   0   'False
      EndProperty
      Height          =   255
      Left            =   1200
      TabIndex        =   0
      Top             =   240
      Width           =   3375
   End
End
Attribute VB_Name = "学院管理"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit

    Public rs As New ADODB.Recordset
    Public adoconn As New ADODB.Connection 'Connection 对象代表了打开与数据源的连接。

    Public co_count As Integer
    Public co_name As String

Public Sub connect()
    adoconn.ConnectionString = Adodc1.ConnectionString 'Adodc1为窗体中的ADO控件,并已成功连接数据库
    Set rs = New ADODB.Recordset
    rs.CursorLocation = adUseClient
    rs.CursorType = adOpenKeyset
    rs.LockType = adLockOptimistic
End Sub

Private Sub Combo1_click()
    co_name = Combo1.Text
    labely.Caption = "  您选择的是" & co_name
End Sub

'添加
Private Sub Command1_Click()
    adoconn.Open

    '从文本输入框获取数据的变量声明
    Dim id As String
    Dim name As String
    
    '获取用户输入值
    id = idtxt.Text
    name = nametxt.Text
    
    '检验是否为空或编号是否为字母
    If Len(id) = 0 Then
        MsgBox "学院编号不能为空且不能为字母!"
        idtxt.Text = ""
        idtxt.SetFocus
        adoconn.Close
        Exit Sub
    ElseIf Len(name) = 0 Then
        MsgBox "学院名称不能为空!"
        nametxt.Text = ""
        nametxt.SetFocus
        adoconn.Close
        Exit Sub
    End If
    
    Dim i As Integer
    '数据库中没有用户信息时
    rs.Open "select * from college", adoconn
    If rs.RecordCount = 0 Then
        rs.AddNew
        rs.Fields("id") = id
        rs.Fields("name") = name
        rs.Update
        rs.Close
        idtxt.Text = ""
        nametxt.Text = ""
        MsgBox "添加学院信息成功"
        adoconn.Close
        Call load
    Else
        '数据库中至少存在一个用户信息,判断新输入的用户是否已经存在
        For i = 0 To rs.RecordCount - 1
            If id = rs.Fields("id") Then
                MsgBox "该编号已经添加!"
                idtxt.Text = ""
                nametxt.Text = ""
                idtxt.SetFocus
                rs.Close
                adoconn.Close
                Exit Sub
            End If
            rs.MoveNext
        Next i
        
        '新输入的用户名在数据库中找不到,且输入不为空值,则添加进数据库
        rs.AddNew
        rs.Fields("id") = id
        rs.Fields("name") = name
        rs.Update
        rs.Close
        MsgBox "添加学院信息成功"
        adoconn.Close
        Call load
        idtxt.Text = ""
        nametxt.Text = ""
        idtxt.SetFocus
    End If
End Sub

Private Sub combo()
    Combo1.Clear
    
    rs.Open "select * from college", adoconn

    Dim i As Integer
    If rs.RecordCount = 0 Then
        rs.Close
    Else
        rs.MoveFirst
        For i = 0 To rs.RecordCount - 1
            Combo1.AddItem rs.Fields("name")
            rs.MoveNext
        Next i
        rs.Close
    End If
End Sub

'删除
Private Sub Command2_Click()
    adoconn.Open
    If Len(co_name) = 0 Then
        MsgBox "请先选择你要删除的学院名称!"
        Exit Sub
    End If
    
    rs.Open "select * from stu,college where stu.college=college.id and college.name='" & co_name & "'", adoconn
    If rs.RecordCount <> 0 Then
        MsgBox "该学院已经存在学生,不允许删除!若要继续,请先删除该院学生信息!"
        rs.Close
        adoconn.Close
        Combo1.SetFocus
        Exit Sub
    End If
    rs.Close
 
    '确认对话框:有“是”与“否”两种选择
    Dim r As Integer
    r = MsgBox("您确认要删除该学院吗?", 4 + 32 + 0, "删除学院")
    '选择是
    If r = 6 Then
        rs.Open "select * from major,college where major.college=college.id and college.name='" & co_name & "'", adoconn
        If rs.RecordCount <> 0 Then
            MsgBox "该学院已设相关专业,不允许删除!"
            rs.Close
            adoconn.Close
            Exit Sub
        End If
        rs.Close
        
        rs.Open "delete from college where name='" & co_name & "'", adoconn
        co_name = ""
        labely.Caption = ""
        MsgBox "删除学院成功!"

        adoconn.Close
        Call load
    '选择否
    ElseIf r = 7 Then
        co_name = ""
        labely.Caption = ""
        adoconn.Close
        Call load
        Exit Sub
    End If

End Sub

Private Sub Form_Load()
    Call connect
    Call load
End Sub


Private Sub load()
    adoconn.Open
    rs.Open "select * from college", adoconn
    
    co_count = rs.RecordCount
    rs.Close
    Call combo
    lablex.Caption = "目前有" & co_count & "个学院"

    adoconn.Close
End Sub

⌨️ 快捷键说明

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