Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check for Null entry and setfocus 1

Status
Not open for further replies.

tekila

Programmer
Apr 18, 2002
150
SG
i want to check for null entry in a combobox and display a msgbox when lostfocus and afterupdate events of the combobox and on_click event of the 'Add' control, are triggered.

This is my code for lostfocus:

Private Sub combo_LostFocus()

If IsNull([combo]) Then
If MsgBox("Is combo supposed to be Null?", vbYesNo) = vbYes Then
Me!combo = "-"
Else: Me!combo.SetFocus
Exit Sub
End If

End If

The problem is when i select 'yes' in the msgbox, Me!combo.SetFocus is not implemented, instead 'Add' control is set focus (by the way, 'Add' control is tabbed after combo in the predefined tab order).

How do i solve the problem?

End Sub

 
Well, firstly, your code has Else before the setfocus line. This means that the setfocus line is only actioned if the answer to the messagebox is NO, as the If statement tests for Yes.

However, you also seem to have a colon :)) after the Else, which desgnates it as a label. Could be this that's causing the problem. Have fun! :eek:)

Alex Middleton
 
Sorry, I mean when I select 'No' in the msgbox, I want 'Me!combo.SetFocus' to work.

And I couldn't get rid of the :)) cos it appears automatically when I write the code as :

Else Me!combo.SetFocus

What can I do about it?

 
It shouldn't add the colon automatically - I've never come across that before. An If..Then..Else statement should be written as follows:

If (Condition) Then
(Code.....)
Else
(Code....)
End If

The colon designates a label (used in Goto statements) and therefore the line will not be recognised as an Else statement in the If...Then...Else construct. Have fun! :eek:)

Alex Middleton
 
I have the same problem. To work around it I put the code in the on enter property of Add.

It will then allow you to setfocus to the combo box.

For some reason it does not allow you to set the focus to itself on conditions.
 
I've already included the code in the on_click property of Add but I also need to include the same code in the afterupdate property of the combo box. If not, the user may enter a value and decide to delete it, and when he hits the Add command, the record is saved without displaying the msgbox.

Can somebody help me with this?
 
So, surely code in the onclick event of the Add button should be able to trap this and deal accordingly. I presume you are saying that you have tried this and it still doesn't work. Forgive me if it seems obvious and my comments are not helping much, but this SHOULD work - however sometimes these things don't work when they should and drive you nuts - I know the feeling. I regret I haven't had time to check them for myself so cannot advise further at this stage. If I get a chance I will try it for myself but, meantime, I'm sure someone else has come across this and will be able to help. Have fun! :eek:)

Alex Middleton
 
I've already included the code in the on_click property of Add but I also need to include the same code in the afterupdate property of the combo box. If not, the user may enter

You can reference a chunk of working code without having to "include the same code" somewhere else. This is a computer. You should never have to repeat yourself...

On Some_event_Click()
if XXX = YYY then
call Button1_Click()
else
do something here...

In other words, you can call the code in the BUTTON1_CLICK event from some other event or step in the same module.

Got it?






Jim Hare
"Remember, you're unique - just like everyone else"
 
I don't think I can do that,Jim cos there're other codes in on_click event of Add command. Pls look at my codes and advise.


Private Sub Add_Click()

If IsNull(Me!combo1) Then
MsgBox "combo1 is Null. Please select/enter a value. "
Me!combo1.SetFocus
Exit Sub
End If

If IsNull(Me!combo2) Then
MsgBox "combo2 is Null. Please select/enter a value. "
Me!combo2.SetFocus
Exit Sub
End If

If IsNull([combo3]) Then
If MsgBox("Is combo3 supposed to be Null?", vbYesNo) = vbYes Then
Me!Description = "-"
Else
Me!combo3.SetFocus
Exit Sub
End If
End If



On Error GoTo Err_Add_Click

DoCmd.GoToRecord , , acNewRec

Exit_Add_Click:
Exit Sub

Err_Add_Click:
MsgBox Err.Description
Resume Exit_Add_Click

End Sub

____________________________________________________________

Private Sub combo3_AfterUpdate()

If IsNull([combo3]) Then
If MsgBox("Is combo3 supposed to be Null?", vbYesNo) = vbYes Then
Me!combo3 = "-"
Else
Me!combo3.SetFocus
Exit Sub
End If


Else
Me!combo3 = "/" & UCase(Me!combo3)
End If

End Sub

____________________________________________________________

The code in green is workable but not the one in red.

 
Hi!

You're allowing Access to get too far. Use this code in the before update event of the combo box:

Private Sub combo_BeforeUpdate(Cancel As Integer)

If IsNull([combo]) Then
If MsgBox("Is combo supposed to be Null?", vbYesNo) = vbYes Then
Me!combo = "-"
Else
Cancel = -1
End If
End If

End Sub

hth
Jeff Bridgham
bridgham@purdue.edu
 
Jeff, if I include your code as well, there'll be a run-time error when I hit Add command, though setfocus works for combo3's afterupdate event.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top