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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3344417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 11:57:06  来源:igfitidea点击:

Passing Form Objects to VBA Subs/Functions in MS-Access

ms-accessvbapass-by-reference

提问by Nitrodist

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.

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

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.

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

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_Answeredsub, like this:

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

Dim imgGreen as Image
imgGreen = imgGreenA

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

将 MS-Access 2003 MDB 与 MS-Access 2007 一起使用。

回答by dwo

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?

使用控制对象调用 VBA 函数确实没有问题。您是否以相同的形式或在某个模块中编写了您的函数?

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

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

回答by El Ronnoco

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.

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

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

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

回答by David-W-Fenton

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.

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

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)

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

回答by Giuseppe Mignogna

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

回答by JeffO

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

如果在调用 sub 时出现错误,

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

改变: Question_Answered("A", imgGreenA, imgRedA)

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

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