Control form image from VBA Module (1 Viewer)

MrHans

Registered User
Local time
Today, 15:21
Joined
Jul 27, 2015
Messages
147
Hi all,

I need some help.
On my form I have an unbound image object.
I want to set the image source and image tooltip text from an external procedure (module in the same DB).

It's basically a validation sub.
The user enters some details, for example an e-mail address.
Then I run the validation procedure.
If the validation is successfull I want to display a green check mark, if it's not successfull, I want to display a red cross icon.

In the form itself it's easy:
Code:
me.imgResult.Picture = "path\check-fail.png"
me.imgResult.ControlTipText = "Validation failed..."
me.imgResult.Visible = True
But I want to perform these actions in the Validation module.

Something like:
Call ValidateInput(me.imgResult)

And
Code:
Public Function ValidateInput(img as Control)
   Validation stuff here

   if Validation = False then
   img.Picture = "path\check-fail.png"
   img.ControlTipText = "Validation failed..."
   img.Visible = True
End Function
How do I reference the image properly?
Is it an object? Or control?

Thanks!!
 

June7

AWF VIP
Local time
Today, 05:21
Joined
Mar 9, 2014
Messages
5,501
I would use ControlSource property instead of Picture. Setting Picture property was required before Access2007 when ControlSource property was added to the Image control. I have never used Picture.

Is Validation a yes/no field? Expression in ControlSource:

="filepath\check-" & IIf([Validation], "pass", "fail") & ".png"

Don't really see need for ControlTip. If you must display text along with the image, use a textbox with similar expression in ControlSource.

No need for VBA.

Setting control properties with VBA will not work nicely if form is Continuous. ALL instances of the control will reflect the same setting.
 
Last edited:

MrHans

Registered User
Local time
Today, 15:21
Joined
Jul 27, 2015
Messages
147
Thanks for the feedback June.
I will switch to ControlSource instead.

I'm actually using the ControlTipText to provide feedback on the result, what is wrong etc.
So is it possible to refer to the name of the image in the function call?

I have mulitple of these validation checks and images to illustrate the result.
So it would be good if I can refer to this specific image object in the function.
 

June7

AWF VIP
Local time
Today, 05:21
Joined
Mar 9, 2014
Messages
5,501
Don't really see need to pass image object to a VBA procedure, just the image name as string.

What you have posted is really a Sub procedure, not a Function, in spite of the declaration. Normally use a function to return a value to a calling object or procedure. A Sub just does something but does not return a result. There are occasions requiring a procedure to be declared as Function even if no result is returned. For instance, a function can be called by a macro or referenced in ControlSource but Sub cannot.

But where would you call this procedure from? With ControlSource expression in Image and textbox controls, why do you need VBA? What would be 'validation stuff'?

Is this form in Single View?
 

MrHans

Registered User
Local time
Today, 15:21
Joined
Jul 27, 2015
Messages
147
Yes, the example I provided was just air code.
In fact, I have a Public Sub.

I'm calling it from a single form, not continuous.
But there are multiple (single) form that have this same validation procedure and images.

So maybe I need to include both the form name and the image object in the call?
Call ValidateResult(me.Name, me.imgResult)

Code:
Public Sub ValidateResult(frm as Form, img as Control)
  Do some validation bla bla

  with frm.img
     .ControlSource = ....
     .ControlTipText = " ... "
     .Visible = True
  end with
End Sub
 

June7

AWF VIP
Local time
Today, 05:21
Joined
Mar 9, 2014
Messages
5,501
Yes, or use my suggestion that eliminates VBA.

Your With might work, I've never tried. I have passed names of objects and used code like:

Forms(strForm).Controls(strImg).Visible = True

Neither approach will work if any form is used as a subform.
 

MrHans

Registered User
Local time
Today, 15:21
Joined
Jul 27, 2015
Messages
147
Ok, finally got it to work.

The image object is in fact recognised as an image.

So in the Public Sub call is now:
Call ValidateInput(me.imgResult)

And in the Public Sub it is declared as an image:
Public Sub ValidateInput(img as Image)

To modify the image itself, I do need to use the Picture property.
Anyway, it works now!

Thanks for the responses June.
 

Users who are viewing this thread

Top Bottom