Consulting

Results 1 to 3 of 3

Thread: setfocus not working

  1. #1
    VBAX Newbie
    Joined
    Feb 2018
    Posts
    2
    Location

    setfocus not working

    After scouring the web for reasons why textbox.setfocus was not working I tried this and it worked. I am posting here to reach a wider audience. This is done in Solidworks and some of the events are not available that were used in other solutions. I tried KeyUp but that did not help. I attached the form and copied the code below, including remarked 'code that did not work:


    Private Sub TextBoxPartNumber_KeyDown(ByVal KeyCode As msforms.ReturnInteger, ByVal Shift As Integer)
    If KeyCode = vbKeyReturn Then 'vbKeyReturn=13
     
        Dim bl As Boolean
        Dim Item1(0 To 4) As Variant
        Dim CFList As Control ' has list of stock numbers to process
        Set CFList = Me.ListBoxPartsList
        Item1(0) = Me.TextBoxPartNumber.Value
        If Item1(0) = "" Then
            CommandButtonMakePdfs.SetFocus
            SendKeys "[ENTER]" 'start making files if user hits enter on empty text box
        Else
            bl = AddtoSelectList(Item1, CFList) ' search list, don't add if it's already there
            If bl = True Then '161230'
                CFList.Selected(CFList.ListCount - 1) = True '180209'
            End If
        End If
        
    Exit1:
        'Me.TextBoxPartNumber.SetFocus
        KeyCode = 0 '180209'
        Me.TextBoxPartNumber.Value = ""
        'Me.TextBoxPartNumber.SelStart = 0
        'SendKeys "{TAB}{TAB}" ' get back to the textbox
        On Error Resume Next
        Erase Item1
        Set CFList = Nothing
    End If
    
    End Sub
    edit: The user types text then presses enter to add the line item to the list box below and continues typing a new line item without having to click back into the box.
    Attached Files Attached Files
    Last edited by SamT; 02-09-2018 at 11:37 AM. Reason: Reason for needing setfocus

  2. #2
    Moderator VBAX Wizard SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    6,964
    Location
    This should let the User press Enter once to add to the other Control, and twice to actually exit this control

    Option Explicit
    
    Dim HasChanges  As Boolean
    Private Sub TextBoxPartNumber_Change()
      HasChanges = True
    End Sub

    Private Sub TextBoxPartNumber_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    'Triggered when Enter is pressed
      If Not HasChanged Then 'No text was added before this Enter press
        Cancel = False 'Handle Enter press normally
        Exit Sub
      End If
    
    'If HasChanged then new text was added before this Enter press      
      Cancel = True 'don't follow normal SOP when pressing Enter
          
      'Edit to add new item as needed
      bl = AddtoSelectList(Item1, CFList) ' search list, don't add if it's already there
      '
      '
      '
      
      HasChanges = False 'Reset Boolean after second Enter press
    
    End Sub
    I always expect the student to do their homework and find all the errrors I leeve in.

    Please take the time to read the Forum FAQ

  3. #3
    VBAX Newbie
    Joined
    Feb 2018
    Posts
    2
    Location
    Thank you SamT! But for solidworks 2016 vba, that exit event is not available.

    BTW, the previously attached form has a lot more code in itthan I posted in the message.

Tags for this Thread

Posting Permissions

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