Set Focus Problem with Userform Textbox after Data Entry

NorthbyNorthwest

Board Regular
Joined
Oct 27, 2013
Messages
173
Office Version
  1. 365
Hi, everyone. I am having trouble with code for checking if a textbox is empty. The empty textbox triggers a message to enter info in textbox. It then returns me the first page in my multipage form and sets focus to the textbox. The trouble is once I actually enter info in the textbox I cannot get out of the textbox. Doesn't matter if I hit tab or enter or use mouse. The cursor remains in the textbox, and I keep getting message that textbox is empty. Below are to to pieces of code I've tried with the same results. Help!

VBA Code:
If Me.txtID = vbNullString And Me.cboAuditGrp = "Unit 1" Or Me.cboAuditGrp = "Unit 3" And Me.cboReviewStatus = "Routine" Or Me.cboReviewStatus = "Makeup" Or Me.cboReviewStatus = "Double" Or Me.cboReviewStatus = "OJT (feedback only)" Or Me.cboReviewStatus = "Feedback" Then
MsgBox "Please enter the audit's ID number.", vbOKOnly + vbInformation, "Missing ID"
Me.MultiPage1.value = 0
Me.txtID.SetFocus
Exit Sub
End If

Above code is from Add button sub routine.

I also tried code below with Exit and BeforeUpdate events. Same results as above. What am I doing wrong?

Code:
Private Sub txtID_Exit(ByVal Cancel As MSForms.ReturnBoolean)
If Trim(txtID.value & vbNullString) = vbNullString And cboAuditGrp = "Unit 1" Or cboAuditGrp = "Unit 3" And cboReviewStatus = "Routine" Or cboReviewStatus = "Makeup" Or cboReviewStatus = "Double" Or cboReviewStatus = "OJT (feedback only)" Or cboReviewStatus = "Feedback" And Me.Visible Then
    MsgBox "Please enter the audit's ID number."
    Cancel = True
    MultiPage1.value = 0
    txtID.SetFocus
Else
    'do nothing
End If
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
This actually works:

VBA Code:
Private Sub txtID_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If txtID.Value = "" Then
    MsgBox "Please enter the audit's ID number."
    Cancel = True
    MultiPage1.Value = 0
  Else
    'do nothing
  End If
End Sub


I don't understand how matches should be validated, but I guess it should be something like this:
(Check each validation in parentheses)

Rich (BB code):
Private Sub txtID_Exit(ByVal Cancel As MSForms.ReturnBoolean)
  If txtID.Value = "" And _
     (cboAuditGrp = "Unit 1" Or cboAuditGrp = "Unit 3") And _
     (cboReviewStatus = "Routine" Or cboReviewStatus = "Makeup" Or _
      cboReviewStatus = "Double" Or cboReviewStatus = "OJT (feedback only)" Or _
      cboReviewStatus = "Feedback") And _
      Me.Visible Then
      
    MsgBox "Please enter the audit's ID number."
    Cancel = True
    MultiPage1.Value = 0
    
  Else
    'do nothing
  End If
End Sub
 
Upvote 0
Solution
Thank you so much for responding. You have the idea right. My code works like it should in that it takes the user back to the multipage control. But it's after the user enters an ID number that things go awry. Instead of accepting the ID entered and moving on (which is what I want), the user gets the "Please enter the audit's ID number." message again. And the cursor now appears to the right of the user ID entered. It's as if it does not recognize that the textbox is no longer empty. I am at loss as to why.
 
Upvote 0
Did you try my code?

What are the matches that need to be checked and in what order?
 
Upvote 0
I tried both the scaled down sub routine you provided and the matches. The sub routine that tests if the textbox is empty and nothing else triggered the message after I entered data in textbox. The full sub routine (FYI: Your use of brackets made perfect sense) accepted the audit even though the textbox was empty. No "Please enter audit's ID number." It just added audit to worksheet.
 
Upvote 0
I don't understand what the validations are.
You must expose how the validations should be verified, as I asked you in posts #2, #4 and now in this post.

Share your book on google drive.
And it explains step by step what I should put in each textbox and combobox and what you expect as a result.
If you do not explain what you need, we will hardly be able to understand you.

You could upload a copy of your file to a free site such google drive. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. If the workbook contains confidential information, you could replace it with generic data.
 
Upvote 0
Hi, Dante. Sorry I wasn't able to respond more quickly. My workbook is so large it was difficult to sanitize. I tried anyway. I also tried saving it to google drive. Not sure I did it correctly. None of my code transferred. So, I went back to the drawing board. I examined my code again and found the flaw in it that prevented the code you suggested from working. Thanks so much for the assist. Appreciate your desire to help me and others who struggle with coding. Have a great day.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,260
Members
452,627
Latest member
KitkatToby

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top