Results 1 to 7 of 7
  1. #1
    Juan4412 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    209

    VBA to Get Value Input From Text Box

    I am attempting from a button press event to capture the values input in 3 text boxes. I have the syntax below, but I get an error of


    You can't reference a property or method for a control unless the control has the focus

    How should I capture this information?

    Code:
    Private Sub btnTest_Click()
    Dim textvalue1 As String, textvalue2 As String, textvalue3 As String
    textvalue1 = Me.txtInputOne.Text
    textvalue2 = Me.txtInputTwo.Text
    textvalue3 = Me.txtInputThree.Text
    End Sub

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Me!txtImputOne

  3. #3
    Juan4412 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Dec 2010
    Posts
    209
    Why the ! and not a . and .Text on the end?

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Here is a good explanation: https://bytes.com/topic/access/answe...-bang-notation

    I have been trying to find the object's definitions, will keep looking.

  5. #5
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    OK, here's a good explanation https://stackoverflow.com/questions/...-in-vba-access

    It seems that ".text" can only be used when the control has the focus. I would assume that ".value" is the default. Maybe someone else can provide further clarity.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    My understanding:

    There are 3 properties of a text box concerning values:
    OldValue property
    Value property
    Text property


    While the Text box control (MUST be a bound control) has the focus,
    the OldValue property contains the unedited (previous) data from a bound control.
    the Value property contains the last saved data for the control.
    the Text property contains the text data currently (ie uncommitted) in the control.

    When you move the focus to another control, the control's data is updated, and the Value property is set to this new value (the Text property).
    The Text property setting is then unavailable until the control gets the focus again.


    If you do not specify an available property of the control, the Value property is returned; Value is the control default property (not the same as the default value property).
    This is why you can use "Me.txtInputOne" to get the saved value of a control and not worry about getting the background color property or the Name property.


    As noted, the default property, Value, is available all of the time, but the Text property is only available when the control had the focus.

  7. #7
    Micron is offline Virtually Inert Person
    Windows 7 32bit Access 2007
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,737
    Why the ! and not a . and .Text on the end?
    While this Me!txtFName can work just as well as Me.txtFName, it is not correct and can lead to bugs.

    If a form has a textbox named txtFname and you mis-spell and code as Me!txtName (and there is no control named txtName on this form), your code will compile but will likely induce a run time error. If you code as Me.txtName and compile, it will fail.

    The bang operator (!) provides late bound access to the default member of an object by passing the name that follows the operator as a reference. Late bound means that the member will not be "discovered" (for lack of a better word at the moment) until code executes. Furthermore, if the reference is not a name of any member of the default, another error will likely be raised. I say likely because it's never happened to me as I only use the ! operator in a minimal number of circumstances. And no, rs!edit is not one of them.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 11
    Last Post: 10-12-2012, 08:37 AM
  2. Replies: 3
    Last Post: 09-20-2011, 11:54 AM
  3. Replies: 1
    Last Post: 09-20-2011, 05:57 AM
  4. Replies: 15
    Last Post: 04-01-2011, 11:41 AM
  5. Input data In unbound text box
    By chu3w in forum Forms
    Replies: 1
    Last Post: 04-01-2010, 10:21 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums