How can I use my current form name as a variable?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ryaanmichael
    New Member
    • May 2012
    • 24

    #1

    How can I use my current form name as a variable?

    I have a form that runs a series of sub routines when it loads. In my subs, I want to use a variable which pulls the name of the current form that I'm loading. I have the following code, but I keep getting error 2475: "You entered and expression that requires a form to be the active window".

    Code:
    Private Sub Form_Load()
        Dim frmCurrentForm As Form
           Set frmCurrentForm = Screen.ActiveForm
        Dim frmName As String
           frmName = frmCurrentForm.Name
    
    'I have various script like the one below which I need to use the form name as a variable
    
        userNameBC = Forms(frmName).userName.Value
    
    End Sub
    How am I supposed to define the form's name when the form loads, without getting error 2475? I've tried to run it on open rather than on load, but still doesn't work.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    If you're trying to get the name of the form that's loading, why not just use Me.Name?

    Comment

    • ryaanmichael
      New Member
      • May 2012
      • 24

      #3
      Hi Rabbit,

      Thanks for your reply. The reason I can't use me. is because I have multiple forms that will use one function. I need a way to get the form name by using a variable.

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Yes but if you're putting it into the Load event, then wouldn't the active form be the same as me?

        Comment

        • Stewart Ross
          Recognized Expert Moderator Specialist
          • Feb 2008
          • 2545

          #5
          I'd suggest that whatever common procedures or functions you run should be defined in a global-scope module, grouped into a suitable function or sub that will run the necessary steps in one go if possible, and given an appropriate name. You can then call the main DoSomething function or sub that groups everything together from whatever form's On Load sub you want without getting in a tangle over which form did the calling.

          Like Rabbit I think that Screen.ActiveFo rm is going to return the same form as Me would when used from On Load, but it's hard to tell when we don't know what happens as your code is executed - maybe you open another form so that the active form is no longer the one that started running whatever steps you are calling.

          The skeleton example below is how I'd implement a function in a global module to do whatever steps you are doing. You can pass the Me property as a parameter to the function, giving access to the calling form's name and other properties as appropriate:

          Code:
          Function DoSomething (SomeForm as Form) as WhateverTypeYouReturn
          .
          .
          frmName = SomeForm.Name
          .
          .
          End Function
          You would call such a function (or execute a Sub if you don't need a return value) from whatever form's On Load event like this:

          Code:
          Private Sub Form_Load()
          .
          SomeValue = DoSomething(Me)
          .
          End Sub
          Last edited by Stewart Ross; May 20 '13, 09:24 PM.

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            ryaanmichael,

            Pardon my confusion, but I thought that the form's name was not unique, in the case when multiple instances of a form were existant. I don't have the microsoft apps available at the moment, so I may be completely out to lunch at the moment.

            Regardless, may I ask why you are doing dynamic look-up of forms by name, rather than simply keeping references to the appropriate forms in your central application object? If you let us know what you are trying to achieve, we can probably give you some good suggestions about how to implement a robust solution.

            From what I can see, Rabbit and Stewart both have the right of it in that you have the "Me" property available in the form's subroutines and functions, so there should be no difficulty in the form registering its self with your central application control. Then you have direct access to the form from your global application code.

            Luck!
            Oralloy

            Comment

            Working...