I have a combo box that, when left empty, it tells the user that it is empty and sets the focus back to the combobox. I am using the code:
If Me.cmbRECEIPT_TYPE.Value = "" Or IsNull(Me.cmbRECEIPT_TYPE.Value) = True Then
MsgBox "RECEIPT TYPE can not be left blank. Please enter either SALES or EXPENSE.", vbOKOnly, "Invalid Receipt Type"
Me.cmbRECEIPT_TYPE.SetFocus
Exit Sub
Else
End If
I get the message box, but it doesn't reset the focus. I have been working for hours so maybe I am missing something simple. I have done this a 100 times in the past, but I can't frigure out what is going on today.
been there .. its complicated but bottom line microsoft managed to halt control setting focus to itself so it wouldnt go on in an infinite loop and crash .
how to achieve the goal then ? (Try to stay focused here for the upcoming illustration requires it)
- on losing focus of your specific control , direct focus to another control that does the check for you than rather embeding the check within same control.
I realize you have your solution but I just thought I throw a penny into it.
You didn't specify where you have this code located but for what is happening to you I would say you have it within the LostFocus event of the Combo Box. If your Form is bound to a Table or Query then this really isn't a good spot to use for carrying out this check. You would have to be under the assumption that all Users will Tab key their way through the Form. This is not always the case. What if Focus is never put onto the Combo Box?
If the Form is bound to a Table or Query then the best place to carry out this check (in my opinion) is within the BeforeUpdate event of the Form. In his event, you never have to worry about whether or not the User ever set Focus on the Combo Box or any Control for that matter. If data was entered anywhere within the Form the BeforeUpdate event fires when the User tries to Navigate to a different record, create a new record, or close the Form. Ideally, you would want to carry out a check on all pertinent Form Fields to ensure data was entered. The BeforeUpdate event is the place to do it. If something doesn't check out properly then you can set the Cancel argument of the event which ultimately halts the desired action of the User and leaves the Form open for further scrutiny. Conditions and Messages you apply within the BeforeUpdate event explain to the User why their particular action was halted.
If you were to apply your code to the BeforeUpdate event of the Form then is may look something like this:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Nz(Me.cmbRECEIPT_TYPE.Value, "") = "" Then
MsgBox "RECEIPT TYPE can not be left blank. Please enter either SALES or EXPENSE.", _
vbExclamation, "Invalid Receipt Type"
Cancel = True
Me.cmbRECEIPT_TYPE.SetFocus
Exit Sub
End If
End Sub
Perhaps you would want to check all the Text Boxes and Combo Boxes on the Form so as to ensure that data has been entered in all of them. If this is the case then you can use code something like this in the Form's BeforeUpdate event:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If Ctrl.ControlType = acTextBox or Ctrl.ControlType = acComboBox Then
If Nz(Ctrl.Value, "") = "" Then
MsgBox "You have not entered data into some important Fields. " & _
"Please do so now. " & vbCr & "The cursor will be set upon the Field" & _
" which requires attention.", vbExclamation, "Insufficent Data Entry"
Cancel = True
Ctrl.SetFocus
Exit For
End If
End if
Next Ctrl
End Sub
Perhaps you would want to check only certain Fields within the Form rather than all of them. If this is the case then you would add similar text (let's say: CheckMe) to the Tag property of each Control you want checked. You might use Code something like this:
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
Dim Ctrl As Control
For Each Ctrl In Me.Controls
If Ctrl.Tag = "CheckMe" Then
If Nz(Ctrl.Value, "") = "" Then
MsgBox "You have not entered data into some important Fields. " & _
"Please do so now. " & vbCr & "The cursor will be set upon the Field" & _
" which requires attention.", vbExclamation, "Insufficent Data Entry"
Cancel = True
Ctrl.SetFocus
Exit For
End If
End if
Next Ctrl
End Sub
Remember....for the code above to Work in the Form's BeforeUpdate event, the Form must be bound to a Table or Query and to work properly, the Controls being checked should be bound to Fields contained within that Table or Query. You can get away with only one Control in Form bound to a Field in Table but you may get some undesirable results.
Anyways...if you are determined to continue with what you have now, you can keep the code right where you have it. Just set Focus to another control and then back to your Combo Box Control. Its nice sometimes t keep things where you know you can find them:
Code:
Private Sub cmbRECEIPT_TYPE_LostFocus()
If Nz(Me.cmbRECEIPT_TYPE.Value, "") = "" Then
MsgBox "RECEIPT TYPE can not be left blank. Please enter either SALES or EXPENSE.", _
vbExclamation + vbOKOnly, "Invalid Receipt Type"
Me.[COLOR="Red"][I]SomeOtherControlOnForm[/I][/COLOR].SetFocus
Me.cmbRECEIPT_TYPE.SetFocus
End If
End Sub