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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3662

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

    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:

    Code:
    Private Sub Form_Open(Cancel As Integer)
    On Error GoTo EH
        Me.Pic1.Picture = strDBASEPATH & "IMAGES\Pic1.jpg"
        Me.Pic2.Picture = strDBASEPATH & "IMAGES\Pic2.jpg"
        Me.Pic3.Picture = strDBASEPATH & "IMAGES\Pic3.jpg"
        Me.Pic4.Picture = strDBASEPATH & "IMAGES\Pic4.jpg"
        Me.Pic5.Picture = strDBASEPATH & "IMAGES\Pic5.jpg"
        Exit Sub
    EH:
        MsgBox "Error initializing Form!  Please contact your Database Administrator.", vbCritical, "Error!"
        Exit Sub
    End Sub
    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:
    Code:
    Private Sub Form_Open(Cancel As Integer)
    On Error GoTo EH
        DisplayImages Me.Form.Name
        Exit Sub
    EH:
        MsgBox "Error initializing Form!  Please contact your Database Administrator.", vbCritical, "Error!"
        Exit Sub
    End Sub
    In the module:
    Code:
    Public Function DisplayImages(FormName As String)
    On Error GoTo EH
        Forms!FormName!Pic1.Picture = strDBASEPATH & "IMAGES\Pic1.jpg"
        Forms!FormName!Pic2.Picture = strDBASEPATH & "IMAGES\Pic2.jpg"
        Forms!FormName!Pic3.Picture = strDBASEPATH & "IMAGES\Pic3.jpg"
        Forms!FormName!Pic4.Picture = strDBASEPATH & "IMAGES\Pic4.jpg"
        Forms!FormName!Pic5.Picture = strDBASEPATH & "IMAGES\Pic5.jpg"
        Exit Function
    EH:
        MsgBox "Error displaying images!  Please contact your Database Administrator.", vbCritical, "Error!"
        Exit Function
    End Function
    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 "frmMainFor m" 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!
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3662

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

    Code:
    Public Function DisplayImages(FormName As String) 
    On Error GoTo EH 
        Forms(FormName).Pic1.Picture = strDBASEPATH & "IMAGES\Pic1.jpg" 
        Forms(FormName).Pic2.Picture = strDBASEPATH & "IMAGES\Pic2.jpg" 
        Forms(FormName).Pic3.Picture = strDBASEPATH & "IMAGES\Pic3.jpg" 
        Forms(FormName).Pic4.Picture = strDBASEPATH & "IMAGES\Pic4.jpg" 
        Forms(FormName).Pic5.Picture = strDBASEPATH & "IMAGES\Pic5.jpg" 
        Exit Function 
    EH: 
        MsgBox "Error displaying images!  Please contact your Database Administrator.", vbCritical, "Error!" 
        Exit Function 
    End Function
    Works like a charm! Hope this helps anyone else who has a similar requirement!

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #3
      Glad we could help. :)

      Seriously, thanks for posting your solution. As you said, hopefully it will help others.

      Comment

      Working...