By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,050 Members | 1,703 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,050 IT Pros & Developers. It's quick & easy.

muddled if condition

P: n/a

How can i make my Else..If statement make look no so
cumbersome? Besides the condition for previewing the report

doesnt work,may be my If condition does no apply properly.
Perhaps this is due to the many conditions in my If else statements.
Do i have a mistake in my function CmdStockReceipt ?
Private Sub CmdStockReceipt_Click()
Dim f As Form
Set f = Forms![FOrderInformation]
If IsNull(f![LstStockReceipt]) Then
DoCmd.Beep
MsgBox " Please select Order first ! "
Exit Sub
Else
Dim stLinkCriteria As String
Dim stDocName As String
stDocName = "Invoice"
stLinkCriteria = "orderid = " & f![LstStockReceipt]
Dim intPrint As Integer
Dim intAnswer As Integer
intAnswer = MsgBox(" Delete? ", vbQuestion + vbYesNo)
If intAnswer = vbYes Then
Application.Echo False
CancelOrderOnOffice
Else
intPrint = MsgBox(" Print? ", vbQuestion + vbYesNo)
If intPrint = vbYes Then
FncPrint
Else
DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
VisibleOrder
Reports![invoice]![LblStockreceipt].Visible = True
End If
End If
End If
End Sub


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 12 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Peter yeshew <fr*******@abv.bg> wrote in
news:3f***********************@news.frii.net:
How can i make my Else..If statement make look no so
cumbersome? Besides the condition for previewing the report
doesnt work,may be my If condition does no apply properly.
Perhaps this is due to the many conditions in my If else
statements. Do i have a mistake in my function CmdStockReceipt
?
You can do some simplification, see my comments in the code.

Private Sub CmdStockReceipt_Click()
Dim f As Form
Set f = Forms![FOrderInformation]
If IsNull(f![LstStockReceipt]) Then
If CmdStockReceipt is a control on [FOrderInformation], you are
better off using the me! reference. Yhen you dont need to dim and
set variable f.
DoCmd.Beep
MsgBox " Please select Order first ! "
Exit Sub
Since you Exit sub here, you don't need the else, remove it and
relocate the corresponding End if.
Else
Dim stLinkCriteria As String
Dim stDocName As String
stDocName = "Invoice"
stLinkCriteria = "orderid = " & f![LstStockReceipt]
Dim intPrint As Integer
Dim intAnswer As Integer
intAnswer = MsgBox(" Delete? ", vbQuestion + vbYesNo)
If intAnswer = vbYes Then
You can combine the three statements into one:
If Msgbox("Delete? ", vbquestion+vbyesno) = vbyes then Application.Echo False
CancelOrderOnOffice
Else
intPrint = MsgBox(" Print? ", vbQuestion + vbYesNo)
If intPrint = vbYes Then
And again
ElseIf MsgBox(" Print? ", vbQuestion + vbYesNo) = VbYes then

Note the ElseIf piggybacks on tHe existing If, and does not require
a separate End if
FncPrint
Else
DoCmd.OpenReport stDocName, acPreview, ,
stLinkCriteria
VisibleOrder 'Is VisibleOrder a cut and paste error, or a subprocedure?
Reports![invoice]![LblStockreceipt].Visible =
True
End If
End If
End If
End Sub

Redone gives me:

Private Sub CmdStockReceipt_Click()
If IsNull(me![LstStockReceipt]) Then
DoCmd.Beep
MsgBox " Please select Order first ! "
Exit Sub
End If
Dim stLinkCriteria As String
Dim stDocName As String
stDocName = "Invoice"
stLinkCriteria = "orderid = " & f![LstStockReceipt]
If Msgbox("Delete? ", vbquestion+vbyesno) = vbyes then
Application.Echo False
CancelOrderOnOffice
ElseIf MsgBox(" Print? ", vbQuestion + vbYesNo) = VbYes then
FncPrint
Else
DoCmd.OpenReport stDocName, acPreview,,stLinkCriteria
Reports![invoice]![LblStockreceipt].Visible = True
End If
End Sub

Bob Q


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


Nov 12 '05 #2

P: n/a
Steve Jorgensen <no****@nospam.nospam> wrote in
news:pr********************************@4ax.com:
For the sake of legibility, when I'm comparing a constant to
the return value of a long function call, I generally put the
constant first, so it's easier for the eye to find.
Thanks for the tip. I've just gotten used to the rule that puts the
constant on the RightHand side of an equation.

Then again, I tend to prefer a Select Case block if I'm dealing
with more than one return value as in this case which really
makes the constant compare easy to read. To me, putting
another message box call in the elseif may make the code more
compact, but actually makes it more confusing to read.
It is not an issue here, but a select case where there can be ONLY
TWO results imposes a performance hit over If-Else. Inside a loop
where it gets called thousands of times, it makes a difference, so
I got into the habit of using each construct where applicable.

Bob

Finally, the If for the first Msgbox actually looks like
another guard clause to me.
I'd use a custom form with the Print, Delete, do nothing on one
form.
Bob

If vbYes = Msgbox("Delete? ", vbquestion+vbyesno) Then
Application.Echo False
CancelOrderOnOffice
Exit Sub
End If
Select Case MsgBox(" Print? ", vbQuestion + vbYesNo)
Case VbYes
FncPrint
Case Else
DoCmd.OpenReport stDocName, acPreview,,stLinkCriteria
Reports![invoice]![LblStockreceipt].Visible = True
End Select

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.