Results 1 to 7 of 7
  1. #1
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Control name in form (Access 2003 SP2)

    Is there a way to identify the Control Source of the control you are in while in Click event VBA code behind the control, or in the OnClick event of the form.

    In other words, i have a control named CompanyName (both Name and ControlSource are called CompanyName), i need to get the Control Name as a text value and save it in a string variable. This needs to be done in the OnClick event of the control.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Control name in form (Access 2003 SP2)

    You could use

    Private strSource As String

    ...

    Private Sub CompanyName_Click()
    strSource = Me!CompanyName.ControlSource
    End Sub

  3. #3
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Control name in form (Access 2003 SP2)

    Thanks but is there a way of doing the same thing in the OnClick event of the form.

    Say i clicked in the CompanyName control, can i get the Control Source of the control (in this case CompanyName) in the OnClick event of the form?

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Control name in form (Access 2003 SP2)

    The On Click event of the form only occurs if the user clicks in the record selector. You could use

    Screen.PreviousControl.ControlSource

  5. #5
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Control name in form (Access 2003 SP2)

    Thanks for that, i can now (with 2 clicks) identify the controlsource of the previous control that was clicked.

    This will save coding (with only one click) in all those controls i have on this form, there are about 20 controls, i'm being a bit lazy here as this is a oneoff to code all those controls once, so i may go with your original solution.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Control name in form (Access 2003 SP2)

    Another solution would be to use a function in as standard module

    Public strSource As String

    Function GetControlSource()
    strSource = Screen.ActiveControl.ControlSource
    End Function

    Select a bunch of controls on your form, and enter

    =GetControlSource()

    in the On Click box in the Properties window. This way, you don't have to create an event procedure for each individual control.

  7. #7
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    Re: Control name in form (Access 2003 SP2)

    Clever Hans, thank you.

Posting Permissions

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