智能排课系统vb代码 - 图文

更新时间:2024-06-16 07:10:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

Vb核心代码设计

一:输入界面与代码(以班级表为例)

Dim Conn As New ADODB.Connection '连接变量 Dim SqlString As String '查询字符串 Dim Connstring As String '连接字符串 Dim RS1 As New ADODB.Recordset '记录集变量

Dim bm '定义一个变量用来存放添加记录时原来记录的位置 Dim tj As Boolean

Private Sub Command10_Click() Form4.Hide Form3.Show End Sub

Private Sub Command1_Click() '移到首记录 RS1.MoveFirst

Command1.Enabled = False Command2.Enabled = False Command3.Enabled = True Command4.Enabled = True

Call DispValue '显示当前记录的值 End Sub

Private Sub Command2_Click() '前移 RS1.MovePrevious

If RS1.BOF Then RS1.MoveFirst

Command1.Enabled = False Command2.Enabled = False End If

Command3.Enabled = True Command4.Enabled = True

Call DispValue '显示当前记录的值 End Sub

Private Sub Command3_Click() '后移 RS1.MoveNext If RS1.EOF Then RS1.MoveLast

Command3.Enabled = False Command4.Enabled = False End If

Command1.Enabled = True Command2.Enabled = True

Call DispValue '显示当前记录的值 End Sub

Private Sub Command4_Click() '移到末记录 RS1.MoveLast

Command3.Enabled = False Command4.Enabled = False Command1.Enabled = True Command2.Enabled = True

Call DispValue '显示当前记录的值 End Sub

Private Sub Command5_Click() '删除记录

If (MsgBox(\真的要删除该记录吗?\删除提示\ RS1.Delete '删除 RS1.MoveNext

If RS1.EOF Then '如果删除的是末记录 RS1.MoveLast End If End If

Call DispValue '显示当前记录的值 End Sub

Private Sub Command6_Click() '添加记录

bm = RS1.Bookmark '用书签记下当前记录位置

RS1.AddNew '调用AddNew方法增加新记录 Call EditStatus '置按钮和文本框状态 Call EmptyText '清空文本框 tj = True '是添加新记录 End Sub

Private Sub Command7_Click() '修改记录 Call EditStatus '置按钮和文本框状态 tj = False '不是添加记录,是修改记录 End Sub

Private Sub Command8_Click() '确定操作 Call SetValue '给各字段赋值

RS1.Update '调用Update方法保存 Call OkStatus '置按钮和文本框状态

MsgBox (\保存成功\给出保存成功提示 End Sub

Private Sub Command9_Click() '取消操作

If (MsgBox(\真的要取消修改吗?\ RS1.CancelUpdate

If tj Then '如果是增加新记录

RS1.Bookmark = bm '把记录指针移动到原来的位置 End If

Call DispValue '显示当前记录的值 Call OkStatus '置按钮和文本框状态 End If End Sub

Private Sub Form_Load()

Connstring = \

& \ 'SQL Server普通安全模式建立到Pubs数据库的连接的连接字符串

'设置连接字符串 With Conn

.ConnectionString = Connstring '给ConnectionString属性赋值 .ConnectionTimeout = 10

.Open '给连接的ConnectionString属性赋值,然后使用求带参数的Open方法打开连接 End With

MsgBox (\连接成功\

RS1.CursorType = adOpenDynamic

RS1.CursorLocation = adUseClient '游标服务的位置、默认值adUseServer。游标用于:控制记录定位、控制其他用户对数据库所作的更改的可视性、控制数据可更新性 RS1.LockType = adLockOptimistic RS1.Open \班级信息表\

If RS1.EOF And RS1.BOF Then '如果表为空 MsgBox \表为空,请添加记录\ Else

Call DispValue '显示当前记录的值 End If

Call TextDisable '使文本框不可用 Command8.Enabled = False Command9.Enabled = False End Sub

Private Sub DispValue()

Text1.Text = RS1.Fields(\班级编号\

If IsNull(Trim(RS1.Fields(\班级名称\ '解决课程名字段值为空移指针出错(无效使用NULL)的问题,其他字段类推。 Text2.Text = \ Else

Text2.Text = RS1.Fields(\班级名称\ End If

Text5.Text = RS1.Fields(\所属系\

Text7.Text = RS1.Fields(\学生人数\End Sub

Private Sub SetValue()

RS1.Fields(\班级编号\ RS1.Fields(\班级名称\

RS1.Fields(\所属系称\

RS1.Fields(\学生人数\End Sub

Private Sub TextEnable() '使所有的文本框可用 Text1.Enabled = True Text2.Enabled = True Text5.Enabled = True Text7.Enabled = True End Sub

Private Sub TextDisable() '使所有的文本框不可用 Text1.Enabled = False Text2.Enabled = False Text5.Enabled = False Text7.Enabled = False End Sub

Private Sub EditStatus() '设置在编辑或添加记录时的各控件状态 Command1.Enabled = False

Command2.Enabled = False Command3.Enabled = False Command4.Enabled = False Command5.Enabled = False Command6.Enabled = False Command7.Enabled = False Command8.Enabled = True Command9.Enabled = True Call TextEnable End Sub

Private Sub EmptyText() '清空文本框 Text1.Text = \ Text2.Text = \ Text5.Text = \ Text7.Text = \End Sub

Private Sub OkStatus() '设置不在编辑或添加记录时的各控件状态 Command1.Enabled = True Command2.Enabled = True Command3.Enabled = True Command4.Enabled = True Command5.Enabled = True Command6.Enabled = True Command7.Enabled = True Command8.Enabled = False Command9.Enabled = False Call TextDisable End Sub

2、查看课表窗口及代码(以教室课表为例)

Dim Conn As New ADODB.Connection '连接变量

Dim SqlString As String '查询字符串 Dim Connstring As String '连接字符串 Dim RS1 As New ADODB.Recordset '记录集变量 Dim Cmd As New ADODB.Command Private Sub Command1_Click()

Connstring = \

& \ 'SQL Server普通安全模式建立到Pubs数据库的连接的连接字符串

'设置连接字符串 With Conn

.ConnectionString = Connstring '给ConnectionString属性赋值 .ConnectionTimeout = 10

.Open '给连接的ConnectionString属性赋值,然后使用求带参数的Open方法打开连接 End With

MsgBox (\连接成功\

Command1.Enabled = False '连接按钮不可用 Command2.Enabled = True '显示数据可用 End Sub

Private Sub Command2_Click()

Set Cmd.ActiveConnection = Conn '设置Command对象的ActiveConnection为某个打开的连接

SqlString = \课程名称,教师姓名,上课教室,班级编号,上课时间 from 排课表 Where 上课教室 like '\ '形成查询字符串1

Cmd.CommandText = SqlString '设置Command对象执行的命令字符串

Cmd.CommandType = adCmdText '设置命令类型,表示CommandText中为一命令文本 ,童P188

Set RS1 = Cmd.Execute '执行命令得到一个结果记录集 Call ShowData(RS1, MSFlexGrid1) '调用过程显示记录集中的数据 Command1.Visible = True End Sub

Private Sub ShowData(Rs As ADODB.Recordset, Dgrid As MSFlexGrid) '该过程用来在Dgrid网格中显示记录集RS中的内容 Dim RowNum As Integer

RowNum = 1 '代表MsFlexGrid控件的行数 Dgrid.Rows = RowNum '设置MsFlexGrid控件的行数 Dgrid.Cols = Rs.Fields.Count '设置MsFlexGrid控件的列数 For j = 0 To Rs.Fields.Count - 1 '遍列所有列

Dgrid.TextMatrix(0, j) = Rs.Fields(j).Name '把列名显示在设置MsFlexGrid控件的首行

Next j

Do While Not Rs.EOF '如果不是记录集的结尾 RowNum = RowNum + 1 '行数加1

Dgrid.Rows = RowNum '设置MsFlexGrid控件的行数 For j = 0 To Rs.Fields.Count - 1 '遍列所有列

If Not IsNull(Rs.Fields(j).Value) Then '如果列中的数据不为空,切记不能用如 if Var1=Null来判断变量Var1的值是否为Null

Dgrid.TextMatrix(RowNum - 1, j) = Rs.Fields(j).Value '把列的数据显示在MsFlexGrid的相应网格中 End If Next j

Rs.MoveNext '记录指针下移 Loop

Command2.Enabled = False End Sub

Private Sub Command3_Click() Form14.Hide Form3.Show End Sub

Private Sub Command4_Click() RS1.Close '关闭记录集 Conn.Close '关闭连接 Text1.Text = \

Command1.Enabled = True '连接按钮不可用 Command2.Enabled = True '显示数据可用 End Sub

Private Sub Command5_Click() DataReport1.Show vbModal, Me End Sub

3、查看初始化窗口及代码

Option Explicit

Dim SQL As String

Dim Conn As New ADODB.Connection '连接变量

Dim SqlString As String '查询字符串 Dim Connstring As String

Dim msg As String '连接字符串 Private Sub CmdSave_Click() '进行初始化操作

Dim Counnter As Integer Connstring = \

Server};Server=PC2012110412ewr.;Database=GXPK;\

& \ 'SQL Server普通安全模式建立到Pubs数据库的

连接的连接字符串

'设置连接字符串 With Conn

.ConnectionString = Connstring '给ConnectionString属性赋值 .ConnectionTimeout = 10

.Open '给连接的ConnectionString属性赋值,然后使用求带参数的Open

方法打开连接

End With

MsgBox (\连接成功\

ProgressBar1.Visible = True ProgressBar1.Max = 200

ProgressBar1.Value = ProgressBar1.Min

msg = MsgBox(\您确实要初始化这些数据吗?\ If msg = vbNo Then Exit Sub

For Counnter = ProgressBar1.Min To ProgressBar1.Max ProgressBar1.Value = Counnter

If Check1.Value = 1 Then '如果班级信息表被选中 '删除全部提醒信息数据

SQL = \班级信息表\ Conn.Execute SQL End If

If Check2.Value = 1 Then '如果课程信息表被选中 '删除全部通讯日志数据

SQL = \课程信息表\ Conn.Execute SQL End If

If Check3.Value = 1 Then '如果班级课程信息表被选中 '删除全部群组信息数据

SQL = \任课表\ Conn.Execute SQL End If

If Check4.Value = 1 Then '如果教师信息表被选中 '删除全部联系人信息数据

SQL = \教师信息表\

Conn.Execute SQL End If

If Check6.Value = 1 Then '如果排课信息表被选中 '删除全部联系人信息数据

SQL = \排课表\ Conn.Execute SQL End If Next Counnter

ProgressBar1.Visible = False

ProgressBar1.Value = ProgressBar1.Min End Sub

Private Sub CmdExit_Click() '退出操作

Form15.Hide Form3.Show End Sub

Private Sub Form_Unload(Cancel As Integer) '退出操作

Form15.Hide Form3.Show End Sub

4、查看自动排课窗口及代码

Dim Conn As New ADODB.Connection '连接变量 Dim SqlString As String '查询字符串 Dim Connstring As String Dim dlaytime As Integer

Private Sub Command2_Click() Dim SQL As String

Dim sc As ADODB.Recordset Set sc = New ADODB.Recordset SQL = \排课表 \End Sub

Private Sub Command3_Click() Form9.Hide Form3.Show End Sub

Private Sub Form_Load()

constr = \

& \ 'SQL Server普通安全模式建立到Pubs数据库的连接的连接字符串 '定义数据库连接 With Conn

.ConnectionString = constr '给ConnectionString属性赋值 .ConnectionTimeout = 10

.Open '给连接的ConnectionString属性赋值,然后使用求带参数的Open方法打开连接 End With

MsgBox (\连接成功\

ProgressBar1.Visible = True ProgressBar1.Max = 200

ProgressBar1.Value = ProgressBar1.Min End Sub

Private Sub Command1_Click() '开始 Dim SQL, sqll, sqlll As String

Dim rt As New ADODB.Recordset Dim rm As New ADODB.Recordset Dim rn As New ADODB.Recordset Dim rp As New ADODB.Recordset Dim rq As New ADODB.Recordset Dim i As Integer

Set Rs = New ADODB.Recordset SQL = \任课表 \Set Rs = Conn.Execute(SQL) ProgressBar1.Max = 100

Do While ProgressBar1.Value < ProgressBar1.Max Do Until Rs.EOF = True i = Rs(\每周节数\ Do Until i = 0

sqll = \时间表\ Set rt = Conn.Execute(sqll) Do Until rt.EOF = True

Set rp = New ADODB.Recordset

coom = \排课表 where [班级编号]='\班级编号\上课时间]='\上课时间\

Set rp = Conn.Execute(coom)

cool = \* from 排课表 where [上课时间]='\+ rt(\上课时间\+ \[教师姓名]='\教师姓名\

Set rq = Conn.Execute(cool)

If rp.EOF = True And rq.EOF = True Then sqlll = \教室信息表\ Set rm = Conn.Execute(sqlll) Do Until rm.EOF = True

coon = \排课表 where [上课时间]='\上课时间\上课教室]='\教室名称\

Set rn = Conn.Execute(coon) If rn.EOF = True Then

SQL = \into 排课表 ([课程名称],[教师姓名],[上课教室],[班级编号],[上课时间]) values ('\课程名称\教师姓名\教室名称\Rs(\班级编号\上课时间\ Set rq = Conn.Execute(SQL) Exit Do Else

rm.MoveNext End If Loop Exit Do Else

rt.MoveNext End If Loop i = i - 1 Loop Rs.MoveNext Loop

ProgressBar1.Value = ProgressBar1.Value + 1 DoEvents Loop

MsgBox \自动排课已完成\提示\End Sub

Private Sub Timer1_Timer() If dlaytime >= 100 Then 'Timer1.Enabled = False Load frmLogin frmLogin.Show Else

dlaytime = dlaytime + 1

P1.Value = dlaytime End If End Sub

5、查看手动调课窗口及代码

Dim Conn As New ADODB.Connection '连接变量 Dim SqlString As String '查询字符串 Dim Connstring As String

Private Sub Command2_Click() Form11.Hide Form3.Show End Sub

Private Sub Command5_Click()

Dim Conn As New ADODB.Connection '连接变量 Dim SqlString As String '查询字符串 Dim Connstring As String '连接字符串 Dim RS1 As New ADODB.Recordset '记录集变量 Dim Cmd As New ADODB.Command

Connstring = \

& \ 'SQL Server普通安全模式建立到Pubs数据库的连接的连接字符串

'设置连接字符串 With Conn

.ConnectionString = Connstring '给ConnectionString属性赋值 .ConnectionTimeout = 10

.Open '给连接的ConnectionString属性赋值,然后使用求带参数的Open方法打开连接 End With

Set Cmd.ActiveConnection = Conn '设置Command对象的ActiveConnection为某个打开

的连接

SqlString = \课程名称,教师姓名,上课教室,班级编号,上课时间 from 排课表 \ '形成查询字符串1

Cmd.CommandText = SqlString '设置Command对象执行的命令字符串

Cmd.CommandType = adCmdText '设置命令类型,表示CommandText中为一命令文本 ,童P188

Set RS1 = Cmd.Execute '执行命令得到一个结果记录集 Call ShowData(RS1, MSFlexGrid1) '调用过程显示记录集中的数据 End Sub

Private Sub Form_Load()

constr = \

& \ 'SQL Server普通安全模式建立到Pubs数据库的连接的连接字符串 '定义数据库连接 With Conn

.ConnectionString = constr '给ConnectionString属性赋值 .ConnectionTimeout = 10

.Open '给连接的ConnectionString属性赋值,然后使用求带参数的Open方法打开连接 End With

MsgBox (\连接成功\End Sub

Private Sub Command1_Click() '调整教室 Dim rt As New ADODB.Recordset

If (Text1.Text = \Then

MsgBox (\请输入完整信息\Else

Set Rs = New ADODB.Recordset SQL = \排课表\Set Rs = Conn.Execute(SQL)

If Rs(\上课教室\

MsgBox (\该时间段该教室已被占用,请重新选择\ Text4.Text = \ Text5.Text = \Else

Set Rs = New ADODB.Recordset

SQL = \排课表 set [上课教室]='\课程名称]='\\教师姓名]='\上课时间]='\Set Rs = Conn.Execute(SQL) MsgBox (\教室调整成功\ Text1.Text = \ Text2.Text = \ Text3.Text = \ Text4.Text = \ Text5.Text = \

End If End If End Sub

Private Sub Command3_Click() '调整时间 Dim rt As New ADODB.Recordset

If (Text1.Text = \Then

MsgBox (\请输入完整信息\Else

Set Rs = New ADODB.Recordset SQL = \排课表\Set Rs = Conn.Execute(SQL)

If Rs(\上课时间\

MsgBox (\该教室该时间段已被占用,请重新选择\ Text4.Text = \ Text5.Text = \ Else

Set Rs = New ADODB.Recordset

SQL = \排课表 set [上课时间]='\课程名称]='\+ \教师姓名]='\教室名称]='\ Set Rs = Conn.Execute(SQL) MsgBox (\时间调整成功\ 'Adodc1.Refresh Text1.Text = \ Text2.Text = \ Text3.Text = \ Text4.Text = \ Text5.Text = \End If End If End Sub

Private Sub ShowData(Rs As ADODB.Recordset, Dgrid As MSFlexGrid) '该过程用来在Dgrid网格中显示记录集RS中的内容 Dim RowNum As Integer

RowNum = 1 '代表MsFlexGrid控件的行数 Dgrid.Rows = RowNum '设置MsFlexGrid控件的行数 Dgrid.Cols = Rs.Fields.Count '设置MsFlexGrid控件的列数 For j = 0 To Rs.Fields.Count - 1 '遍列所有列

Dgrid.TextMatrix(0, j) = Rs.Fields(j).Name '把列名显示在设置MsFlexGrid控件的首行

Next j

Do While Not Rs.EOF '如果不是记录集的结尾 RowNum = RowNum + 1 '行数加1

Dgrid.Rows = RowNum '设置MsFlexGrid控件的行数

For j = 0 To Rs.Fields.Count - 1 '遍列所有列

If Not IsNull(Rs.Fields(j).Value) Then '如果列中的数据不为空,切记不能用如 if Var1=Null来判断变量Var1的值是否为Null

Dgrid.TextMatrix(RowNum - 1, j) = Rs.Fields(j).Value '把列的数据显示在MsFlexGrid的相应网格中 End If Next j

Rs.MoveNext '记录指针下移 Loop End Sub

本文来源:https://www.bwwdw.com/article/6oz3.html

Top