Elseif not working

L

lcox400w

I have written if then else statements, but new to elseif. I have tried to
follow examples from others, but my code below is not working. When the code
is run, it never seems to get to the elseif portion of the statement. Can
someone help point me in the correct direction.

thanks

Private Sub Dispo_Exit(Cancel As Integer)

'Checks to see if the Dispo is "Open", if so, enables dispodate field
If Dispo = "Open" Then
DispoDate.Enabled = False

'If user leaves dispo field null or blank, the dispodate field is disabled
ElseIf Not IsNull(Dispo) Or Dispo = "" Then DispoDate.Enabled = False

Else

'If the user selects any dispo, other than OPEN, the dispo field is enabled.
DispoDate.Enabled = True
MsgBox "You must enter a dispo date next", vbOKOnly, "Enter Dispo Date"
DispoDate.SetFocus

End If
 
T

tina

comments inline.

lcox400w said:
I have written if then else statements, but new to elseif. I have tried to
follow examples from others, but my code below is not working. When the code
is run, it never seems to get to the elseif portion of the statement. Can
someone help point me in the correct direction.

thanks

Private Sub Dispo_Exit(Cancel As Integer)

'Checks to see if the Dispo is "Open", if so, enables dispodate field
If Dispo = "Open" Then
DispoDate.Enabled = False

wait a minute. your comments say if Dispo = "Open" then enable DispoDate
field. BUT the code says if Dispo = "Open" then *dis*able the DispoDate
field. which action do you want to take - enable or disable on Dispo =
"Open"?
'If user leaves dispo field null or blank, the dispodate field is disabled
ElseIf Not IsNull(Dispo) Or Dispo = "" Then DispoDate.Enabled = False

wait another minute. your comments say disable DispoDate when Dispo is Null
or a zero-length string (""). BUT the code says if Dispo is *not* null or if
Dispo is a zero-length string, then disable DispoDate. are we going for Null
or not Null, here?
Else

'If the user selects any dispo, other than OPEN, the dispo field is enabled.
DispoDate.Enabled = True
MsgBox "You must enter a dispo date next", vbOKOnly, "Enter Dispo Date"
DispoDate.SetFocus

okay, this one is clear (at least i think so).

please clarify what you want to happen, and i'll do my best to help you get
there. it may be easier if you explain it in plain ol' sentences, rather
than in coding syntax.

hth
 
L

lcox400w

tina said:
comments inline.



wait a minute. your comments say if Dispo = "Open" then enable DispoDate
field. BUT the code says if Dispo = "Open" then *dis*able the DispoDate
field. which action do you want to take - enable or disable on Dispo =
"Open"?


wait another minute. your comments say disable DispoDate when Dispo is Null
or a zero-length string (""). BUT the code says if Dispo is *not* null or if
Dispo is a zero-length string, then disable DispoDate. are we going for Null
or not Null, here?


okay, this one is clear (at least i think so).


please clarify what you want to happen, and i'll do my best to help you get
there. it may be easier if you explain it in plain ol' sentences, rather
than in coding syntax.

hth

I do see that my comments were in error.

This line: 'Checks to see if the Dispo is "Open", if so, enables dispodate
field
should of said "disables" dispodate field. Sorry for the confusion, I did
not catch that. As for the second part, I am getting confused with the
elseif statement. I have written out notes as to what I am trying to
accomlish below and hope this helps clarify my goal.



'Purpose of this code:
'if dispo field is set to 'open' the user can not enter a dispo date.,
disable the dispodate field
'if dispo field is left blank you can not enter a dispo date, disable the
dispodate field
'if the user selects any other dispo, other than open, you MUST enter a
dispo date, send user to dispodate field
'if there is already a dispo date in the dispodate field, determine if the
dispo is set to 'open'. if dispo is 'open' then
' there can not be a dispo date. Ask user if they truely want to set the
dispo to 'open'. If yes then set dispo to open and
'erase the dispodate field. If no, then require the user to change the
dispo to something other than open.
 
L

lcox400w

lcox400w said:
I have written if then else statements, but new to elseif. I have tried to
follow examples from others, but my code below is not working. When the code
is run, it never seems to get to the elseif portion of the statement. Can
someone help point me in the correct direction.

thanks

Private Sub Dispo_Exit(Cancel As Integer)

'Checks to see if the Dispo is "Open", if so, enables dispodate field
If Dispo = "Open" Then
DispoDate.Enabled = False

'If user leaves dispo field null or blank, the dispodate field is disabled
ElseIf Not IsNull(Dispo) Or Dispo = "" Then DispoDate.Enabled = False

Else

'If the user selects any dispo, other than OPEN, the dispo field is enabled.
DispoDate.Enabled = True
MsgBox "You must enter a dispo date next", vbOKOnly, "Enter Dispo Date"
DispoDate.SetFocus

End If

I rewrote the code without the elseif statements and it seems to work, I'm
sure that this code could be more effecient, but I couldnt get the elseif to
work for me.


If IsNull(Dispo) Or Dispo = "" And Not IsNull(DispoDate) Then

If MsgBox("You have a Dispo Date and there is no Dispo. Do you want to
select a Dispo?", _
vbQuestion + vbYesNo, "Select a Dispo?") = vbYes Then
Me.Dispo.Dropdown

Else

MsgBox "Since there is no Dispo, Dispo Date will be erased", vbCritical,
"Eraseing Dispo Date!"
DispoDate = Null

End If

End If

If Dispo = "Open" And Not IsNull(DispoDate) Then

If MsgBox("Dispo is set to OPEN and there is a Dispo Date. You can not
have a Dispo Date if the case is open." & _
"Do you want me to erase Dispo Date?", vbCritical + vbYesNo, "Erase
Dispo Date") = vbYes Then

DispoDate = Null

Else

MsgBox "Since you selected NO, you must change the Dispo to something
other than OPEN", vbCritical, "Change Dispo"
Me.Dispo.Dropdown

End If

End If
 
T

tina

okay, we'll give this a go. first of all, suggest you place the code in the
Dispo control's BeforeUpdate and AfterUpdate events as shown, so the code
only runs when the value is edited. if you use the Exit event procedure,
simply tabbing through the control will activate the code.

Private Sub Dispo_BeforeUpdate(Cancel As Integer)

If Me!Dispo = "Open" And _
Not IsNull(Me!DispoDate) Then
If MsgBox("Are you sure you want to set Dispo " _
& "to Open?", vbExclamation+vbYesNo) = vbNo Then
Cancel = True
Me!Dispo.Undo
Exit Sub
End If
End If

Me!DispoDate.Enabled = Not (Me!Dispo = "Open" _
Or IsNull(Me!Dispo))

End Sub

Private Sub Dispo_AfterUpdate()

If Me!Dispo = "Open" Or IsNull(Me!Dispo) Then
If Not IsNull(Me!DispoDate) Then
Me!DispoDate = Null
End If
ElseIf IsNull(Me!DispoDate) Then
Me!DispoDate.SetFocus
End If

End Sub

also, add the following to the *form's* Current event procedure, as

Private Sub Form_Current()

Me!DispoDate.Enabled = Not (Me!Dispo = "Open" _
Or IsNull(Me!Dispo))

End Sub

hth
 
L

lcox400w

You made this complicated code much easier to understand. I also never
considered using the beforeUpdate event.

Thanks a lot.
 
T

tina

you're welcome, and remember to test it thoroughly with various data entry
scenarios, to make sure the logic is sound. :)
 

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