• Getting a Form Name

    Author
    Topic
    #472097

    Dear Loungers,

    I want to know the name of the current form. So I thougt I could have a small VB routine that would run on Load… something like this:

    Dim frmCurrentForm As Form
    Set frmCurrentForm = Screen.ActiveForm
    TempVars(“tv_CurrentForm”).Value = frmCurrentForm.Name

    But if I do this access complains “you entered an expression that requires the form to be the active window” I thought it was the active window – nothing else is loaded – but there are a couple of macros that run on Load and on Current, does this mean the form itself is not active?

    The reason for using a tv is that I want to then have some generic routines that will use this – for example to close a form.

    I’m sure it’s simple but I can’t fix it………… liz

    Viewing 5 reply threads
    Author
    Replies
    • #1248044

      The Load Event (and also the Open event) are too early. The form has not yet become the Active Form.

      By the time the Activate event happens it is Active.

      But you can just use

      TempVars(“tv_CurrentForm”).Value = me.Name

      in any event.

    • #1248047

      Dera John,

      Thank you, easy when you know how! And, good to understand the event ordering a little more.

      thank you……………… liz

    • #1248189

      I have been playing with this and couldn’t see where I would put my function to get the CurrentForm name, I wanted to do it when the form is loaded but as John explained the events I had used are too early. This is what I understand about the event sequence on opening a form:

        [*]Open – “This doesn’t occur if you move to a form that is already open. A subform & its records are loaded BEFORE the main form, this means the events also occur before the main form’s events”
        [*]Load
        [*]Resize
        [*]Activate – does not occur for a sub-form
        [*]GotFocus -if no active controls
        [*]Current

      I have tried using my CurrentForm() function with each of these events and it always complains about no active form – or does nothing in the case of GotFocus, even Current, although it’s after Activate which I thought should work.

      So my current solution is to use the timer events, I run the function after 1 millisecond, but I think this means the event is running persistently? Can I force it to run once only? And, Is there a better, or different,way of achieving this?

      Thank you…………………….. liz

    • #1248191

      Liz,

      Another approach you may want to consider is to code your generic routines to accept an argument of the current form. From your post and the variable tv_CurrentForm I’m making the assumption that your generic routines will be called from the current form? If this is the case you can just call them as:

      Code:
      bRtdValue =MyCloseRoutine(Me.Name)

      etc.
      The code would be:

      Code:
      Function MyCloseRoutine(vfrmName as Variant) as Boolean.
      ...Your code here...
      End Function

      This would be similar to the calling sequence I used on the post to SetFocus to the first available field on a form.

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1248237

      Oh Yes, a good idea I shall investigate more, thank you!

      • #1248537

        Oh Yes, a good idea I shall investigate more, thank you!

        This one came from Hans:

        CodeContextObject is a handy and powerful variable because it lets you write code in a standard module that can be called from any form (or report) without having to pass a reference to the form (or report) or its name.

        • #1248545

          This one came from Hans:

          CodeContextObject is a handy and powerful variable because it lets you write code in a standard module that can be called from any form (or report) without having to pass a reference to the form (or report) or its name.

          Patt,

          Nice, here’s the MSDN page on the CodeContextObject.

          May the Forces of good computing be with you!

          RG

          PowerShell & VBA Rule!
          Computer Specs

    • #1248513

      if you put

      Code:
      MsgBox Me.Name

      in the open event it will display the form name

      so as RetiredGeek suggest, passing either the name of the form from the Open event

      Code:
      If DoWhatever(Me.Name) Then

      to

      Code:
      Function DoWhatever(strFormName as String) as Boolean.
      ...Your code here...
      End Function

      or passing the form object itself

      Code:
      If DoWhatever(Me) Then
      Code:
      Function MyCloseRoutine(frm as Form) as Boolean.
      Dim strFormName as string
        strFormName = frm.Name
      End Function
    Viewing 5 reply threads
    Reply To: Getting a Form Name

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: