views:

72

answers:

4

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.

+1  A: 

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.

dwo
The subs are within the same form's VBA code. The btnA_Clicked is generated by Access, but the question_answered sub is hand-written by my-self.
Nitrodist
If you are in the same form, you don't need to pass the control at all, because it is available there anyway!
dwo
A: 

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

Jeff O
No need to add the parameter names -- just remove the parens, i.e., Question_Answered "A", imgGreenA, imgRedA
David-W-Fenton
A: 

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

El Ronnoco
A: 

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)

David-W-Fenton