PDA

View Full Version : [SOLVED:] setfocus not working



ScoobyV
02-09-2018, 09:54 AM
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.

SamT
02-09-2018, 12:00 PM
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

ScoobyV
02-09-2018, 12:23 PM
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.