?? frmselect.frm
字號:
Left = 622
TabIndex = 18
Top = 2274
Width = 450
End
Begin VB.Label Label4
AutoSize = -1 'True
Caption = "選項C"
Height = 180
Left = 622
TabIndex = 17
Top = 2632
Width = 450
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "題干內容"
Height = 180
Left = 622
TabIndex = 16
Top = 315
Width = 720
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "選項A"
Height = 180
Left = 622
TabIndex = 15
Top = 1916
Width = 450
End
End
Attribute VB_Name = "SelectOne"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Dim isAdding As Boolean '定義操作狀態標志
Dim objSelect As Recordset '用于保存單項選擇題數據表記錄
Dim objCn As Connection '用于建立數據庫聯接
Private Sub cmdExit_Click()
Unload Me '關閉單項選擇題管理窗體
End Sub
Private Sub Form_Load()
'建立數據庫聯接
Set objCn = New Connection '實例化聯接對象
With objCn '建立數據庫聯接
.Provider = "SQLOLEDB"
.ConnectionString = "User ID=sa;PWD=123;Data Source=(local);" & _
"Initial Catalog=自測考試"
.Open
End With
'獲取單項選擇題記錄
Set objSelect = New Recordset '實例化objSelect對象
With objSelect
Set .ActiveConnection = objCn
.CursorLocation = adUseClient '指定使用客戶端游標
.CursorType = adOpenStatic '指定使用靜態游標
.LockType = adLockOptimistic
.Open "SELECT * FROM 選擇題" '獲取單項選擇題數據
End With
cmdMove(0).Value = True '觸發按鈕單擊事件,顯示第一個記錄
End Sub
Private Sub cmdMove_Click(Index As Integer)
With objSelect
Select Case Index '切換當前記錄
Case 0 '使第一個記錄成為當前記錄
If .RecordCount > 0 And Not .BOF Then .MoveFirst
Case 1 '使上一個記錄成為當前記錄
If .RecordCount > 0 And Not .BOF Then
.MovePrevious
If .BOF Then .MoveFirst
End If
Case 2 '使下一個記錄成為當前記錄
If .RecordCount > 0 And Not .EOF Then
.MoveNext
If .EOF Then .MoveLast
End If
Case 3 '使最后一個記錄成為當前記錄
If .RecordCount > 0 And Not .EOF Then .MoveLast
End Select
Show_Data
End With
If isAdding Then isAdding = False
End Sub
Private Sub cmdAdd_Click()
txtNews = "添加新記錄"
txtQuestion = ""
txtA = "": txtB = ""
txtC = "": txtD = ""
optA = True
txtPoint = "1"
txtParse = ""
isAdding = True
txtQuestion.SetFocus
End Sub
Private Sub cmdDelete_Click()
'根據是否處于添加記錄狀態執行不同的操作
If isAdding Then
'退出添加記錄狀態,顯示當前記錄
isAdding = False
If objSelect.RecordCount <= 0 Then
txtNews = "記錄:無" '顯示無記錄提示
Else
Show_Data '顯示當前記錄數據
End If
Else
If objSelect.RecordCount > 0 Then
If MsgBox("是否刪除當前記錄?", vbYesNo + vbQuestion, _
"單項選擇題管理") = vbYes Then
objSelect.Delete '執行刪除當前記錄操作
cmdMove(2).Value = True '顯示下一記錄數據
Else
Show_Data '顯示當前記錄數據
End If
End If
End If
End Sub
Private Sub cmdSave_Click()
Dim objCopy As New Recordset
'在當前表中無數據和不是添加記錄時,不執行保存操作
If Not isAdding And objSelect.RecordCount < 1 Then Exit Sub
If Trim(txtQuestion) = "" Then
MsgBox "題干不能為空!", vbCritical, "單項選擇題管理"
txtQuestion.SetFocus: txtQuestion = ""
ElseIf Trim(txtA) = "" Then
MsgBox "請輸入選項A!", vbCritical, "單項選擇題管理"
txtA.SetFocus: txtA = ""
ElseIf Trim(txtB) = "" Then
MsgBox "請輸入選項B!", vbCritical, "單項選擇題管理"
txtB.SetFocus: txtB = ""
ElseIf Trim(txtC) = "" Then
MsgBox "請輸入選項C!", vbCritical, "單項選擇題管理"
txtC.SetFocus: txtC = ""
ElseIf Trim(txtD) = "" Then
MsgBox "請輸入選項D!", vbCritical, "單項選擇題管理"
txtD.SetFocus: txtD = ""
ElseIf Not txtPoint Like "[1-9]" Then
MsgBox "請輸入有效的分數!", vbCritical, "單項選擇題管理"
txtPoint.SetFocus
txtPoint.SelStart = 0: txtPoint.SelLength = Len(txtPoint)
Else
Set objCopy = objSelect.Clone
With objCopy
If .RecordCount > 0 Then
'檢查題干是否重復
.MoveFirst
.Find "題干='" & Trim(txtQuestion) & "'"
If (isAdding And Not .EOF) Or _
(Not isAdding And Not .EOF And _
.AbsolutePosition <> objSelect.AbsolutePosition) Then
MsgBox "試題重復,請修改!", vbCritical, "單項選擇題管理"
txtQuestion.SetFocus
txtQuestion.SelStart = 0
txtQuestion.SelLength = Len(txtQuestion)
Exit Sub
End If
If isAdding Then objSelect.AddNew '添加新記錄
Else
If isAdding Then objSelect.AddNew '添加新記錄
End If
objSelect.Fields("題干") = Trim(txtQuestion)
objSelect.Fields("選項a") = Trim(txtA)
objSelect.Fields("選項b") = Trim(txtB)
objSelect.Fields("選項c") = Trim(txtC)
objSelect.Fields("選項d") = Trim(txtD)
objSelect.Fields("分數") = Trim(txtPoint)
objSelect.Fields("答案") = Switch(optA, "A", optB, "B", optC, "C", optD, "D")
If Trim(txtParse) <> "" Then objSelect.Fields("解析") = Trim(txtParse)
objSelect.Update
MsgBox "數據保存成功!", vbInformation, "單項選擇題管理"
isAdding = False
'顯示當前記錄編號和記錄總數
txtNews = "記錄:" & objSelect.AbsolutePosition & "/" & objSelect.RecordCount
End With
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
objCn.Close '關閉數據聯接
Set objCn = Nothing '釋放數據庫聯接
Set objSelect = Nothing '釋放記錄集對象
End Sub
'限制分數輸入
Private Sub txtPoint_KeyPress(KeyAscii As Integer)
If Not (Chr(KeyAscii) Like "[1-9]" Or KeyAscii = vbKeyBack) Then
KeyAscii = 0 '輸入不是數字或退格鍵,取消輸入
End If
End Sub
Private Sub Show_Data()
Dim i%
With objSelect
If .RecordCount < 1 Then
txtNews = "記錄:無" '顯示無記錄提示
'清除顯示數據
txtQuestion = "": txtPoint = "": txtParse = ""
txtA = "": txtB = "": txtC = "": txtD = ""
Else
'顯示當前記錄數據
txtQuestion = .Fields("題干"): txtPoint = .Fields("分數")
If IsNull(.Fields("解析")) Then
txtParse = ""
Else
txtParse = .Fields("解析")
End If
txtA = .Fields("選項a"): txtB = .Fields("選項b")
txtC = .Fields("選項c"): txtD = .Fields("選項d")
'顯示參考答案
Select Case .Fields("答案")
Case "A"
optA = True
Case "B"
optB = True
Case "C"
optC = True
Case "D"
optD = True
End Select
'顯示當前記錄編號和記錄總數
txtNews = "記錄:" & .AbsolutePosition & "/" & .RecordCount
End If
End With
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -