Access VBA脚本未成功将变量传递到Report中的Text Box字段

时间:2017-09-14 18:47:54

标签: vba ms-access access-vba

我正在尝试通过VBA将简单变量传递到报告上的文本框。

我已经完成了研究,并看到过使用Me.fieldName = value之类的内容,但是我尝试使用关键字错误。所以我尝试使用OpenReport的Where部分,但似乎也不会飞。

代码会按预期请求输入,然后打开报告,然后请求参数,即使它应该从字符串接收它。

我知道我错过了一些东西,但我的眼睛却没有看到它。请帮我解开我的白痴。

当前相关代码:

Dim Message, Title, Default, MyValue
Message = "Please enter test variation (A, B, C, etc)"
Title = "Test Variation"
Default = ""
MyValue = InputBox(Message, Title, Default)
DoCmd.OpenReport "Random_Test", acViewPreview, , "TestNum=" & MyValue

编辑:为清晰起见: Random_Test是我填写的报告。该报告中包含一个名为Text Box的{​​{1}}类型字段 TestNum InputBox应该接受用户输入以使MyValue表示某些东西,(据我所知)应该通过MyValue调用的Where部分传递。

我可以在参数中手动输入数据,但我不想这样做,特别是如果我需要快速生成多个文件。

1 个答案:

答案 0 :(得分:1)

您可以在.OpenArgs属性中传递参数,并在报告加载时捕获它。

传递给.OpenArgs:

DoCmd.OpenReport "Random_Test", acViewPreview, , , acWindowNormal, "TestNum=" & MyValue

捕获报告的加载事件:

Private Sub Report_Load()
    With Me
        If Not IsNull(.OpenArgs) Then
            .TestNum.Value = .OpenArgs
        End If
    End With
End Sub