473,399 Members | 2,159 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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

twinnyfo
3,653 Expert Mod 2GB
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!

2 1820
twinnyfo
3,653 Expert Mod 2GB
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
Killer42
8,435 Expert 8TB
Glad we could help. :)

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

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

Similar topics

3
by: Scott Navarre | last post by:
Hello, I have a javascript function that I want to read the name of the form which is calling it. Here is my HTML: <HTML> <HEAD> <SCRIPT LANGUAGE="JavaScript">
4
by: nospam_stunter2001 | last post by:
Hi folks, I'm new here and am a neophyte Access 2000 user/developer. A year ago, I barely knew what Access was. I was asked by the people that pay us to begin keeping certain data about the...
7
by: Saintor | last post by:
What I do now is I put a value in the tag property, and using the form_current event, I run through all controls properties until the ones with the required tag value are met. Sound OK in theory,...
4
by: Dean Slindee | last post by:
Is there a shorter way of referring to another forms controls/events than creating a class hold forms, adding a form item each time a new form is loaded, and then looping thru the class of forms to...
3
by: Tim Fitzgerald | last post by:
Hello all, I have no problem accessing another form in my app.. however, when I try to access a ListView on a different form, I come up empty... Basically, Dim pForm As New frmMain Dim iCount...
3
by: B-Dog | last post by:
I'm capturing the checked radio button to XML file using the name of the radio button. I want to read my xml file to find which button was checked on close and the check the appropriate button...
4
by: Ryan | last post by:
I have some custom forms that contain their own properties and I call them as such: Dim myDialog As New myDialogForm myDialog.mID = 1 ' sets the custom property mID to 1...
4
by: Michael R | last post by:
Hi All. Is it possible to concentrate dim frm as Form set frm = Forms! & strVar or dim frm as Form strVar = "Forms!frmFormName" set frm = strVar
9
by: scripteaze | last post by:
Im using mechanize method for retrieving the form so that i may log into it. I need to find a way to get the form name. Its not listed anywhere in the html source.The reason i need to do this is...
2
by: ejamnadas | last post by:
I have a Function in a Module that takes in a parameter. From my form, I call that function and pass my form name. I'm trying to use forms!formname.Controls in the function and I get the error: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.