1

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.

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
If you are in the same form, you don't need to pass the control at all, because it is available there anyway!
but dwo he is trying to pass to the same function for logic processing for different object on the main form.
1

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

Comments

1

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)

Comments

0

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

1 Comment

No need to add the parameter names -- just remove the parens, i.e., Question_Answered "A", imgGreenA, imgRedA
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.