Results 1 to 9 of 9
  1. #1
    Join Date
    Jun 2009
    Posts
    89

    Unanswered: Does a VBA function know what form called it?

    Hi. I've got loads of code that is repeated in different forms, the only difference being the form name.

    I want to do this properly and have just one function and all forms clal that function, but I dont' want to pass the form name. Is there a variable in VBA which holds the name of the form that called a function?

  2. #2
    Join Date
    Sep 2002
    Location
    South Wales
    Posts
    580
    "I dont' want to pass the form name"

    Any good reason why? I would maybe pass the form as an object anyway?

    Public Function fnYourFunction(blah as etc....., frmCallingForm As Form)
    ...
    ...
    YourLocalVariableForName = frmCallingForm.name
    ...
    etc.

    <This no good to you?
    Windows Server 2003-8 / Terminal Services / SQL 2000 / Access 2003 / Office 2003-7 / Exchange 2003-7 / Blackberry Enterprise Server / AutoCAD / Lambert And Butler / Red Bull

  3. #3
    Join Date
    Jun 2009
    Posts
    89
    ONly reason I didn't want to do it is becuase I thought it was a hack. I take it this is the normal way to do it??

  4. #4
    Join Date
    Sep 2002
    Location
    South Wales
    Posts
    580
    Not so sure there is a 'normal way' - open to suggestions!
    Windows Server 2003-8 / Terminal Services / SQL 2000 / Access 2003 / Office 2003-7 / Exchange 2003-7 / Blackberry Enterprise Server / AutoCAD / Lambert And Butler / Red Bull

  5. #5
    Join Date
    Nov 2007
    Location
    Adelaide, South Australia
    Posts
    4,049
    Yes, kinda.

    By referencing the ActiveForm object, you can find out which form is active at the time the code runs.
    Owner and Manager of
    CypherBYTE, Microsoft Access Development Specialists.
    Microsoft Access MCP.
    And all around nice guy!


    "Heck it's something understood by accountants ... so it can't be 'that' difficult..." -- Healdem
    "...teach a man to code and he'll be frustrated for life! " -- georgev

  6. #6
    Join Date
    Sep 2002
    Location
    South Wales
    Posts
    580
    Coolio - Here's the MS reference;

    ActiveForm Property [Access 2003 VBA Language Reference]

    NB Note the caveat (may not be relevant)...

    "If a subform has the focus, ActiveForm refers to the main form. If no form or subform has the focus when you use the ActiveForm property, an error occurs."
    Windows Server 2003-8 / Terminal Services / SQL 2000 / Access 2003 / Office 2003-7 / Exchange 2003-7 / Blackberry Enterprise Server / AutoCAD / Lambert And Butler / Red Bull

  7. #7
    Join Date
    Apr 2013
    Posts
    9

    Calling function_ simple clicking

    Have a similar problem
    I have 300 labels's on a a form that after clicking calls a function

    as I can not use a command button (hence activecontrols) and do not want to write

    call show_label("called_from_Click1") ... call show_label("called_from_Click300")

    is there a function that could work like

    function show_label
    dim calledformwhichClick as string
    calledformwhichClick = vbaXXXX (or any other method)
    thanks
    ..
    end function

  8. #8
    Join Date
    Jun 2009
    Posts
    89
    I'm not sure I understand what you're trying to do (why would you click on a label?), but i think you can do this using the "tag" property of each of the 300 objects.

    put the values you want to pass on in the Tag property. Then when a user clicks on that label/button, they can all call the same function, and the function can reference the active object's tag field using :
    Code:
    Screen.ActiveControl.Tag
    I haven't; tested the above but it should work. Or you could use the Lable's Names or Values by using .Name or .Value.

  9. #9
    Join Date
    Apr 2013
    Posts
    9

    Tag a Label

    Thx for getting back so quickly..

    When I tag a label. then write
    MsgBox Screen.ActiveControl.Tag

    it shows the tag value of an activecontrol which is a text and not a label.

    What I am trying to do is that I have many stockLocations
    When I click on this it opens another location and shows me stockdetails
    A text box has the drawback that you go into it and I can not stop the cursor going in.

    Ideally I would like to write a function

    ON CLICK =ShowMyStockDetails(thislocation.value)

    I would like to avoide writing:

    ON CLICK =ShowMyStockDetails("MyLocation1")
    ON CLICK =ShowMyStockDetails("MyLocation2") ...etc

    I just want to do it elegantly... thanks for your help

Posting Permissions

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