If this then that

J

JohnLute

I need help with an IF statement.

Unbound [DecWtg] needs to convert bound [Volume] it to a g (gram) value
based on the selected value in bound [UOM].

[UOM] has 4 choices:
oz.
lb.
g
Density

If [UOM] is oz. then [DecWtg] needs to calculate: [Volume]*28.35
If [UOM] is lb. then [DecWtg] needs to claculate: [Volume]/([floz]*128)
If [UOM] is g then [DecWtg] needs to do nothing as [Volume] is already in g.
If [UOM] is Density then [DecWtg] needs to calculate: [Volume]*3.55*64

Is this possible?

Thanks in advance!
 
O

Ofer Cohen

Try this, on the cotrol source of the DecWtg field

=IIf( [UOM] = "oz", [Volume]*28.35, IIf( [UOM] = "lb"
,[Volume]/([floz]*128), IIf([UOM] = "Density", [Volume]*3.55*64, [Volume])))
 
S

storrboy

I need help with an IF statement.

Unbound [DecWtg] needs to convert bound [Volume] it to a g (gram) value
based on the selected value in bound [UOM].

[UOM] has 4 choices:
oz.
lb.
g
Density

If [UOM] is oz. then [DecWtg] needs to calculate: [Volume]*28.35
If [UOM] is lb. then [DecWtg] needs to claculate: [Volume]/([floz]*128)
If [UOM] is g then [DecWtg] needs to do nothing as [Volume] is already in g.
If [UOM] is Density then [DecWtg] needs to calculate: [Volume]*3.55*64

Is this possible?

Thanks in advance!

--www.Marzetti.com


Another method which may or may not be easier for you to read and
manage is to use a Select..Case statement in the AfterUpdate event of
the combo.

Select Case Me!UOM
Case "oz"
Me!DecWtg=Me!Volume*28.35
Case "lb"
Me!DecWtg=Me!Volume/(Me!floz*128)
Case "g"
'Do nothing
Case ....

End Select

Add a Case branch for each option in the list that requires a
different calculation.
 
J

JohnLute

Thanks - that's another good solution, too!

--
www.Marzetti.com


storrboy said:
I need help with an IF statement.

Unbound [DecWtg] needs to convert bound [Volume] it to a g (gram) value
based on the selected value in bound [UOM].

[UOM] has 4 choices:
oz.
lb.
g
Density

If [UOM] is oz. then [DecWtg] needs to calculate: [Volume]*28.35
If [UOM] is lb. then [DecWtg] needs to claculate: [Volume]/([floz]*128)
If [UOM] is g then [DecWtg] needs to do nothing as [Volume] is already in g.
If [UOM] is Density then [DecWtg] needs to calculate: [Volume]*3.55*64

Is this possible?

Thanks in advance!

--www.Marzetti.com


Another method which may or may not be easier for you to read and
manage is to use a Select..Case statement in the AfterUpdate event of
the combo.

Select Case Me!UOM
Case "oz"
Me!DecWtg=Me!Volume*28.35
Case "lb"
Me!DecWtg=Me!Volume/(Me!floz*128)
Case "g"
'Do nothing
Case ....

End Select

Add a Case branch for each option in the list that requires a
different calculation.
 
J

JohnLute

I've resolved to use Ofer's solution however I now need and AfterUpdate Event
similar to what you've described below.

If [UOM] is "oz.", "lb.", "g" then the cursor needs to go to [PKTareWt].

AND

[Density] must be NULL.

How can I code this if possible?

Thanks for your help and patience!

--
www.Marzetti.com


storrboy said:
I need help with an IF statement.

Unbound [DecWtg] needs to convert bound [Volume] it to a g (gram) value
based on the selected value in bound [UOM].

[UOM] has 4 choices:
oz.
lb.
g
Density

If [UOM] is oz. then [DecWtg] needs to calculate: [Volume]*28.35
If [UOM] is lb. then [DecWtg] needs to claculate: [Volume]/([floz]*128)
If [UOM] is g then [DecWtg] needs to do nothing as [Volume] is already in g.
If [UOM] is Density then [DecWtg] needs to calculate: [Volume]*3.55*64

Is this possible?

Thanks in advance!

--www.Marzetti.com


Another method which may or may not be easier for you to read and
manage is to use a Select..Case statement in the AfterUpdate event of
the combo.

Select Case Me!UOM
Case "oz"
Me!DecWtg=Me!Volume*28.35
Case "lb"
Me!DecWtg=Me!Volume/(Me!floz*128)
Case "g"
'Do nothing
Case ....

End Select

Add a Case branch for each option in the list that requires a
different calculation.
 
S

storrboy

I've resolved to use Ofer's solution however I now need and AfterUpdate Event
similar to what you've described below.

If [UOM] is "oz.", "lb.", "g" then the cursor needs to go to [PKTareWt].

AND

[Density] must be NULL.

How can I code this if possible?

Thanks for your help and patience!

--www.Marzetti.com

storrboy said:
I need help with an IF statement.
Unbound [DecWtg] needs to convert bound [Volume] it to a g (gram) value
based on the selected value in bound [UOM].
[UOM] has 4 choices:
oz.
lb.
g
Density
If [UOM] is oz. then [DecWtg] needs to calculate: [Volume]*28.35
If [UOM] is lb. then [DecWtg] needs to claculate: [Volume]/([floz]*128)
If [UOM] is g then [DecWtg] needs to do nothing as [Volume] is already in g.
If [UOM] is Density then [DecWtg] needs to calculate: [Volume]*3.55*64
Is this possible?
Thanks in advance!
--www.Marzetti.com
Another method which may or may not be easier for you to read and
manage is to use a Select..Case statement in the AfterUpdate event of
the combo.
Select Case Me!UOM
Case "oz"
Me!DecWtg=Me!Volume*28.35
Case "lb"
Me!DecWtg=Me!Volume/(Me!floz*128)
Case "g"
'Do nothing
Case ....
End Select
Add a Case branch for each option in the list that requires a
different calculation.


Take a look in the help files (as well as my previous example) for
examples and info of how to use Select Case statements. Also look into
the SetFocus method and assigning values to controls. If you still
can't figure it out, post back and we'll get you more guidance.
 
J

JohnLute

Take a look in the help files (as well as my previous example) for
examples and info of how to use Select Case statements. Also look into
the SetFocus method and assigning values to controls. If you still
can't figure it out, post back and we'll get you more guidance.

I've changed a couple things around. This is what I've come up with but I
know it's clumsy/wrong despite the fact that it works:
Private Sub UOM_AfterUpdate()
If UOM = "fl. oz." Then
Beep
MsgBox "Density is required!"
DoCmd.GoToControl "Density"
End If

If UOM = "g" Then
Beep
MsgBox "Density must be NULL!"
DoCmd.GoToControl "Density"
End If

If UOM = "oz." Then
Beep
MsgBox "Density must be NULL!"
DoCmd.GoToControl "Density"
End If

If UOM = "lb." Then
Beep
MsgBox "Density must be NULL!"
DoCmd.GoToControl "Density"
End If

End Sub

Is there an easier/more proper way to write this? Thanks!
 
D

Douglas J. Steele

The biggest problem I see is that you're not checking to see whether
anything's filled in for Density or not.

Try something like:

Private Sub UOM_AfterUpdate()
If Me!UOM = "fl. oz." Then
If Len(Me!Density & vbNullString) = 0 Then
Beep
MsgBox "Density is required!"
Me!Density.SetFocus
End If
ElseIf Me!UOM = "g" Or Me!UOM = "oz." _
Or Me!UOM = "lb." Then
If Len(Me!Density & vbNullString) > 0 THen
Beep
MsgBox "Density must be NULL!"
Me!Density.SetFocus
End If
End If
End Sub
 
J

JohnLute

Thanks, Douglas! That's simplified things tremendously. One remaining thing:
how can I assure that [Density] IsNull If [UOM] = "g", "oz.", or "lb."? I
have this but it generates compile errors:

Private Sub Density_Exit(Cancel As Integer)
If UOM = "g" Or "oz." Or "lb." And IsNotNull(Density) Then
Beep
MsgBox "Density must be NULL!"
Cancel = True
End If

End Sub

Thanks, again!
 
D

Douglas J. Steele

Private Sub Density_Exit(Cancel As Integer)

If Me!UOM = "g" Or Me!UOM = "oz." Or Me!UOM = "lb." _
And IsNull(Me!Density) = False Then
Beep
MsgBox "Density must be NULL!"
Cancel = True
End If

End Sub


--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


JohnLute said:
Thanks, Douglas! That's simplified things tremendously. One remaining
thing:
how can I assure that [Density] IsNull If [UOM] = "g", "oz.", or "lb."? I
have this but it generates compile errors:

Private Sub Density_Exit(Cancel As Integer)
If UOM = "g" Or "oz." Or "lb." And IsNotNull(Density) Then
Beep
MsgBox "Density must be NULL!"
Cancel = True
End If

End Sub

Thanks, again!

--
www.Marzetti.com


Douglas J. Steele said:
The biggest problem I see is that you're not checking to see whether
anything's filled in for Density or not.

Try something like:

Private Sub UOM_AfterUpdate()
If Me!UOM = "fl. oz." Then
If Len(Me!Density & vbNullString) = 0 Then
Beep
MsgBox "Density is required!"
Me!Density.SetFocus
End If
ElseIf Me!UOM = "g" Or Me!UOM = "oz." _
Or Me!UOM = "lb." Then
If Len(Me!Density & vbNullString) > 0 THen
Beep
MsgBox "Density must be NULL!"
Me!Density.SetFocus
End If
End If
End Sub
 
J

JohnLute

Thanks, Douglas!

Something's not quite right.

What is needed:
If [UOM] is g, oz., or lb. then [Density] must be NULL.

Solution:
Private Sub Density_Exit(Cancel As Integer)
If Me!UOM = "g" Or Me!UOM = "oz." Or Me!UOM = "lb." _
And IsNull(Me!Density) = False Then
Beep
MsgBox "Density must be NULL!"
Cancel = True
End If

End Sub

If I select "oz." for [Density] the "Density must be NULL!" MsgBox executes
however I can't exit [Density] even if it's NULL.

Also, if I select "fl. oz." for [Density] the "Density is required!" MsgBox
executes however I can exit [Density] even if it's NULL.

I can't see at all why this is...?
 
D

Douglas J. Steele

You shouldn't be getting the message box when you've selected fl. oz., as
that's not one of the values trapped in that code, so you must have code
else where that's generating message boxes.
 
J

JohnLute

Sorry - I wasn't clear. I have only 2 event procedures. I deleted the other
and left just this:

Private Sub Density_Exit(Cancel As Integer)
If Me!UOM = "g" Or Me!UOM = "oz." Or Me!UOM = "lb." _
And IsNull(Me!Density) = False Then
Beep
MsgBox "Density must be NULL!"
Cancel = True
End If

End Sub

If I select "g", "oz.", or "lb."for [Density] the "Density must be NULL!"
MsgBox executes however I can't exit (tab out or click out) [Density] even
though it's NULL or even after making it NULL.
 
D

Douglas J. Steele

You sure it's Null, and not a zero-length string?

Try:

If Me!UOM = "g" Or Me!UOM = "oz." Or Me!UOM = "lb." _
And Len(Trim(Me!Density & vbNullString)) > 0 Then
 
J

JohnLute

Still no good. This is very perplexing. Have you tried it in a model? Here is
the SQL of the subform in my design:

SELECT tblFGsTargetFillWeightsCalculated.txtFacilityID,
tblFGsTargetFillWeightsCalculated.txtProfileID,
tblFGsTargetFillWeightsCalculated.LineID,
tblFGsTargetFillWeightsCalculated.VolWt,
tblFGsTargetFillWeightsCalculated.UOM,
tblFGsTargetFillWeightsCalculated.Density,
tblFGsTargetFillWeightsCalculated.PKTareWtg,
tblFGsTargetFillWeightsCalculated.STDEV,
tblFGsTargetFillWeightsCalculated.Comments, tblFGPhysicalAttributes.UnitCount
FROM tblFGsTargetFillWeightsCalculated INNER JOIN tblFGPhysicalAttributes ON
tblFGsTargetFillWeightsCalculated.txtProfileID =
tblFGPhysicalAttributes.txtProfileID ORDER BY
tblFGsTargetFillWeightsCalculated.txtFacilityID,
tblFGsTargetFillWeightsCalculated.LineID;

Here are the two event procedures per your suggestion:

Private Sub UOM_AfterUpdate()
If Me!UOM = "fl. oz." Then
If Len(Me!Density & vbNullString) = 0 Then
Beep
MsgBox "Density is required!"
Me!Density.SetFocus
End If
ElseIf Me!UOM = "g" Or Me!UOM = "oz." _
Or Me!UOM = "lb." Then
If Len(Me!Density & vbNullString) > 0 Then
Beep
MsgBox "Density must be NULL!"
Me!Density.SetFocus
End If
End If

End Sub

Private Sub Density_Exit(Cancel As Integer)
If Me!UOM = "g" Or Me!UOM = "oz." Or Me!UOM = "lb." _
And Len(Trim(Me!Density & vbNullString)) > 0 Then
Beep
MsgBox "Density must be NULL!"
Cancel = True
End If

End Sub

Can you see any conflicts?
 
D

Douglas J. Steele

Which event is raising the message? (If you're not sure, change the message
to indicate)

You should likely put code in the Density_Exit sub to ensure that the
Density has been set if fl. oz. was selected.

One thing to remember is that your users might not update the fields in a
logical order. It's possible in Density_Exit that UOM hasn't been selected
yet, so you need to check for that.

Private Sub Density_Exit(Cancel As Integer)
If IsNull(Me!UOM) = False Then
If Me!UOM = "g" Or Me!UOM = "oz." Or Me!UOM = "lb." _
And Len(Trim(Me!Density & vbNullString)) > 0 Then
Beep
MsgBox "Density must be NULL!"
Cancel = True
ElseIf Me!UOM = "fl. oz." And _
Len(Trim(Me!Density & vbNullString)) = 0 Then
Beep
MsgBox "Density is required."
Cancel = True
End If
End If
End Sub

Have you considered setting the Density to Null when they select g., oz. or
lb.?

Private Sub UOM_AfterUpdate()
If Me!UOM = "fl. oz." Then
If Len(Me!Density & vbNullString) = 0 Then
Beep
MsgBox "Density is required!"
Me!Density.SetFocus
End If
ElseIf Me!UOM = "g" Or Me!UOM = "oz." _
Or Me!UOM = "lb." Then
If Len(Me!Density & vbNullString) > 0 Then
Beep
If MsgBox("Density must be NULL!" & vbCrLf & _
"Do you want to set it to Null?", vbYesNo + _
vbQuestion) = vbYes Then
Me!Density = Null
End If
End If
End If
End Sub
 
J

JohnLute

All of that makes absolute sense. The "set to Null" is pretty slick, too.
Unfortunately, things still aren't working as expected. The two problems:

New record:
Tab to [UOM] and select "g" > Tab OUT to [Density] and leave Null but can't
Tab OUT as the MsgBox "Density must be NULL!" executes. Have to hit ESC or
exit the form to get out.

Existing record:
Tab to [UOM] and change from "fl. oz." to "g" > MsgBox "Density must be
NULL! "Do you want to set it to Null?" appears. > Select Yes - [Density]
updates to Null. > Tab OUT to [Density] but can't Tab OUT out of [Density] as
the MsgBox "Density must be NULL!" executes. Have to hit ESC or exit the form
to get out.

My head is spinning! I can't understand why this is behaving in this manner.
Perhaps the DoCmd.GoToControl is needed? For example, if [UOM] "g" is
selected then leave [Density] Null and go to [PKTareWTg]?

Also, if [UOM] "fl. oz." is selected then go to [Density] and add value?

???
 
D

Douglas J. Steele

I'm guessing that the form controls are named the same as the fields in the
underlying recordset. It usually helps to have them named differently (and
refer to the controls in the VBA code, not the fields). (I always rename all
my text boxes to start with txt, my combo boxes to start with cbo and so on)

You don't happen to have a default value assigned to Density, do you? That
would might explain the problems.

BTW, it might be simpler to remove the logic from the two controls, and to
put all of the checks in the form's BeforeUpdate event.

--
Doug Steele, Microsoft Access MVP

(no e-mails, please!)


JohnLute said:
All of that makes absolute sense. The "set to Null" is pretty slick, too.
Unfortunately, things still aren't working as expected. The two problems:

New record:
Tab to [UOM] and select "g" > Tab OUT to [Density] and leave Null but
can't
Tab OUT as the MsgBox "Density must be NULL!" executes. Have to hit ESC or
exit the form to get out.

Existing record:
Tab to [UOM] and change from "fl. oz." to "g" > MsgBox "Density must be
NULL! "Do you want to set it to Null?" appears. > Select Yes - [Density]
updates to Null. > Tab OUT to [Density] but can't Tab OUT out of [Density]
as
the MsgBox "Density must be NULL!" executes. Have to hit ESC or exit the
form
to get out.

My head is spinning! I can't understand why this is behaving in this
manner.
Perhaps the DoCmd.GoToControl is needed? For example, if [UOM] "g" is
selected then leave [Density] Null and go to [PKTareWTg]?

Also, if [UOM] "fl. oz." is selected then go to [Density] and add value?

???

--
www.Marzetti.com


Douglas J. Steele said:
Which event is raising the message? (If you're not sure, change the
message
to indicate)

You should likely put code in the Density_Exit sub to ensure that the
Density has been set if fl. oz. was selected.

One thing to remember is that your users might not update the fields in a
logical order. It's possible in Density_Exit that UOM hasn't been
selected
yet, so you need to check for that.

Private Sub Density_Exit(Cancel As Integer)
If IsNull(Me!UOM) = False Then
If Me!UOM = "g" Or Me!UOM = "oz." Or Me!UOM = "lb." _
And Len(Trim(Me!Density & vbNullString)) > 0 Then
Beep
MsgBox "Density must be NULL!"
Cancel = True
ElseIf Me!UOM = "fl. oz." And _
Len(Trim(Me!Density & vbNullString)) = 0 Then
Beep
MsgBox "Density is required."
Cancel = True
End If
End If
End Sub

Have you considered setting the Density to Null when they select g., oz.
or
lb.?

Private Sub UOM_AfterUpdate()
If Me!UOM = "fl. oz." Then
If Len(Me!Density & vbNullString) = 0 Then
Beep
MsgBox "Density is required!"
Me!Density.SetFocus
End If
ElseIf Me!UOM = "g" Or Me!UOM = "oz." _
Or Me!UOM = "lb." Then
If Len(Me!Density & vbNullString) > 0 Then
Beep
If MsgBox("Density must be NULL!" & vbCrLf & _
"Do you want to set it to Null?", vbYesNo + _
vbQuestion) = vbYes Then
Me!Density = Null
End If
End If
End If
End Sub
 
J

JohnLute

Thanks, Doug I truly appreciate your help. I think I've stumbled onto a real
brain buster here - read on if you feel brave!
I'm guessing that the form controls are named the same as the fields in the
underlying recordset. It usually helps to have them named differently (and
refer to the controls in the VBA code, not the fields). (I always rename all
my text boxes to start with txt, my combo boxes to start with cbo and so on)

I changed all of the names accordingly and still had the Exit [Density]
problem. I changed the names back and tried again. This time I noticed that
if I selected "lb." for [Density] that everything worked fine however if I
selected either "g" or "oz." then the Exit [Density] problem occured.

That is truly puzzling. I didn't notice it before. So things have narrowed
down! I can't Tab or click OUT of [Density] after creating a new or editing
an existing record with "g" or "oz." selected for [Density]. Moreover there
are no problems when "fl. oz." is selected for [Density] regardless if I'm
creating a new or editing an existing record.

This just doesn't make any immediate sense.

Here are the events as slightly edited:

Private Sub Density_Exit(Cancel As Integer)
If IsNull(Me!UOM) = False Then
If Me!UOM = "g" Or Me!UOM = "oz." Or Me!UOM = "lb." _
And Len(Trim(Me!Density & vbNullString)) > 0 Then
Beep
MsgBox "Density must be NULL!"
Cancel = True
ElseIf Me!UOM = "fl. oz." And _
Len(Trim(Me!Density & vbNullString)) = 0 Then
Beep
MsgBox "Density is required!"
Cancel = True
End If
End If

End Sub

Private Sub UOM_AfterUpdate()
If Me!UOM = "fl. oz." Then
If Len(Me!Density & vbNullString) = 0 Then
Beep
MsgBox "Density is required!"
Me!Density.SetFocus
End If
ElseIf Me!UOM = "g" Or Me!UOM = "oz." Or Me!UOM = "lb." Then
If Len(Me!Density & vbNullString) > 0 Then
Beep
If MsgBox("Density must be NULL!" & vbCrLf & _
"Do you want to set it to Null?", vbYesNo + _
vbQuestion) = vbYes Then
Me!Density = Null
DoCmd.GoToControl "PKTareWtg"
End If
End If
End If

End Sub
You don't happen to have a default value assigned to Density, do you? That
would might explain the problems.

Nope. I checked all other fields, too. No defaults.
BTW, it might be simpler to remove the logic from the two controls, and to
put all of the checks in the form's BeforeUpdate event.

I tried that, too and it doesn't resolve the problem.

Did you happen to try a model based off the SQL I posted?

SELECT tblFGsTargetFillWeightsCalculated.txtFacilityID,
tblFGsTargetFillWeightsCalculated.txtProfileID,
tblFGsTargetFillWeightsCalculated.LineID,
tblFGsTargetFillWeightsCalculated.VolWt,
tblFGsTargetFillWeightsCalculated.UOM,
tblFGsTargetFillWeightsCalculated.Density,
tblFGsTargetFillWeightsCalculated.PKTareWtg,
tblFGsTargetFillWeightsCalculated.STDEV,
tblFGsTargetFillWeightsCalculated.Comments, tblFGPhysicalAttributes.UnitCount
FROM tblFGsTargetFillWeightsCalculated INNER JOIN tblFGPhysicalAttributes ON
tblFGsTargetFillWeightsCalculated.txtProfileID =
tblFGPhysicalAttributes.txtProfileID
ORDER BY tblFGsTargetFillWeightsCalculated.txtFacilityID,
tblFGsTargetFillWeightsCalculated.LineID;

KA-BOOOM! That was my head that just exploded!
 
J

JohnLute

Correction!!!
I changed all of the names accordingly and still had the Exit [Density]
problem. I changed the names back and tried again. This time I noticed that
if I selected "lb." for [Density] that everything worked fine however if I
selected either "g" or "oz." then the Exit [Density] problem occured.

Should be:
I changed all of the names accordingly and still had the Exit [Density]
problem. I changed the names back and tried again. This time I noticed that
if I selected "lb." for [UOM] that everything worked fine however if I
selected either "g" or "oz." then the Exit [Density] problem occured.
and
That is truly puzzling. I didn't notice it before. So things have narrowed
down! I can't Tab or click OUT of [Density] after creating a new or editing
an existing record with "g" or "oz." selected for [Density]. Moreover there
are no problems when "fl. oz." is selected for [Density] regardless if I'm
creating a new or editing an existing record.

Should be:
That is truly puzzling. I didn't notice it before. So things have narrowed
down! I can't Tab or click OUT of [Density] after creating a new or editing
an existing record with "g" or "oz." selected for [UOM]. Moreover there
are no problems when "fl. oz." is selected for [UOM] regardless if I'm
creating a new or editing an existing record.

So sorry for the confusion!
 
Top