OnExit driving me crazy

J

jlute

The following is driving me crazy. The first half of the code is fine
but the ElseIf isn't working. It doesn't matter if Percent IsNull or
Null - I can exit either way.

I've tried several things but can't get it.

Can anyone please give me a hand?

Thanks!

Private Sub Where_Exit(Cancel As Integer)
If IsNull(Me!Where) = False Then
If (Me!Where = "Used") _
And Len(Trim(Me!Percent & vbNullString)) > 0 Then
Beep
If MsgBox("% must be NULL!" & vbCrLf & _
"Click OK to set to Null.", vbOKOnly + _
vbQuestion) = vbOK Then
Me!Percent = Null
Cancel = True
DoCmd.GoToControl "Comments"
ElseIf (Me!Where = "Packaged" Or Me!Where = "Produced" Or Me!
Where = "Inventoried" Or Me!Where = "WIP") _
And IsNull(Me!Percent) = True Then
DoCmd.GoToControl "Percent"
End If
End If
End If

End Sub
 
J

jlute

Well, I'm not thinking clearly! I just realized that this is best in
the AfterUpdate Event however the ElseIf still isn't working!
 
D

Dirk Goldgar

The following is driving me crazy. The first half of the code is fine
but the ElseIf isn't working. It doesn't matter if Percent IsNull or
Null - I can exit either way.

I've tried several things but can't get it.

Can anyone please give me a hand?

Thanks!

Private Sub Where_Exit(Cancel As Integer)
If IsNull(Me!Where) = False Then
If (Me!Where = "Used") _
And Len(Trim(Me!Percent & vbNullString)) > 0 Then
Beep
If MsgBox("% must be NULL!" & vbCrLf & _
"Click OK to set to Null.", vbOKOnly + _
vbQuestion) = vbOK Then
Me!Percent = Null
Cancel = True
DoCmd.GoToControl "Comments"
ElseIf (Me!Where = "Packaged" Or Me!Where = "Produced" Or Me!
Where = "Inventoried" Or Me!Where = "WIP") _
And IsNull(Me!Percent) = True Then
DoCmd.GoToControl "Percent"
End If
End If
End If

End Sub


John -

It looks like you've got your ElseIf nested under the wrong If. Try this in
the control's AfterUpdate event:

'----- start of suggested code -----
Private Sub Where_AfterUpdate()

Dim blnPercentIsNull As Boolean

blnPercentIsNull = (Len(Me!Percent & vbNullString) = 0)
' Note - it may be possible to use just:
' blnPercentIsNull = IsNull(Me!Percent)

Select Case Me!Where

Case "Used"
If Not blnPercentIsNull Then
DoCmd.Beep
If MsgBox( _
"% must be NULL!" & vbCrLf & _
"Click OK to set to Null.", _
vbOKOnly+vbQuestion) _
= vbOK _
Then
Me!Percent = Null
Cancel = True
Me!Comments.SetFocus
End If
End If

Case "Packaged", "Produced", "Inventoried", "WIP"
If blnPercentIsNull Then
Me!Percent.SetFocus
End If

End Select

End Sub
'----- end of suggested code -----
 
J

jlute

Dirk to the rescue again!

<sigh> I marvel like a school girl does with a copy of Tiger Beat
sporting Leif Garrett on the cover - well, a 70's school girl, anyway.

Geez. Your "blnPercentIsNull = (Len(Me!Percent & vbNullString) = 0)"
trick is a new twist for me.

I've got such a long way to go to understand syntax. I struggle enough
with English! I fear I'll always be syntax illiterate!

Thanks, Dirk!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top