By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,883 Members | 955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,883 IT Pros & Developers. It's quick & easy.

Refer to another forms controls using a variable as the form name

twinnyfo
Expert Mod 2.5K+
P: 3,205
Dear Bytes Fans,

Here is what my database does: On my standard forms, I have quite a few images that are displayed. They are placed at random locations throughout the forms, and although the pcitures stay the same for every form, they are sometimes in different locations or come in different sizes because of form design, etc. This is a sample of the current code:

Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. On Error GoTo EH
  3.     Me.Pic1.Picture = strDBASEPATH & "IMAGES\Pic1.jpg"
  4.     Me.Pic2.Picture = strDBASEPATH & "IMAGES\Pic2.jpg"
  5.     Me.Pic3.Picture = strDBASEPATH & "IMAGES\Pic3.jpg"
  6.     Me.Pic4.Picture = strDBASEPATH & "IMAGES\Pic4.jpg"
  7.     Me.Pic5.Picture = strDBASEPATH & "IMAGES\Pic5.jpg"
  8.     Exit Sub
  9. EH:
  10.     MsgBox "Error initializing Form!  Please contact your Database Administrator.", vbCritical, "Error!"
  11.     Exit Sub
  12. End Sub
  13.  
I am doing it this way, because when I actually embedded the photos on the forms, the DB file size balooned. This code works fine.

What I want to do is create a Function in a module that receives the current Form name, and sets the images for those controls on the main form from which the function is called. This is what I have right now:

On the main form:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Open(Cancel As Integer)
  2. On Error GoTo EH
  3.     DisplayImages Me.Form.Name
  4.     Exit Sub
  5. EH:
  6.     MsgBox "Error initializing Form!  Please contact your Database Administrator.", vbCritical, "Error!"
  7.     Exit Sub
  8. End Sub
  9.  
In the module:
Expand|Select|Wrap|Line Numbers
  1. Public Function DisplayImages(FormName As String)
  2. On Error GoTo EH
  3.     Forms!FormName!Pic1.Picture = strDBASEPATH & "IMAGES\Pic1.jpg"
  4.     Forms!FormName!Pic2.Picture = strDBASEPATH & "IMAGES\Pic2.jpg"
  5.     Forms!FormName!Pic3.Picture = strDBASEPATH & "IMAGES\Pic3.jpg"
  6.     Forms!FormName!Pic4.Picture = strDBASEPATH & "IMAGES\Pic4.jpg"
  7.     Forms!FormName!Pic5.Picture = strDBASEPATH & "IMAGES\Pic5.jpg"
  8.     Exit Function
  9. EH:
  10.     MsgBox "Error displaying images!  Please contact your Database Administrator.", vbCritical, "Error!"
  11.     Exit Function
  12. End Function
  13.  
Every time the code executes, I get the error "Can't find the form 'FormName' referred to in Visual Basic Code."

I know "why" I am getting this error, because the code thinks that "FormName" is the name of the Form. However, how do I set the code in the module so that the Form name that the VBA code is looking for is the *variable* which is the name of the main form from which the code is executed, eg, the code should be looking for "frmMainForm" and NOT "FormName"?

I've tried as many permutations of brackets, quotes, dots, parentheses etc. I can think of, but nothing seems to work.

Also, if any o' all y'all think I am just chasing my tail on this and there is no need for this code, let me know. I prefer to code things in one area, so that I can call it from aywhere at anytime. Just a preference, but if that is not going to work in this instance, then I am cool with that.

Any suggestions?
Warmest regards!
Mar 1 '12 #1

✓ answered by twinnyfo

After several MORE hours of playing around and experimenting, I think I have the solution:

Expand|Select|Wrap|Line Numbers
  1. Public Function DisplayImages(FormName As String) 
  2. On Error GoTo EH 
  3.     Forms(FormName).Pic1.Picture = strDBASEPATH & "IMAGES\Pic1.jpg" 
  4.     Forms(FormName).Pic2.Picture = strDBASEPATH & "IMAGES\Pic2.jpg" 
  5.     Forms(FormName).Pic3.Picture = strDBASEPATH & "IMAGES\Pic3.jpg" 
  6.     Forms(FormName).Pic4.Picture = strDBASEPATH & "IMAGES\Pic4.jpg" 
  7.     Forms(FormName).Pic5.Picture = strDBASEPATH & "IMAGES\Pic5.jpg" 
  8.     Exit Function 
  9. EH: 
  10.     MsgBox "Error displaying images!  Please contact your Database Administrator.", vbCritical, "Error!" 
  11.     Exit Function 
  12. End Function
Works like a charm! Hope this helps anyone else who has a similar requirement!

Share this Question
Share on Google+
2 Replies


twinnyfo
Expert Mod 2.5K+
P: 3,205
After several MORE hours of playing around and experimenting, I think I have the solution:

Expand|Select|Wrap|Line Numbers
  1. Public Function DisplayImages(FormName As String) 
  2. On Error GoTo EH 
  3.     Forms(FormName).Pic1.Picture = strDBASEPATH & "IMAGES\Pic1.jpg" 
  4.     Forms(FormName).Pic2.Picture = strDBASEPATH & "IMAGES\Pic2.jpg" 
  5.     Forms(FormName).Pic3.Picture = strDBASEPATH & "IMAGES\Pic3.jpg" 
  6.     Forms(FormName).Pic4.Picture = strDBASEPATH & "IMAGES\Pic4.jpg" 
  7.     Forms(FormName).Pic5.Picture = strDBASEPATH & "IMAGES\Pic5.jpg" 
  8.     Exit Function 
  9. EH: 
  10.     MsgBox "Error displaying images!  Please contact your Database Administrator.", vbCritical, "Error!" 
  11.     Exit Function 
  12. End Function
Works like a charm! Hope this helps anyone else who has a similar requirement!
Mar 1 '12 #2

Expert 5K+
P: 8,434
Glad we could help. :)

Seriously, thanks for posting your solution. As you said, hopefully it will help others.
Mar 12 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.