Reputation: 1745
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.
Upvotes: 1
Views: 10683
Reputation: 11
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
Upvotes: 0
Reputation: 23067
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)
Upvotes: 1
Reputation: 8043
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
Upvotes: 0
Reputation: 11922
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
Upvotes: 1
Reputation: 3636
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
.
Upvotes: 2