继续操作之前,请从选项组获取用户输入



我有一个数据库,可以按随机顺序生成测试问题。当我打开答案表(frmAnswers)时,我所能做的就是让它使用Enter键或esc键滚动浏览所有问题。我有一个弹出的MsgBox,只是为了让我知道一些变量设置正确,但代码不会暂停接受表单中OptionGroup的输入。

以下是我假设的相关代码:

Set rsCourse = CurrentDb.OpenRecordset(strCourse)
DoCmd.OpenForm ("frmAnswers")
rcdCnt = 1
While Not rsCourse.EOF
With rsCourse
Screen.ActiveForm.ctlQ_No = rcdCnt
Screen.ActiveForm.ctlQuestion = .Fields("Question")
Screen.ActiveForm.ctlAns_A = .Fields("Ans A")
Screen.ActiveForm.ctlAns_B = .Fields("Ans B")
Screen.ActiveForm.ctlAns_C = .Fields("Ans C")
Screen.ActiveForm.ctlAns_D = .Fields("Ans D")
Forms!frmAnswers!optAnswer.SetFocus
Select Case Forms.frmAnswers.optAnswer
Case Is = 1:  strAns = "A"
Case Is = 2:  strAns = "B"
Case Is = 3:  strAns = "C"
Case Is = 4:  strAns = "D"
Case Is = Null:  srtAns = "Nothing"
End Select
If strAns = .Fields("Correct Answer") Then
Exit Sub
Else
MsgBox "The correct answer is " & .Fields("Correct Answer") _
& Chr(13) & Chr(10) & "You answered " & strAns
End If
End With
rcdCnt = rcdCnt + 1
If rcdCnt > 100 Then
Exit Sub
End If
rsCourse.MoveNext
Wend

我已经搜索了很多网站,包括微软、pcreview、accessmvp等,但还没有找到任何有用的东西。我试过了;

Select Case 
Case 1
Case 2
Etc.
End Select

以及我的示例中的代码。除了MsgBox之外,似乎没有什么能暂停代码。

我也试着把这个代码作为一个函数:

Call TestClick(strCourse)

具有以下功能:

Function TestClick(strCourse)

函数中包含上述代码。它在Set rsCourse行返回一个编译错误:"Object required"。

我也尝试过将其作为一个子程序,但出现了相同的错误。

为了清晰起见,以下是我为调用frmAnswers form的表单编写的代码:

DoCmd.OpenForm ("frmIntroduction_VBA")
If IsNull(Me.cboTrainee_Name) Then                                    ' No Name
MsgBox "You must enter your name to continue!", vbOKOnly          ' Tell user
Me.cboTrainee_Name.SetFocus                                       ' Focus the control
Exit Sub                                                          ' Exit the method
End If                                                                ' End the IsNull test
Trainee_Name = Forms!frmIntroduction_VBA!cboTrainee_Name
If IsNull(Me.cboCourse) Then                                          ' Check if a course is selected
If IsNull(Me.cboVol) Then
MsgBox "You must select either a Course or Volume Review to continue!"         ' Tell user
Me.cboCourse.SetFocus
Exit Sub
End If
End If
If IsNull(Me.cboCourse) Then
strCourse = Me.cboVol.Value
Else
strCourse = Me.cboCourse.Value
End If

我想实际调用frmAnswers表单的另一个子,但不知道如何将rsCourse变量传递给子。

我相信这是一个相当容易解决的问题,但我无论如何都不是专家。一旦我解决了这个问题,我将继续,并尝试让VBA创建一个记录集,其中包含要附加到现有表的测试结果。

感谢大家提供的帮助。

这只是一百万种不同的方法之一。如果其他人以其他方式配合,我的感情不会受到伤害。但这个解决方案可能最符合你已经走的路:

您需要一种方法将strCourse传递到表单。
-一种方法是在frmAnswers类模块中声明一个String变量strCourse,并在打开frmAanswers后从frmIntroduction_VBA进行设置
-另一种方法是在frmAsnwers上创建一个名为strCourse的不可见字段,并在使用form!frmAnswers!strCourse=strCourse打开表单后进行设置
-我认为最简单的方法就是从frmAnswers表单中引用frmIntroduction_VBA表单。这就是我们在这里要做的。

首先:打开frmAnswers。

DoCmd.OpenForm("frmAnswers")

现在,让我们将您的所有其余代码移动到frmAnswers表单中。这是frmAnswers类模块:

Option Explicit
'The following will be variables that will persist as long as the form is open
dim rsCourse as Recordset
dim strCourse as String
dim rcdCnt as Long
dim strCorrectAnswer as String
Private Sub Form_Load()    'This basically initializes the variables and loads the first question
If IsNull(Forms!frmIntroduction_VBA!cboCourse) Then
strCourse = Forms!frmIntroduction_VBA!cboVol
Else
strCourse = Forms!frmIntroduction_VBA!cboCourse
End If
Set rsCourse = CurrentDb.OpenRecordset(strCourse)
rcdCnt = 0
LoadNextQuestion
End Sub

Private Sub LoadNextQuestion()   'reusable code to load questions
rcdCnt=rcdCnt+1
If (rcdCnt>100) OR rsCourse.EOF Then
rs.Close
DoCmd.Close acForm, "frmAnswers"
Exit Sub
End If
With rsCourse
ctlQ_No = rcdCnt
ctlQuestion = !Question
ctlAns_A = ![Ans A]
ctlAns_B = ![Ans B]
ctlAns_C = ![Ans C]
ctlAns_D = ![Ans D]
strCorrectAnswer = ![Correct Answer]
optAnswer = Null   'clears previous answer
optAnswer.SetFocus
.MoveNext
End With
End Sub


Private Sub btnSubmit_Click()
Dim strAnswer As String
strAnswer = "Nothing"
Select Case optAnswer
Case 1:
strAnswer = "A"
Case 2:
strAnswer = "B"
Case 3:
strAnswer = "C"
Case 4:
strAnswer = "D"
End Select
If strAns = strCorrectAnswer Then
MsgBox "Correct!"
Else
MsgBox "The correct answer is " & strCorrectAnswer & "." _
& Chr(13) & Chr(10) & "You answered " & strAns &"."
End If
LoadNextQuestion
End Sub

从这个开始,然后继续玩。如果你不确定我为什么要做某件事,或者如果我错过了你正在做的事情的一些基本方面,请在评论中留下它,我们会不断完善它。

最新更新