套号设置是一个基础的设置,要想添加新的一份试卷,首先我们该使用该功能来添加一个套的信息,我们设计了如下的界面: http://www.paper51.com
paper51.com
在文本框里输入套号名称后确定就可以在数据库里添加该套号了,添加成功后,系统会自动转入下一个添加状态,也就是可以接着再添加套号。这里的代码实现如下: 内容来自www.paper51.com 其中确定按钮的代码如下: 内容来自论文无忧网 www.paper51.com Private SubCommand1_Click() paper51.com Dim rst AsRecordset paper51.com
On Error GoToProc_Exit paper51.com
If Len(Text1) > 0 Then 内容来自论文无忧网 www.paper51.com Setrst = New Recordset copyright paper51.com rst.Open "select * from 套号", pCN,adOpenStatic, adLockBatchOptimistic paper51.com rst.AddNew copyright paper51.com rst![套号名称] = Text1 copyright paper51.com
rst.UpdateBatch 内容来自www.paper51.com
Text1 = "" http://www.paper51.com End If http://www.paper51.com Proc_Exit: http://www.paper51.com If Err.Number <> 0Then 内容来自www.paper51.com IfErr.Number = -2147467259 Then 内容来自www.paper51.com MsgBox "套号名称不能重复!", vbInformation copyright paper51.com Else 内容来自论文无忧网 www.paper51.com MsgBox "发生意外错误,错误号:"& Err.Number & " 错误描述:" &Err.Description, vbInformation http://www.paper51.com EndIf http://www.paper51.com Err.Clear copyright paper51.com
End If 内容来自www.paper51.com
End Sub 内容来自论文无忧网 www.paper51.com 在本段代码里,我就运用了错误处理的代码。由于套号名称是唯一的,所以当用户添加同样的套号名称的时候,就会发生错误,从上面的代码我们知道,这时候的错误代码就是-2147467259。我们捕获了该错误,然后对用户进行提示。最后把该错误清除了。而发生其他的错误时候,我们就把错误给显示出来,这样非常的有利于我们来维护使用我们的系统。 paper51.com
5.4 题库内容维护和备选答案维护 paper51.com 在前面的设计的时候我们已经知道饿了题库内容和备选答案正好是主表和子表的关系,所以有必要把这几个模块一起处理的。所以我设计了一个典型的主表和子表的浏览界面如下: http://www.paper51.com paper51.com 上面的表格就是题目的内容部分,下面的表格就是该道试题的备选答案的内容。而上面表格列出是哪套试题,取决于在组合框里选择的是哪一套试题了。 内容来自www.paper51.com 我们来了解一下这里的代码实现了,为了充分发挥VB的RAD开发工具的优势,我们这里采取了数据绑定来显示数据和更新数据,整个代码就显的非常的简单了: 内容来自www.paper51.com 首先我们在窗体加载的时候,我们要加载界面的显示内容,我们可以在Form的Load事件里进行这些初始化工作的。比如加载题库套号数据,然后加载该套试题的数据。 内容来自www.paper51.com Private SubForm_Load() 内容来自www.paper51.com Call InitSubjectSet 内容来自www.paper51.com Call InitSubject paper51.com
End Sub 内容来自论文无忧网 www.paper51.com '' 初始化组合框里的数据 http://www.paper51.com Private SubInitSubjectSet() 内容来自www.paper51.com Dim rst AsRecordset 内容来自www.paper51.com
Dim i As Integer copyright paper51.com Set rst = New Recordset 内容来自www.paper51.com
rst.LockType = adLockBatchOptimistic http://www.paper51.com
rst.CursorLocation =adUseClient http://www.paper51.com rst.CursorType =adOpenStatic paper51.com rst.ActiveConnection = pCN copyright paper51.com
rst.Source = "select *from 套号" copyright paper51.com rst.Open 内容来自www.paper51.com Me.Combo1.Clear http://www.paper51.com For i = 1 To rst.RecordCount 内容来自www.paper51.com Me.Combo1.AddItem rst![套号名称] copyright paper51.com Me.Combo1.ItemData(Me.Combo1.ListCount - 1) = rst![ID] copyright paper51.com rst.MoveNext http://www.paper51.com Next i copyright paper51.com If Combo1.ListCount > 0Then paper51.com Combo1.ListIndex = 0 http://www.paper51.com End If copyright paper51.com Set rst = Nothing 内容来自论文无忧网 www.paper51.com
End Sub 内容来自论文无忧网 www.paper51.com
根据选中的套号从数据库里检索出数据,并且在题目内容的表格里进行显示,代码如下: copyright paper51.com Private SubInitSubject() 内容来自论文无忧网 www.paper51.com
Me.Adodc1.ConnectionString =pCN.ConnectionString paper51.com Me.Adodc1.RecordSource ="select * from 题库主表 where 题目套号=" & Me.Combo1.ItemData(Me.Combo1.ListIndex) paper51.com Me.Adodc1.Refresh paper51.com SetMe.MSHFlexGrid1.Recordset = Me.Adodc1.Recordset http://www.paper51.com
If Me.MSHFlexGrid1.Rows >1 Then 内容来自www.paper51.com Me.MSHFlexGrid1.Row = 1 http://www.paper51.com End If http://www.paper51.com Call InitSubjectAnswer 内容来自www.paper51.com End Sub copyright paper51.com 由于我们在选中上面表格的每道试题的时候,需要显示下面的数据,当表格的选中的单元格发生改变的时候会发生RowColChange事件,所以我们把备选答案的显示放在该事件里做,代码如下: 内容来自www.paper51.com Private SubMSHFlexGrid1_RowColChange() paper51.com Call InitSubjectAnswer 内容来自论文无忧网 www.paper51.com End Sub paper51.com
Private SubInitSubjectAnswer() copyright paper51.com If Me.MSHFlexGrid1.Rows >1 Then 内容来自论文无忧网 www.paper51.com Me.Adodc2.ConnectionString = pCN.ConnectionString http://www.paper51.com
Me.Adodc2.RecordSource = "select * from 题目明细 where 题目编号='" &Me.MSHFlexGrid1.TextMatrix(Me.MSHFlexGrid1.Row, 1) & "'" 内容来自www.paper51.com Me.Adodc2.Refresh paper51.com SetMe.MSHFlexGrid2.Recordset = Me.Adodc2.Recordset 内容来自www.paper51.com
Else paper51.com Me.MSHFlexGrid2.Rows = 1 内容来自www.paper51.com
End If copyright paper51.com End Sub copyright paper51.com 按上面的增加按钮后我们就弹出如下的界面: paper51.com copyright paper51.com 该代码里比较重要的代码如下: 内容来自论文无忧网 www.paper51.com 留给外部窗体调用的时候的接口代码: 内容来自www.paper51.com PublicFunction EditSubject(Optional ByVal lngID As Long = 0, Optional lngSubjectSetIDAs Long) 内容来自www.paper51.com Dim rst AsRecordset 内容来自www.paper51.com mlngSubjectID =lngSubjectSetID paper51.com mlngID = lngID copyright paper51.com mblnEdit = IIf(lngID > 0,True, False) paper51.com If mblnEdit Then 内容来自论文无忧网 www.paper51.com
Setrst = New Recordset 内容来自www.paper51.com
rst.Open "select * from 题库主表 whereID=" & lngID, pCN, adOpenStatic, adLockBatchOptimistic copyright paper51.com Text3 = rst![题目编号] 内容来自www.paper51.com Ifrst![题目类型] Then 内容来自www.paper51.com Option2.Value = True 内容来自论文无忧网 www.paper51.com Else copyright paper51.com Option1.Value = True copyright paper51.com
EndIf 内容来自www.paper51.com Text1 = rst![题目内容] 内容来自论文无忧网 www.paper51.com Text4 = rst![题目分值] copyright paper51.com Text5 = rst![题目答案] paper51.com Text2 = rst![题目备注] & "" http://www.paper51.com Text3.BackColor = &H80000018 内容来自论文无忧网 www.paper51.com Text3.Locked = True paper51.com Else 内容来自www.paper51.com Text3.BackColor = vbWhite 内容来自论文无忧网 www.paper51.com Text3.Locked = False copyright paper51.com
End If 内容来自www.paper51.com Set rst = Nothing 内容来自论文无忧网 www.paper51.com Me.Show 1 copyright paper51.com End Function paper51.com 保存代码如下 内容来自www.paper51.com Private SubCommand1_Click() copyright paper51.com Dim rst AsRecordset paper51.com Dim strTemp AsString 内容来自www.paper51.com Dim i As Integer http://www.paper51.com On Error GoToProc_Exit copyright paper51.com If Len(Text3) = 0 Then 内容来自www.paper51.com MsgBox "题目编号不为空!", vbInformation paper51.com
GoToProc_Exit copyright paper51.com End If http://www.paper51.com
If Len(Text1) = 0 Then 内容来自论文无忧网 www.paper51.com MsgBox "题目内容不为空!", vbInformation 内容来自论文无忧网 www.paper51.com GoToProc_Exit copyright paper51.com
End If copyright paper51.com If Len(Text5) = 0 Then 内容来自论文无忧网 www.paper51.com MsgBox "题目答案不为空!", vbInformation 内容来自论文无忧网 www.paper51.com GoToProc_Exit copyright paper51.com End If paper51.com
For i = 1 To Len(Text5) 内容来自论文无忧网 www.paper51.com strTemp = Mid(Text5, i, 1) 内容来自www.paper51.com IfNot (Asc(strTemp) >= 65 And Asc(strTemp) <= 90) And Not (Asc(strTemp)>= 97 And Asc(strTemp) <= 122) Then 内容来自论文无忧网 www.paper51.com
MsgBox "题目答案必须为字母!", vbInformation 内容来自论文无忧网 www.paper51.com GoTo Proc_Exit paper51.com EndIf http://www.paper51.com
Next i 内容来自www.paper51.com Set rst = New Recordset paper51.com If mblnEdit Then paper51.com rst.Open "select * from 题库主表 whereID=" & mlngID, pCN, adOpenStatic, adLockBatchOptimistic http://www.paper51.com
Else paper51.com rst.Open "select * from 题库主表", pCN,adOpenStatic, adLockBatchOptimistic copyright paper51.com rst.AddNew 内容来自论文无忧网 www.paper51.com
End If http://www.paper51.com rst![题目编号] = Text3 内容来自www.paper51.com If Option1.Value Then 内容来自论文无忧网 www.paper51.com rst![题目类型] = False http://www.paper51.com
Else http://www.paper51.com
rst![题目类型] = True copyright paper51.com End If paper51.com rst![题目内容] = Text1 内容来自论文无忧网 www.paper51.com rst![题目分值] = Val(Text4) 内容来自论文无忧网 www.paper51.com
rst![题目答案] = UCase(Text5) paper51.com rst![题目备注] = Text2 paper51.com rst![题目套号] = mlngSubjectID 内容来自www.paper51.com rst.UpdateBatch paper51.com Text1 = "" 内容来自www.paper51.com Text2 = "" paper51.com Text3 = "" http://www.paper51.com Text4 = "" copyright paper51.com Text5 = "" http://www.paper51.com
mblnEdit = False copyright paper51.com Text3.BackColor = vbWhite copyright paper51.com Text3.Locked = False http://www.paper51.com
Proc_Exit: 内容来自www.paper51.com Set rst = Nothing 内容来自www.paper51.com
If Err.Number <> 0Then 内容来自论文无忧网 www.paper51.com IfErr.Number = -2147467259 Then paper51.com MsgBox "题目编号不能重复!", vbInformation copyright paper51.com Else 内容来自www.paper51.com
MsgBox "发生意外错误,错误号:"& Err.Number & " 错误描述:" &Err.Description, vbInformation 内容来自www.paper51.com EndIf 内容来自www.paper51.com Err.Clear 内容来自www.paper51.com End If 内容来自论文无忧网 www.paper51.com End Sub paper51.com |