Results 1 to 14 of 14
  1. #1
    Join Date
    Feb 2003
    Posts
    65

    Question Unanswered: How do you get the name of a control?

    I am using code OnChange event of a control, but within the code I want to refer to the name of the same control.

    Example: If on a subform I want to refer to the name of a control called "Price" how do I do this with code in the OnChange event of "Price".

    I tried something like this:

    Me.ActiveControl - gives me the number of the control, not the name

    Me.Name - gives me the name of the subform

    Me.CurrentRecord - gives me the number

    I need to print the name, and idealy the value of the control. Something that will produce the following results:

    ?Me.something-here.name = "Price"
    ?Me.something.value = 25.04 (for example)

    Thanks for your help guys.
    Jazz

  2. #2
    Join Date
    Dec 2001
    Posts
    79
    Screen.ActiveControl

    ...should do it for you.

    Hope this helps,
    Peter De Baets
    Peter's Software - Microsoft Access Tools for Developers
    http://www.peterssoftware.com

  3. #3
    Join Date
    Feb 2003
    Posts
    65
    Originally posted by pdebaets
    Screen.ActiveControl

    ...should do it for you.

    Hope this helps,
    Peter, thanks for the idea. Screen.ActiveControl gives me the same result as Me.ActiveControl -- 19, which is probably the 19th control in the DB. I need the name of the control.

    Anything else to try?
    Jazz

  4. #4
    Join Date
    Mar 2003
    Location
    London
    Posts
    40
    Originally posted by jazz novice
    Peter, thanks for the idea. Screen.ActiveControl gives me the same result as Me.ActiveControl -- 19, which is probably the 19th control in the DB. I need the name of the control.

    Anything else to try?
    Jazz
    Have you tried me.activecontrol.name and me.activecontol.value?

  5. #5
    Join Date
    Feb 2003
    Posts
    65
    Yes, I have tried, however after typing Me.ActiveControl. --- Name is not an option.

    I wonder if this is problem of my DB refferences. I have DAO selected...

  6. #6
    Join Date
    Mar 2003
    Location
    London
    Posts
    40
    Originally posted by jazz novice
    Yes, I have tried, however after typing Me.ActiveControl. --- Name is not an option.

    I wonder if this is problem of my DB refferences. I have DAO selected...

    Name is not an option, because ActiveControl can be of any type and name is not necessarily a valid property.

    But if you just type me.activecontrol.name or me.activecontol.value, it will work. (at least for a text box in Access 2000)

    Alternatively you can use me.activecontrol.properties("Name")

  7. #7
    Join Date
    Feb 2003
    Posts
    65
    Originally posted by yk58301
    Name is not an option, because ActiveControl can be of any type and name is not necessarily a valid property.

    But if you just type me.activecontrol.name or me.activecontol.value, it will work. (at least for a text box in Access 2000)

    Alternatively you can use me.activecontrol.properties("Name")
    Bingo!!! You are right man! Thanks a million!

    At least Me.ActiveControl.Properties("Name") works. However Me.ActiveControl.Properties(Value) gives me the same result - the name, not the value of the control.

    Any other ideas?
    Jazz

  8. #8
    Join Date
    Mar 2003
    Location
    London
    Posts
    40
    Originally posted by jazz novice
    Bingo!!! You are right man! Thanks a million!

    At least Me.ActiveControl.Properties("Name") works. However Me.ActiveControl.Properties(Value) gives me the same result - the name, not the value of the control.

    Any other ideas?
    Jazz
    if you don't want to use me.activecontrol.name and me.activecontrol.value, you can use complex syntax
    Me.ActiveControl.Properties("Name") and
    Me.Controls(Me.ActiveControl.Properties("Name")).V alue

  9. #9
    Join Date
    Feb 2003
    Posts
    65
    Originally posted by yk58301
    if you don't want to use me.activecontrol.name and me.activecontrol.value, you can use complex syntax
    Me.ActiveControl.Properties("Name") and
    Me.Controls(Me.ActiveControl.Properties("Name")).V alue
    Your suggested syntax works fine, but I get only the name of the control (example: "price"). Now I need the value of control "price". Is it $5.12, or $432.04. How do I extract this numeric value?

    Thank a lot for your help.
    Jazz

  10. #10
    Join Date
    Mar 2003
    Location
    London
    Posts
    40
    Originally posted by jazz novice
    Your suggested syntax works fine, but I get only the name of the control (example: "price"). Now I need the value of control "price". Is it $5.12, or $432.04. How do I extract this numeric value?

    Thank a lot for your help.
    Jazz
    I set up a form with a textbox formatted as currency to replicate your example, i.e. when I type 35.12 it displays 35.12

    As I said in my previous reply:
    Both expressions

    Me.Controls(Me.ActiveControl.Properties("Name")).V alue and
    me.activecontol.value

    return 35.12 after typing 35.12

    BTW, you are better off using AfterUpdate event, otherwise your code execute every time you type a number in your Price field (5 times for 35.12)

  11. #11
    Join Date
    Feb 2003
    Posts
    65
    OK, here is my code. It runs only OnChange event and intends to log record changes in a memo field ("Note").

    ---------CODE BEGINS--------------

    Private Sub Discount_Change()

    Dim db As Database
    Dim msg, response As String

    msg = "You are about to change this record. Please enter the reason for this."
    response = InputBox(msg, "Changing record")

    Me!Note = Me!Note & Now() & ": -> " & Me.ActiveControl.Properties("Name") & _
    " changed from " & Me.Controls(Me.ActiveControl.Properties("Name")).V alue & " Reason: " & response & vbCrLf
    Debug.Print "name is: " & Me.ActiveControl.Properties("Name")
    Debug.Print Me.Controls(Me.ActiveControl.Properties("Name")).V alue
    End Sub

    ------------CODE END------------------

    The output from the Debug.Print is:

    name is: Discount
    5

    The name of the control is correct. "Discount" is a combo, and the value should be "early bird". The Debug.Print Me.Controls(Me.ActiveControl.Properties("Name")).V alue
    though outputs -- 5.

    I hope this will make it more clear for you.

    Jazz

  12. #12
    Join Date
    Feb 2003
    Posts
    65
    It's all OK. It was all my fault -- trying to get the value of one control, when I was actually reffering to another control.

    Yes, Me.ActiveControl.Properties(value) works fine. I can continue ahead now. Yahoo!

    Thanks you very much for your help!

    Jazz

  13. #13
    Join Date
    Mar 2003
    Location
    London
    Posts
    40
    Originally posted by jazz novice
    It's all OK. It was all my fault -- trying to get the value of one control, when I was actually reffering to another control.

    Yes, Me.ActiveControl.Properties(value) works fine. I can continue ahead now. Yahoo!

    Thanks you very much for your help!

    Jazz
    BTW for combos you'll need a different syntax, i.e. to get "early bird" instead of 5 you can use

    me.activecontrol.column(1)
    or me.controls(me.activecontrol.properties("Name")).c olumn(1)

  14. #14
    Join Date
    Feb 2003
    Posts
    65
    Originally posted by yk58301
    BTW for combos you'll need a different syntax, i.e. to get "early bird" instead of 5 you can use

    me.activecontrol.column(1)
    or me.controls(me.activecontrol.properties("Name")).c olumn(1)
    Oh, re: "early bird" - it was my mistake. Actually Discount is 5, "early bird" was the DiscountType.

    So it is fine now. Thanks again. Great help!
    Jazz

Posting Permissions

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