Results 1 to 9 of 9
  1. #1
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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

  2. #2
    Super Moderator
    Join Date
    Jun 2002
    Location
    Mt Macedon, Victoria, Australia
    Posts
    3,993
    Thanks
    1
    Thanked 45 Times in 44 Posts
    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.
    Regards
    John



  3. #3
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Dera John,

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

    thank you.................. liz

  4. #4
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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

  5. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    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!

    My Systems: Desktop Specs
    Laptop Specs

  6. #6
    4 Star Lounger
    Join Date
    Nov 2002
    Location
    London, Gtr London, England
    Posts
    496
    Thanks
    8
    Thanked 0 Times in 0 Posts
    Oh Yes, a good idea I shall investigate more, thank you!

  7. #7
    3 Star Lounger
    Join Date
    Aug 2002
    Location
    Brisbane, Queensland, Australia
    Posts
    387
    Thanks
    0
    Thanked 1 Time in 1 Post
    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

  8. #8
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts
    Quote Originally Posted by lizat View Post
    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.

  9. #9
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Quote Originally Posted by patt View Post
    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!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •