Results 1 to 2 of 2

Thread: Set Focus

  1. #1
    Join Date
    Apr 2007
    Stalybridge, Manchester

    Unanswered: Set Focus

    I have a search form that has this code behind below... The problem I'm having is that once I select an option from my ComboBox and an error pops up...

    Run-time Error 2185
    you cant reference a property or method for a control unless the control has the focus.

    I thought in my code I had set the focus on the ComboBox, hovering over the debug line it points to this
    sQRY = _
                    "SELECT * " & vbCrLf & _
                    "FROM jez.HaH_ReferralReason " & vbCrLf & _
                    "WHERE jez.HaH_ReferralReason." & Me.cboSearchOn.Text & " LIKE '*" & Me.txtInputSearch.Text & "*' "
    Highlighting the section it shows the error message over the part Me.txtInpustSearch.text. but I've been unable to enter anything in that TextBox as the error has popped up before the opportunity to do so.

    Private Sub txtInputSearch_Change()
        Dim cnn As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim sQRY As String
    '    On Error GoTo Err
        Set cnn = New ADODB.Connection
        Set rs = New ADODB.Recordset
        cnn.Open "Provider=sqloledb;Data Source=CISSQL1;Initial Catalog=CORPINFO;Integrated Security=SSPI;"
        If Not IsNull(cboSearchOn) Then
            If Not IsNull(Me.txtInputSearch.Text) Then
            sQRY = _
                    "SELECT * " & vbCrLf & _
                    "FROM jez.HaH_ReferralReason " & vbCrLf & _
                    "WHERE jez.HaH_ReferralReason." & Me.cboSearchOn.Text & " LIKE '*" & Me.txtInputSearch.Text & "*' "
                rs.CursorLocation = adUseClient
                rs.Open sQRY, cnn, adOpenForwardOnly, adLockReadOnly
                Me.lstSearch.RowSource = sQRY
            End If
        End If
        Set rs = Nothing
        Set cnn = Nothing
        Exit Sub
    '    basError.LogError VBA.Err, VBA.Error$, "Form_frmTest- txtInputSearch_Change()"
    End Sub
    Where am I going wrong?

  2. #2
    Join Date
    Sep 2002
    South Wales
    Use a temporary control to get the value you require as you may not be able to set the focus to that control to get its value;

    Dim tmpControl As Control
    Set tmpControl = Me.txtInputSearchOrWhatever

    Any probs, drop me a line.
    Windows Server 2003-8 / Terminal Services / SQL 2000 / Access 2003 / Office 2003-7 / Exchange 2003-7 / Blackberry Enterprise Server / AutoCAD / Lambert And Butler / Red Bull

Posting Permissions

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