在继续之前从选项组获取用户输入

时间:2017-10-25 18:36:26

标签: ms-access access-vba

我有一个以随机顺序生成测试问题的数据库。当我得到答案表格(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)

在函数中使用上面的代码。它返回一个编译错误:"需要对象"在设置rsCourse线。

我也尝试过将此作为具有相同错误的子程序。

对于clairification,这是我调用frmAnswers表单的表单的代码:

    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表单调用另一个Sub,但不知道如何将rsCourse变量传递给Sub。

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

感谢大家提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

这只是百万种不同方式中的一种。如果其他人以其他方式插话,我的感受并不会受到伤害。但是这个解决方案可能与你已经走下去的道路最为一致:

你需要一种方法将你的strCourse传递给表格 - 一种方法是在frmAnswers类模块中声明String变量strCourse,并在打开frmAnswers后从frmIntroduction_VBA设置它。
- 另一种方法是在名为strCourse的frmAsnwers上创建一个不可见的字段,并在用form!frmAnswers!strCourse=strCourse打开表单后设置它。
- 我想你最简单的方法就是从frmnswers表单中引用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

从那开始并玩弄它。如果您不确定我为什么要做某些事情,或者我错过了您正在做的事情的一些基本方面,请将其留在评论中,我们会不断完善它。