If Then Statement Help

J

JudyB

I have a form with a combo box named "Status" which has four options from
which the user can select - Open (default), Closed, Scheduled, and
Incomplete. I have another text box named "Date Closed" of which I would
like to have automatically filled with the current date if the user selects
the "Closed" option in the "Status" box. But, if the user selects one of the
other three options, I would like for the "Closed Date" text box to be left
null. Can anyone help me with the code. This is what I have thus far:

Private Sub Status_AfterUpdate()
Me.[DateClosed] = Date

End Sub

Thank you in advance for any help. I know that this is probably an easy
fix, but I am new to writing code. Thanks again!
 
S

SimonG via AccessMonster.com

Try this:

Private Sub Status_AfterUpdate()
If Me!Status = "Closed" Then
Me.[DateClosed] = Date
End If

End Sub

Regards,
Simon
 
S

Stuart McCall

JudyB said:
I have a form with a combo box named "Status" which has four options from
which the user can select - Open (default), Closed, Scheduled, and
Incomplete. I have another text box named "Date Closed" of which I would
like to have automatically filled with the current date if the user
selects
the "Closed" option in the "Status" box. But, if the user selects one of
the
other three options, I would like for the "Closed Date" text box to be
left
null. Can anyone help me with the code. This is what I have thus far:

Private Sub Status_AfterUpdate()
Me.[DateClosed] = Date

End Sub

Thank you in advance for any help. I know that this is probably an easy
fix, but I am new to writing code. Thanks again!

Private Sub Status_AfterUpdate()
If Me.Status.Value = "Closed" Then
Me.[DateClosed] = Date
End If
End Sub

This will only set DateClosed if the user selects "Closed". Otherwise
nothing (in this procedure) will affect DateClosed.
 
R

Ron2006

it should probably be:

Private Sub Status_AfterUpdate()
If Me.Status.Value = "Closed" Then
Me.[DateClosed] = Date
else
Me.[DateClosed] = null
End If
End Sub


You need this because if they choose closed by mistake it will load a
date and you need some way of clearing that mistake out.

Ron
 
T

TinMan

Hi there i have looked at this code which seems similar to my problem. Can
anyone understand why it would not work:

Private Sub txtLocation_AfterUpdate()

Dim txtLocation As String

If Me.txtLocation.Value = "Big Building" Then
Me.txtFireDoors.DefaultValue = "Good"
Me.txtFireAlarmCallPoints.DefaultValue = "Good"
Me.Refresh

ElseIf Me.txtLocation.Value = "Office" Then
Me.txtEmergencyLighting.DefaultValue = "Good"
Me.Refresh

ElseIf Me.txtLocation.Value = "Toy Shop" Then
Me.txtFirePoint_Signs.DefaultValue = "N/A"
Me.txtEmergencyLighting = "Good"
Me.Refresh

ElseIf Me.txtLocation.Value = "Workshop" Then
Me.txtFireDoors.DefaultValue = "Clear"
Me.txtFireAlarmCallPoints.DefaultValue = "N/A"
Me.Refresh

Else
MsgBox "Please Choose valid Location"

End Sub
 
D

Dirk Goldgar

TinMan said:
Hi there i have looked at this code which seems similar to my problem. Can
anyone understand why it would not work:

Private Sub txtLocation_AfterUpdate()

Dim txtLocation As String

If Me.txtLocation.Value = "Big Building" Then
Me.txtFireDoors.DefaultValue = "Good"
Me.txtFireAlarmCallPoints.DefaultValue = "Good"
Me.Refresh

ElseIf Me.txtLocation.Value = "Office" Then
Me.txtEmergencyLighting.DefaultValue = "Good"
Me.Refresh

ElseIf Me.txtLocation.Value = "Toy Shop" Then
Me.txtFirePoint_Signs.DefaultValue = "N/A"
Me.txtEmergencyLighting = "Good"
Me.Refresh

ElseIf Me.txtLocation.Value = "Workshop" Then
Me.txtFireDoors.DefaultValue = "Clear"
Me.txtFireAlarmCallPoints.DefaultValue = "N/A"
Me.Refresh

Else
MsgBox "Please Choose valid Location"

End Sub


1. You need an "End If" statement before the "End Sub" statement.

2. This statement:
Dim txtLocation As String

.... serves no purpose and should be removed. Also, it is confusing at best
to declare a variable with the same name as one of the controls on your
form. At worst, you could get behavior you don't expect, as a simple
reference to "txtLocation:" might not be interpreted the way you intended.

Beyond the above, you'll have to explain what you mean by "not work".
 
T

TinMan

Of course that was all it all. How do i reply to the newsgroup. Thank you for
your help 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