• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

textbox.setfocus is executing textbox_exit event twice|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / textbox.setfocus is executing textbox_exit event twice|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & Macrostextbox.setfocus is executing textb…
sp_PrintTopic sp_TopicIcon
textbox.setfocus is executing textbox_exit event twice
Avatar
raj raj

New Member
Members
Level 0
Forum Posts: 2
Member Since:
May 28, 2021
sp_UserOfflineSmall Offline
1
May 28, 2021 - 7:37 pm
sp_Permalink sp_Print

i have 3 checkbox,1 listbox and 3 textbox with values as 10. Iam adding some values based on check box1.value=true (i.e) AA,ABand ACto the listbox .
Iam adding some values based on check box2.value=true (i.e) BA,BBand BCto the same listbox .
Iam adding some values based on check box3.value=true (i.e) CA,CBand CCto the listbox .
if I select an item in the listbox(AA,AB,AC) and if the textbox1.text is >10 or null then I have to set the focus(text1.setfocus) in the listbox_change event
Private Sub ListBox1_Change()
For i = 0 To ListBox1.ListCount - 1

If ListBox1.Selected(i) Then
if listbox1.list(i)="AA" then

If (CheckBox1.Value = True) and CInt(TextBox1.Text) >= 10 Then

TextBox1.SetFocus

End If

end if
if listbox1.list(i)="BA" then

If (CheckBox2.Value = True) and CInt(TextBox2.Text) >= 10 Then

TextBox2.SetFocus

End If
end if

end if
next I
end sub

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Cancel = False
If (CheckBox1.Value = True) Then

If (CInt(TextBox1.Text) >= 10 Or TextBox1.Text = Null) = True Then
MsgBox "PLEASE CHK THE value(<10) "
Cancel = True
TextBox1.SetFocus
Else
Cancel = False
Exit Sub
End If
End If
End Sub
in this code when I select AA in the Listbox1 I am getting the msg MsgBox "PLEASE CHK THE value(<10) " and the selected item in the listbox gets deselected and the focus goes to textbox1.this happens for the first chkbox other chkbox and txtbox works fine.
Pls Help

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
June 1, 2021 - 3:58 am
sp_Permalink sp_Print

Hi Raj,

At the top of the form module, add this variable:
Private EnableFormEvents as Boolean

 

Changes to:

Private Sub ListBox1_Change()

EnableFormEvents=False

' your code

EnableFormEvents=True

End Sub

 

Changes to:

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)

if EnableFormEvents=False then Exit Sub

'your code

End sub

Basically, when you set focus on a textbox and the focus was on the other textbox, the form needs to Exit from one textbox to focus on the other box, so you need to disable form events.

Avatar
raj raj

New Member
Members
Level 0
Forum Posts: 2
Member Since:
May 28, 2021
sp_UserOfflineSmall Offline
3
June 2, 2021 - 3:03 pm
sp_Permalink sp_Print

hai,

Iam not getting the error, but textexit event code is not doing its purpose(i.e) if I select a list in the listbox then the particular textbox value should be reduced from 10. if the value is 10 it should display the msgbox

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: baber Tufail
Guest(s) 9
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
drsven
Annie Witbrod
wahab tunde
Cong Le Duc
Faisal Bashir
Ivica Cvetkovski
Blaine Cox
Shankar Srinivasan
riyepa fdgf
Hannah Cave
Forum Stats:
Groups: 3
Forums: 24
Topics: 6205
Posts: 27211

 

Member Stats:
Guest Posters: 49
Members: 31880
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.