Run-time error '2185' You can't reference a property or method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sep410
    New Member
    • Jul 2008
    • 77

    #1

    Run-time error '2185' You can't reference a property or method

    Hi all,

    Here is my code:
    Code:
    Private Sub Command12_Click()
    Dim strSql As String
           strSql = "Delete from tbl_city where CityId=" & Val(Me!txtEmail.Text) & ";"
        Set cn = CurrentProject.Connection
        cn.Execute strSql
        
        Debug.Print "MyTableView created"
        Set cn = Nothing
    End Sub
    I get the Run-time error '2185' when I run it.


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

    If I put this line after defending of the strsql everything work well but I don't want to have this line in my code.
    Code:
    ' Me!txtEmail.SetFocus
    There are some SQL statements which has 2 conditions I can't set the focus for both of them at the same time.
  • missinglinq
    Recognized Expert Specialist
    • Nov 2006
    • 3533

    #2
    In Access VBA the Text property is only available when the control has focus, as you've seen .The Text property is seldom used in VBA. Use the .Value Property instead. It doesn't require focus to work. And since it's the Default Property for a textbox/combobox, you don't actually have to include it!

    txtEmail

    is the same as

    txtEmail.Value

    Linq ;0)>

    Comment

    • Sep410
      New Member
      • Jul 2008
      • 77

      #3
      Thank you.It is working now.

      Comment

      • missinglinq
        Recognized Expert Specialist
        • Nov 2006
        • 3533

        #4
        Glad you got it working!

        Linq ;0)>

        Comment

        • sirdevo
          New Member
          • Nov 2009
          • 9

          #5
          Thank you, this helped me too

          I had a couple of textboxes I needed to change the .text property on, but when I would setfocus to update the textbox it would cause problems by activating all my events I had placed on those textboxes. It allows me to change the values without causing events to trigger.

          Sir Devo

          Comment

          • missinglinq
            Recognized Expert Specialist
            • Nov 2006
            • 3533

            #6
            As I said previously, there is seldom any reason to use the Text Property in Access VBA, and it certainly shouldn't be used to set value of a textbox! If you need to change the value of a textbox thru code, use the Value Property, which doesn't require setting the focus to the textbox.

            Me.TextboxName.Value = "Whatever"

            Since the Value Property is the default property for textboxes, comboboxes, etc., you can simply use

            Me.TextboxName = "Whatever"

            omitting the .Value.

            Welcome to Bytes!

            Linq ;0)>

            Comment

            Working...