Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2008
    Location
    All over, Now in Missouri
    Posts
    73

    Unanswered: SetFocus on VBKeyReturn not working

    My users input a value into a txtSearch box, then have an option to press "Enter" or press the "Search" Button to invoke the code.

    Problem! I cannot get the focus back to the txtSearch Box once the "enter key is pressed", I tried:

    - If KeyCode = 13 instead of VBEnterKey
    - Setting KeyCode = 0
    - Setting focus to another txtBox and then reset the focus
    - Requery, Refresh, Rrepaint

    Any Thoughts?

    My Code,,,,,,


    ' If they press enter than call search function
    Private Sub txtsearch_KeyDown(KeyCode As Integer, Shift As Integer)
    If KeyCode = vbKeyReturn Then
    Call cmdSearch_ID1
    End If
    End Sub



    ' If they press enter than call search function
    Private Sub cmdSearch_ID_Click()
    Call cmdSearch_ID1
    End Sub




    Public Sub cmdSearch_ID1()
    Me.cmdsearch_SSN.SetFocus
    Dim Stuidsearch As String
    Dim strSearch As String

    'Check txtSearch for Null value or Nill Entry first.
    If IsNull(Me![txtsearch]) Or (Me![txtsearch]) = "" Then
    MsgBox "Please enter a value!", vbOKOnly, "Invalid Search Criterion!"
    Me.txtsearch.SetFocus
    Exit Sub
    End If
    '---------------------------------------------------------------

    'Performs the search using value entered into txtSearch
    'and evaluates this against values in strStudentID

    DoCmd.ShowAllRecords
    DoCmd.GoToControl "StuID"

    Dim idnum As String
    idnum = Left([txtsearch], 5) & "-" & Right([txtsearch], 4)
    DoCmd.FindRecord idnum

    If (Me![txtsearch]) <> StuID Then
    idnum = (Me![txtsearch])
    DoCmd.FindRecord idnum
    End If

    ' Stuid.SetFocus
    Stuidsearch = Me.StuID.Text
    txtsearch.SetFocus
    strSearch = txtsearch.Text

    'If matching record found sets focus in student id and shows msgbox
    'and clears search control

    If strSearch = Stuidsearch Then
    ' MsgBox "Match Found For: " & strSearch, , "Congratulations!"
    Beep
    StuID.SetFocus
    txtsearch = Null

    ' If value not found sets focus back to txtSearch and shows msgbox
    Else
    MsgBox "Match Not Found For: " & strSearch & " - Please Try Again.", _
    , "Invalid Search Criterion!"
    DoCmd.GoToRecord , , acNewRec
    Beep
    txtsearch = Null
    txtsearch.SetFocus

    End If
    End Sub
    We tend to look at Linear paths which can lead us to a path of resistance!

  2. #2
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Assuming that the focus is set correctly when clicking on your 'search' command button, in Design View select the command button then go to Properties-Other and set the Default Property to Yes. This tells Access to execute the code behind this button any time the <Enter> key is depressed.

    Linq ;0)>
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  3. #3
    Join Date
    Apr 2008
    Location
    All over, Now in Missouri
    Posts
    73
    Got it, burned a few more brain cells on this one.

    Went to Properties-Other
    Set Auto Tab to Yes
    - when the number is entered, it auto tabs to the cmd button, press enter - good to go.

    thanks so much,

    Peter
    We tend to look at Linear paths which can lead us to a path of resistance!

Posting Permissions

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