Runtime Error 2185

I get run time error 2185 , "You cannot reference a property or method for a control unless the control has focus ...".

This is my code that I use.

 Private Sub Command5_Click() Dim cardno As Integer cardno = cardnumber.Text DoCmd.OpenForm "search_card_number", acNormal, , WHERE & cardno = [Account Number] End Sub 
+6
source share
3 answers

A reference to the .Text property of the control requires focus. Just drop it and it should work (default is .Value )

OR

Try entering the SetFocus method as described in Access, i.e.

  Private Sub Command5_Click() Dim cardno As Integer cardnumber.SetFocus <-------Use this line to set the focus cardno = cardnumber.Text DoCmd.OpenForm "search_card_number", acNormal, , WHERE & cardno = [Account Number] End Sub 
+8
source

This runtime error means You can't reference a property or method for a control unless the control has the focus.

You can use .Text when the control has focus.

+2
source

My solution was to check if the control had focus before updating the control property,

Here I update the search in the combo box in wild card search mode

 Private Sub Combo_Change() 'Note control passed to after update sub, then returns to change sub where error occurs 'Combo is the name of your combo box If (Combo Is Me.ActiveControl) Then Me.Combo.RowSource = _ "SELECT [CCL sites v Contract No].ContractID, [CCL Sites].[Site Address], [CCL sites v Contract No].[Contract No] " & _ "FROM [CCL sites v Contract No] INNER JOIN [CCL Sites] ON [CCL sites v Contract No].SiteID = [CCL Sites].SiteID " & _ "WHERE [CCL Sites].[Site Address] LIKE '*" & Me.Combo.Text & "*' " & _ "ORDER BY [CCL Sites].[Site Address]" Me.Combo.Dropdown End If End Sub 
0
source

All Articles