📄 专业管理.frm
字号:
VERSION 5.00
Object = "{67397AA1-7FB1-11D0-B148-00A0C922E820}#6.0#0"; "MSADODC.OCX"
Begin VB.Form 专业管理
BorderStyle = 1 'Fixed Single
Caption = "专业管理"
ClientHeight = 3900
ClientLeft = 4245
ClientTop = 4005
ClientWidth = 6300
LinkTopic = "Form1"
MaxButton = 0 'False
ScaleHeight = 3900
ScaleWidth = 6300
Begin VB.ComboBox Combo2
Height = 300
Left = 2280
Style = 2 'Dropdown List
TabIndex = 0
Top = 240
Width = 2415
End
Begin MSAdodcLib.Adodc Adodc1
Height = 330
Left = 240
Top = 3600
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 = 3000
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 = 3000
Width = 975
End
Begin VB.ComboBox Combo1
Height = 300
Left = 3840
Style = 2 'Dropdown List
TabIndex = 4
Top = 1680
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 = 2280
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 = 1680
Width = 2295
End
Begin VB.Label Label1
Caption = "请先选择专业所在学院"
Height = 255
Left = 240
TabIndex = 11
Top = 240
Width = 1815
End
Begin VB.Line Line1
X1 = 3600
X2 = 3600
Y1 = 1200
Y2 = 3480
End
Begin VB.Label labely
Height = 495
Left = 3960
TabIndex = 10
Top = 2160
Width = 2175
End
Begin VB.Label Label4
Caption = "请选择要删除的专业名称"
Height = 255
Left = 3840
TabIndex = 9
Top = 1320
Width = 2055
End
Begin VB.Label Label3
Caption = "专业名称"
Height = 255
Left = 240
TabIndex = 8
Top = 2400
Width = 975
End
Begin VB.Label Label2
Caption = "专业编号"
Height = 255
Left = 240
TabIndex = 7
Top = 1800
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 = 120
TabIndex = 6
Top = 720
Width = 6015
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 major_count As Integer '专业个数
Public major_name As String '专业名称
Public college_name As String '学院名称
Public college_id As Integer '学院编号
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()
major_name = Combo1.Text
labely.Caption = " 您选择的是" & major_name
End Sub
'选择学院,选择后显示该学院的专业个数
Private Sub Combo2_click()
Dim i As Integer
labely.Caption = ""
adoconn.Open
'获取学院名称
college_name = Combo2.Text
'获取学院编号
rs.Open "select * from college", adoconn
rs.MoveFirst
For i = 0 To rs.RecordCount - 1
If college_name = rs.Fields("name") Then
college_id = rs.Fields("id")
Exit For
End If
rs.MoveNext
Next i
rs.Close
'获取该学院的专业个数
rs.Open "select * from major where college='" & college_id & "'", adoconn
If rs.RecordCount = 0 Then
rs.Close
lablex.Caption = college_name & "目前没有输入任何专业,请添加!"
Else
lablex.Caption = college_name & "目前有" & rs.RecordCount & "个专业。"
End If
adoconn.Close
'加载专业列表
Call major_combo
End Sub
'添加
Private Sub Command1_Click()
adoconn.Open
'从文本输入框获取数据的变量声明
Dim id As String
Dim name As String
'获取用户输入值
id = idtxt.Text
name = nametxt.Text
'检验是否为空或编号是否为字母
If Len(college_name) = 0 Then
MsgBox "请先选择学院!"
Combo2.Text = ""
adoconn.Close
Combo2.SetFocus
Exit Sub
ElseIf 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
rs.MoveFirst
For i = 0 To rs.RecordCount - 1
If college_name = rs.Fields("name") Then
college_id = rs.Fields("id")
rs.Close
Exit For
End If
rs.MoveNext
Next i
'数据库中没有用户信息时
rs.Open "select * from major", adoconn
If rs.RecordCount = 0 Then
rs.AddNew
rs.Fields("id") = id
rs.Fields("name") = name
rs.Fields("college") = college_id
rs.Update
rs.Close
idtxt.Text = ""
nametxt.Text = ""
MsgBox "添加专业信息成功"
adoconn.Close
lablex.Caption = ""
Combo2.Text = college_name
Call combo
Else
'数据库中至少存在一个用户信息,判断新输入的用户是否已经存在
For i = 0 To rs.RecordCount - 1
If id = rs.Fields("id") And college_id = rs.Fields("college") 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.Fields("college") = college_id
rs.Update
rs.Close
MsgBox "添加专业信息成功"
lablex.Caption = ""
adoconn.Close
Call combo
idtxt.Text = ""
nametxt.Text = ""
idtxt.SetFocus
Combo2.Text = college_name
End If
End Sub
'删除
Private Sub Command2_Click()
If Len(major_name) = 0 Then
MsgBox "请先选择你要删除的专业名称!"
Exit Sub
End If
adoconn.Open
'确认对话框:有“是”与“否”两种选择
Dim r As Integer
r = MsgBox("您确认要删除该专业吗?", 4 + 32 + 0, "删除专业")
'选择是
If r = 6 Then
rs.Open "select * from stu,major where major.id=stu.major and major.name='" & major_name & "'", adoconn
If rs.RecordCount <> 0 Then
MsgBox "存在该专业的学生,不允许删除!"
rs.Close
adoconn.Close
Exit Sub
End If
rs.Close
rs.Open "delete from major where name='" & major_name & "'and college='" & college_id & "'", adoconn
major_name = ""
labely.Caption = ""
MsgBox "删除专业成功!"
adoconn.Close
lablex.Caption = ""
Combo2.Text = college_name
Combo1.SetFocus
Call major_combo
Exit Sub
End If
labely.Caption = ""
Combo1.SetFocus
adoconn.Close
End Sub
Private Sub Form_Load()
Call connect
Call combo
End Sub
'加载“学院”列表,
Private Sub combo()
adoconn.Open
Combo1.Clear
Combo2.Clear
Dim i As Integer
rs.Open "select * from college", adoconn
If rs.RecordCount = 0 Then
MsgBox "目前没有任何学院,请先用学院管理添加学院!"
rs.Close
adoconn.Close
End
Else
rs.MoveFirst
For i = 0 To rs.RecordCount - 1
Combo2.AddItem rs.Fields("name")
rs.MoveNext
Next i
rs.Close
End If
adoconn.Close
End Sub
'加载专业列表
Public Sub major_combo()
adoconn.Open
Combo1.Clear
Dim i As Integer
'获取对应专业个数
rs.Open "select * from major", adoconn
If rs.RecordCount = 0 Then
rs.Close
Else
rs.MoveFirst
For i = 0 To rs.RecordCount - 1
If college_id = rs.Fields("college") Then
Combo1.AddItem rs.Fields("name")
End If
rs.MoveNext
Next i
rs.Close
End If
adoconn.Close
End Sub
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -