将表单对象传递给 MS-Access 中的 VBA 子/函数

发布于 2024-09-11 18:45:08 字数 736 浏览 6 评论 0原文

我有一个小的多项选择应用程序。根据正确答案或错误答案,会出现 4 个绿色复选标记和 4 个红色 X。

它们最初都是不可见的,并且位于表单上的特定位置,因此当它们变得可见时,如果他们在答案旁边得到它,它会像一个绿色复选标记,如果他们得到答案,则在他们的答案旁边会出现一个红色复选标记他们弄错了。

我决定制作一个子过程,它接受三个参数,它们的答案(“A”,“B”,“C”或“D”),使绿色图像引用变得可见,使红色图像引用变得可见。

不幸的是,我根本无法让他们通过推荐信。智能感知知道我指的是什么对象。

Private Sub btnA_Clicked ()
  Question_Answered("A", imgGreenA, imgRedA) 'images referenced from form'
End Sub

Private Sub Question_Answered (strUserAnswer as String, imgGreen as Image, imgRed as Image)
  ...
End Sub

另一个(可能相关)问题是我无法将表单中的图像分配给 Question_Answered 子中的局部变量,如下所示:

Dim imgGreen as Image
imgGreen = imgGreenA

Using MS-Access 2003 MDB with MS-Access 2007 .

I have a little multiple choice application. There are 4 green check marks and 4 red x's that will come up based on the right answer or the wrong answer.

They are all not-visible initially and are in specific places on the form so that when they become visible, it'll be like a green check mark if they get it right next to their answer and a red check mark next to their answer if they get it wrong.

I decided to make a sub-procedure that accepts three arguments, their answer ("A", "B", "C" or "D"), the green image reference to make visible and the red image reference to make visible.

Unfortunately, I can't make them pass the references at all. The intellisense knows what objects I'm referring to.

Private Sub btnA_Clicked ()
  Question_Answered("A", imgGreenA, imgRedA) 'images referenced from form'
End Sub

Private Sub Question_Answered (strUserAnswer as String, imgGreen as Image, imgRed as Image)
  ...
End Sub

Another (probably related) problem is that I can't assign the images from the form to local variables in that Question_Answered sub, like this:

Dim imgGreen as Image
imgGreen = imgGreenA

Using MS-Access 2003 MDB with MS-Access 2007.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

如梦初醒的夏天 2024-09-18 18:45:09

我遇到了同样的问题,我在子界面中使用“作为对象”解决了它。试试这个:

    Private Sub btnA_Clicked ()
       Question_Answered("A", imgGreenA, imgRedA) 'images referenced from form'
    End Sub

    Private Sub Question_Answered (strUserAnswer as String, imgGreen as Object, imgRed as Object)
       ...
    End Sub

I had the same problem and I solved it using "As Object" in the sub interface. Try this:

    Private Sub btnA_Clicked ()
       Question_Answered("A", imgGreenA, imgRedA) 'images referenced from form'
    End Sub

    Private Sub Question_Answered (strUserAnswer as String, imgGreen as Object, imgRed as Object)
       ...
    End Sub
抚你发端 2024-09-18 18:45:08

使用控制对象调用 VBA 函数确实可以正常工作。您是否以相同的形式或在某个模块中编写了您的函数?

要分配控制变量,您必须使用set,因此它是Set imgGreen = imgGreenA

Calling a VBA-function with a control object does work without problems. Did you write your function in the same form or in some module?

To assign a control variable, you have to use set, so it is Set imgGreen = imgGreenA.

烟酒忠诚 2024-09-18 18:45:08

您是否尝试过

Private Sub Question_Answered (strUserAnswer as String, ByRef imgGreen as Image, ByRef imgRed as Image)
  ...
End Sub

并尝试编写不带括号的调用语句,我遇到了当事物位于/不在括号中时 VBA 不传递引用的问题。例如。

Private Sub btnA_Clicked ()
  Question_Answered "A", imgGreenA, imgRedA 'images referenced from form'
End Sub

正如 HansUp 所说,您不需要(或想要)在 Question_Answered 内重新调暗 imgGreen

Have you tried

Private Sub Question_Answered (strUserAnswer as String, ByRef imgGreen as Image, ByRef imgRed as Image)
  ...
End Sub

and also try writing the call statement without brackets, I've had issues with VBA not passing references when things are/are not in brackets. eg.

Private Sub btnA_Clicked ()
  Question_Answered "A", imgGreenA, imgRedA 'images referenced from form'
End Sub

and as HansUp says you don't need (or want) to re-dim imgGreen when within Question_Answered

情仇皆在手 2024-09-18 18:45:08

由于您没有发布 Question_Answered() 子例程的内容,因此不可能说出问题是什么,但让我印象深刻的第一件事是您已将图像声明为图像而不是控件。图像实际上从来不会直接在窗体上,而是封装在图像控件内,因此您要使用的控件实际上根本不是图像。

所以:

  Private Sub Question_Answered (ByVal strUserAnswer As String, ByRef imgGreen As Control, ByRef imgRed As Control)

现在,我可能是错的,但这是一个起点。

(另请注意,我明确调用 ByRef 或 ByVal)

Since you don't post the contents of your Question_Answered() subroutine, it's impossible to say what the issue is, but the first thing that sticks out to me is that you've declared the images as images instead of as controls. An image is never actually directly on a form, but encapsulated inside an image control, so the control you're going to be working with won't actually be an image at all.

So:

  Private Sub Question_Answered (ByVal strUserAnswer As String, ByRef imgGreen As Control, ByRef imgRed As Control)

Now, I could be wrong there, but it's a place to start.

(also note that I'm explicit about calling ByRef or ByVal)

久伴你 2024-09-18 18:45:08

如果在调用子程序时出现错误,

请将: Question_Answered("A", imgGreenA, imgRedA) 更改

为:Question_Answered strUserAnswer:="A", imgGreen:=imgGreenA, imgRed :=imgRedA

If you get an error when the sub gets called the,

Change: Question_Answered("A", imgGreenA, imgRedA)

To: Question_Answered strUserAnswer:="A", imgGreen:=imgGreenA, imgRed:=imgRedA

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文