Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Else If.. bungled

aralaral Member Posts: 25
Can somebody help me with the Else If condition? I am confused.I need to build a function that asks the following questions

1. Shall i delete? If yes delete, if not proceed further
2. If i do no delete shall i print the document?
If yes, then it should ask print original or print not original?
if print original then an other code must be executed namely "visible invoice" and then the function Fncprint
if not original then open the report in preview and then execute the fucntion Print

3. If i do not want to delete nor to print,do i just open in preview mode?
if yes then i must ask original or not
if original then i must add the function visible invocie

However, when i want just to open the report, the function executes a print command.I am afraid my end if clauses are
so confused that i canotn see what is wrong.
Can somebody advise me of a better way for doing it?

Below is my function:


Public Function ProcessInvoice()
' the function is placed in the control CmdInvoices being the output for the ListInvoices
Dim f As Form
Set f = Forms![main]

If IsNull(f![ListInvoices]) Then
DoCmd.Beep
MsgBox " Please select Invoice first ! "
Exit Function
Else
Dim stLinkCriteria As String
Dim stDocName As String
stDocName = "Invoice"
Dim intOriginal As Integer
stLinkCriteria = "paymentid = " & f![ListInvoices]

Dim intPrint As Integer
If MsgBox(" Delete? ", vbQuestion + vbYesNo) = vbYes Then
Application.Echo False
Call CancelInvoice
Else
intOriginal = MsgBox(" original? ? ", vbQuestion + vbYesNo)
intPrint = MsgBox("Print ?", vbQuestion + vbYesNo)
If intOriginal = vbYes Then
DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
VisibleInvoice
Reports![invoice]![original].Visible = True
Else
DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
VisibleInvoice
End If
If intPrint = vbYes Then
FncPrint
End If
End If
End If
End Function





Comments

  • compuchipcompuchip Member Posts: 273
    First, I'd like to give a tip for readability:
    First, use the .. command (replace the < and > by [ and ]).
    It shows your indention (if you had any) which makes the code more readable.

    I think I've found some error-sensitive code (in other words: the error is probably there but I was too lazy to test it :))
    Copy it to a Module in Access and see my commments
    [code]

    Public Function ProcessInvoice()
    ' the function is placed in the control CmdInvoices being the output for the ListInvoices
    Dim f As Form
    Set f = Forms![main]

    If IsNull(f![ListInvoices]) Then
    DoCmd.Beep
    MsgBox " Please select Invoice first ! "
    Exit Function
    ' (a) You don't need the Else here!
    ' Else
    End If

    Dim stLinkCriteria As String
    Dim stDocName As String
    stDocName = "Invoice"
    Dim intOriginal As Integer
    stLinkCriteria = "paymentid = " & f![ListInvoices]

    Dim intPrint As Integer
    If MsgBox(" Delete? ", vbQuestion + vbYesNo) = vbYes Then
    Application.Echo False
    Call CancelInvoice
    ' (b) Below I got rid of another Else, using Exit Function and End If, since the
    ' rest of the function was inside the Else ... End If
    Exit Function
    End If

    ' I think this is where the error is. You get a value for intPrint, but I can't
    ' see you using it anywhere.
    ' You should review this block of code
    intOriginal = MsgBox(" original? ? ", vbQuestion + vbYesNo)
    intPrint = MsgBox("Print ?", vbQuestion + vbYesNo)
    If intOriginal = vbYes Then
    DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    VisibleInvoice
    Reports![invoice]![original].Visible = True
    Else
    DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    VisibleInvoice
    End If

    ' You can put these on one line :)
    If intPrint = vbYes Then FncPrint

    ' Skip it - See remark (b)
    ' End If
    ' Skip it - See remark (a)
    ' End If
    End Function
    [/code]

    : Can somebody help me with the Else If condition? I am confused.I need to build a function that asks the following questions
    :
    : 1. Shall i delete? If yes delete, if not proceed further
    : 2. If i do no delete shall i print the document?
    : If yes, then it should ask print original or print not original?
    : if print original then an other code must be executed namely "visible invoice" and then the function Fncprint
    : if not original then open the report in preview and then execute the fucntion Print
    :
    : 3. If i do not want to delete nor to print,do i just open in preview mode?
    : if yes then i must ask original or not
    : if original then i must add the function visible invocie
    :
    : However, when i want just to open the report, the function executes a print command.I am afraid my end if clauses are
    : so confused that i canotn see what is wrong.
    : Can somebody advise me of a better way for doing it?
    :
    : Below is my function:
    :
    :
    : Public Function ProcessInvoice()
    : ' the function is placed in the control CmdInvoices being the output for the ListInvoices
    : Dim f As Form
    : Set f = Forms![main]
    :
    : If IsNull(f![ListInvoices]) Then
    : DoCmd.Beep
    : MsgBox " Please select Invoice first ! "
    : Exit Function
    : Else
    : Dim stLinkCriteria As String
    : Dim stDocName As String
    : stDocName = "Invoice"
    : Dim intOriginal As Integer
    : stLinkCriteria = "paymentid = " & f![ListInvoices]
    :
    : Dim intPrint As Integer
    : If MsgBox(" Delete? ", vbQuestion + vbYesNo) = vbYes Then
    : Application.Echo False
    : Call CancelInvoice
    : Else
    : intOriginal = MsgBox(" original? ? ", vbQuestion + vbYesNo)
    : intPrint = MsgBox("Print ?", vbQuestion + vbYesNo)
    : If intOriginal = vbYes Then
    : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : VisibleInvoice
    : Reports![invoice]![original].Visible = True
    : Else
    : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : VisibleInvoice
    : End If
    : If intPrint = vbYes Then
    : FncPrint
    : End If
    : End If
    : End If
    : End Function
    :
    :
    :
    :
    :
    :

  • splansingsplansing Member Posts: 140
    Sometimes I understand why, other times I do not. Basically, I was always told that using a direct branching statement like GOTO or EXIT was a poor programming practice. But when you start nesting the IF statements in a function like the one below, you really have to do a lot more homework to get the point across. Still, old habits die hard in me, I guess. I can't help but work them out without branching. The only time I really use the Exit statements is just before an error handling procedure, where you really have no choice. This is something I haven't even thought of in years, because, like I said, I guess old habits die hard.

    : First, I'd like to give a tip for readability:
    : First, use the .. command (replace the < and > by [ and ]).
    : It shows your indention (if you had any) which makes the code more readable.
    :
    : I think I've found some error-sensitive code (in other words: the error is probably there but I was too lazy to test it :))
    : Copy it to a Module in Access and see my commments
    : [code]
    :
    : Public Function ProcessInvoice()
    : ' the function is placed in the control CmdInvoices being the output for the ListInvoices
    : Dim f As Form
    : Set f = Forms![main]
    :
    : If IsNull(f![ListInvoices]) Then
    : DoCmd.Beep
    : MsgBox " Please select Invoice first ! "
    : Exit Function
    : ' (a) You don't need the Else here!
    : ' Else
    : End If
    :
    : Dim stLinkCriteria As String
    : Dim stDocName As String
    : stDocName = "Invoice"
    : Dim intOriginal As Integer
    : stLinkCriteria = "paymentid = " & f![ListInvoices]
    :
    : Dim intPrint As Integer
    : If MsgBox(" Delete? ", vbQuestion + vbYesNo) = vbYes Then
    : Application.Echo False
    : Call CancelInvoice
    : ' (b) Below I got rid of another Else, using Exit Function and End If, since the
    : ' rest of the function was inside the Else ... End If
    : Exit Function
    : End If
    :
    : ' I think this is where the error is. You get a value for intPrint, but I can't
    : ' see you using it anywhere.
    : ' You should review this block of code
    : intOriginal = MsgBox(" original? ? ", vbQuestion + vbYesNo)
    : intPrint = MsgBox("Print ?", vbQuestion + vbYesNo)
    : If intOriginal = vbYes Then
    : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : VisibleInvoice
    : Reports![invoice]![original].Visible = True
    : Else
    : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : VisibleInvoice
    : End If
    :
    : ' You can put these on one line :)
    : If intPrint = vbYes Then FncPrint
    :
    : ' Skip it - See remark (b)
    : ' End If
    : ' Skip it - See remark (a)
    : ' End If
    : End Function
    : [/code]
    :
    : : Can somebody help me with the Else If condition? I am confused.I need to build a function that asks the following questions
    : :
    : : 1. Shall i delete? If yes delete, if not proceed further
    : : 2. If i do no delete shall i print the document?
    : : If yes, then it should ask print original or print not original?
    : : if print original then an other code must be executed namely "visible invoice" and then the function Fncprint
    : : if not original then open the report in preview and then execute the fucntion Print
    : :
    : : 3. If i do not want to delete nor to print,do i just open in preview mode?
    : : if yes then i must ask original or not
    : : if original then i must add the function visible invocie
    : :
    : : However, when i want just to open the report, the function executes a print command.I am afraid my end if clauses are
    : : so confused that i canotn see what is wrong.
    : : Can somebody advise me of a better way for doing it?
    : :
    : : Below is my function:
    : :
    : :
    : : Public Function ProcessInvoice()
    : : ' the function is placed in the control CmdInvoices being the output for the ListInvoices
    : : Dim f As Form
    : : Set f = Forms![main]
    : :
    : : If IsNull(f![ListInvoices]) Then
    : : DoCmd.Beep
    : : MsgBox " Please select Invoice first ! "
    : : Exit Function
    : : Else
    : : Dim stLinkCriteria As String
    : : Dim stDocName As String
    : : stDocName = "Invoice"
    : : Dim intOriginal As Integer
    : : stLinkCriteria = "paymentid = " & f![ListInvoices]
    : :
    : : Dim intPrint As Integer
    : : If MsgBox(" Delete? ", vbQuestion + vbYesNo) = vbYes Then
    : : Application.Echo False
    : : Call CancelInvoice
    : : Else
    : : intOriginal = MsgBox(" original? ? ", vbQuestion + vbYesNo)
    : : intPrint = MsgBox("Print ?", vbQuestion + vbYesNo)
    : : If intOriginal = vbYes Then
    : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : VisibleInvoice
    : : Reports![invoice]![original].Visible = True
    : : Else
    : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : VisibleInvoice
    : : End If
    : : If intPrint = vbYes Then
    : : FncPrint
    : : End If
    : : End If
    : : End If
    : : End Function
    : :
    : :
    : :
    : :
    : :
    : :
    :
    :

  • compuchipcompuchip Member Posts: 273
    I don't like GOTO statements either. But I use EXIT in cases like this, because I find it more clear than having a lot of End If's at the end of the function.
    But if you like, you can leave them in :)

    : Sometimes I understand why, other times I do not. Basically, I was always told that using a direct branching statement like GOTO or EXIT was a poor programming practice. But when you start nesting the IF statements in a function like the one below, you really have to do a lot more homework to get the point across. Still, old habits die hard in me, I guess. I can't help but work them out without branching. The only time I really use the Exit statements is just before an error handling procedure, where you really have no choice. This is something I haven't even thought of in years, because, like I said, I guess old habits die hard.
    :
    : : First, I'd like to give a tip for readability:
    : : First, use the .. command (replace the < and > by [ and ]).
    : : It shows your indention (if you had any) which makes the code more readable.
    : :
    : : I think I've found some error-sensitive code (in other words: the error is probably there but I was too lazy to test it :))
    : : Copy it to a Module in Access and see my commments
    : : [code]
    : :
    : : Public Function ProcessInvoice()
    : : ' the function is placed in the control CmdInvoices being the output for the ListInvoices
    : : Dim f As Form
    : : Set f = Forms![main]
    : :
    : : If IsNull(f![ListInvoices]) Then
    : : DoCmd.Beep
    : : MsgBox " Please select Invoice first ! "
    : : Exit Function
    : : ' (a) You don't need the Else here!
    : : ' Else
    : : End If
    : :
    : : Dim stLinkCriteria As String
    : : Dim stDocName As String
    : : stDocName = "Invoice"
    : : Dim intOriginal As Integer
    : : stLinkCriteria = "paymentid = " & f![ListInvoices]
    : :
    : : Dim intPrint As Integer
    : : If MsgBox(" Delete? ", vbQuestion + vbYesNo) = vbYes Then
    : : Application.Echo False
    : : Call CancelInvoice
    : : ' (b) Below I got rid of another Else, using Exit Function and End If, since the
    : : ' rest of the function was inside the Else ... End If
    : : Exit Function
    : : End If
    : :
    : : ' I think this is where the error is. You get a value for intPrint, but I can't
    : : ' see you using it anywhere.
    : : ' You should review this block of code
    : : intOriginal = MsgBox(" original? ? ", vbQuestion + vbYesNo)
    : : intPrint = MsgBox("Print ?", vbQuestion + vbYesNo)
    : : If intOriginal = vbYes Then
    : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : VisibleInvoice
    : : Reports![invoice]![original].Visible = True
    : : Else
    : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : VisibleInvoice
    : : End If
    : :
    : : ' You can put these on one line :)
    : : If intPrint = vbYes Then FncPrint
    : :
    : : ' Skip it - See remark (b)
    : : ' End If
    : : ' Skip it - See remark (a)
    : : ' End If
    : : End Function
    : : [/code]
    : :
    : : : Can somebody help me with the Else If condition? I am confused.I need to build a function that asks the following questions
    : : :
    : : : 1. Shall i delete? If yes delete, if not proceed further
    : : : 2. If i do no delete shall i print the document?
    : : : If yes, then it should ask print original or print not original?
    : : : if print original then an other code must be executed namely "visible invoice" and then the function Fncprint
    : : : if not original then open the report in preview and then execute the fucntion Print
    : : :
    : : : 3. If i do not want to delete nor to print,do i just open in preview mode?
    : : : if yes then i must ask original or not
    : : : if original then i must add the function visible invocie
    : : :
    : : : However, when i want just to open the report, the function executes a print command.I am afraid my end if clauses are
    : : : so confused that i canotn see what is wrong.
    : : : Can somebody advise me of a better way for doing it?
    : : :
    : : : Below is my function:
    : : :
    : : :
    : : : Public Function ProcessInvoice()
    : : : ' the function is placed in the control CmdInvoices being the output for the ListInvoices
    : : : Dim f As Form
    : : : Set f = Forms![main]
    : : :
    : : : If IsNull(f![ListInvoices]) Then
    : : : DoCmd.Beep
    : : : MsgBox " Please select Invoice first ! "
    : : : Exit Function
    : : : Else
    : : : Dim stLinkCriteria As String
    : : : Dim stDocName As String
    : : : stDocName = "Invoice"
    : : : Dim intOriginal As Integer
    : : : stLinkCriteria = "paymentid = " & f![ListInvoices]
    : : :
    : : : Dim intPrint As Integer
    : : : If MsgBox(" Delete? ", vbQuestion + vbYesNo) = vbYes Then
    : : : Application.Echo False
    : : : Call CancelInvoice
    : : : Else
    : : : intOriginal = MsgBox(" original? ? ", vbQuestion + vbYesNo)
    : : : intPrint = MsgBox("Print ?", vbQuestion + vbYesNo)
    : : : If intOriginal = vbYes Then
    : : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : : VisibleInvoice
    : : : Reports![invoice]![original].Visible = True
    : : : Else
    : : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : : VisibleInvoice
    : : : End If
    : : : If intPrint = vbYes Then
    : : : FncPrint
    : : : End If
    : : : End If
    : : : End If
    : : : End Function
    : : :
    : : :
    : : :
    : : :
    : : :
    : : :
    : :
    : :
    :
    :

  • splansingsplansing Member Posts: 140
    You know, just last night at 3AM I was writing Exit statements, and feeling liberated.

    : I don't like GOTO statements either. But I use EXIT in cases like this, because I find it more clear than having a lot of End If's at the end of the function.
    : But if you like, you can leave them in :)
    :
    : : Sometimes I understand why, other times I do not. Basically, I was always told that using a direct branching statement like GOTO or EXIT was a poor programming practice. But when you start nesting the IF statements in a function like the one below, you really have to do a lot more homework to get the point across. Still, old habits die hard in me, I guess. I can't help but work them out without branching. The only time I really use the Exit statements is just before an error handling procedure, where you really have no choice. This is something I haven't even thought of in years, because, like I said, I guess old habits die hard.
    : :
    : : : First, I'd like to give a tip for readability:
    : : : First, use the .. command (replace the < and > by [ and ]).
    : : : It shows your indention (if you had any) which makes the code more readable.
    : : :
    : : : I think I've found some error-sensitive code (in other words: the error is probably there but I was too lazy to test it :))
    : : : Copy it to a Module in Access and see my commments
    : : : [code]
    : : :
    : : : Public Function ProcessInvoice()
    : : : ' the function is placed in the control CmdInvoices being the output for the ListInvoices
    : : : Dim f As Form
    : : : Set f = Forms![main]
    : : :
    : : : If IsNull(f![ListInvoices]) Then
    : : : DoCmd.Beep
    : : : MsgBox " Please select Invoice first ! "
    : : : Exit Function
    : : : ' (a) You don't need the Else here!
    : : : ' Else
    : : : End If
    : : :
    : : : Dim stLinkCriteria As String
    : : : Dim stDocName As String
    : : : stDocName = "Invoice"
    : : : Dim intOriginal As Integer
    : : : stLinkCriteria = "paymentid = " & f![ListInvoices]
    : : :
    : : : Dim intPrint As Integer
    : : : If MsgBox(" Delete? ", vbQuestion + vbYesNo) = vbYes Then
    : : : Application.Echo False
    : : : Call CancelInvoice
    : : : ' (b) Below I got rid of another Else, using Exit Function and End If, since the
    : : : ' rest of the function was inside the Else ... End If
    : : : Exit Function
    : : : End If
    : : :
    : : : ' I think this is where the error is. You get a value for intPrint, but I can't
    : : : ' see you using it anywhere.
    : : : ' You should review this block of code
    : : : intOriginal = MsgBox(" original? ? ", vbQuestion + vbYesNo)
    : : : intPrint = MsgBox("Print ?", vbQuestion + vbYesNo)
    : : : If intOriginal = vbYes Then
    : : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : : VisibleInvoice
    : : : Reports![invoice]![original].Visible = True
    : : : Else
    : : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : : VisibleInvoice
    : : : End If
    : : :
    : : : ' You can put these on one line :)
    : : : If intPrint = vbYes Then FncPrint
    : : :
    : : : ' Skip it - See remark (b)
    : : : ' End If
    : : : ' Skip it - See remark (a)
    : : : ' End If
    : : : End Function
    : : : [/code]
    : : :
    : : : : Can somebody help me with the Else If condition? I am confused.I need to build a function that asks the following questions
    : : : :
    : : : : 1. Shall i delete? If yes delete, if not proceed further
    : : : : 2. If i do no delete shall i print the document?
    : : : : If yes, then it should ask print original or print not original?
    : : : : if print original then an other code must be executed namely "visible invoice" and then the function Fncprint
    : : : : if not original then open the report in preview and then execute the fucntion Print
    : : : :
    : : : : 3. If i do not want to delete nor to print,do i just open in preview mode?
    : : : : if yes then i must ask original or not
    : : : : if original then i must add the function visible invocie
    : : : :
    : : : : However, when i want just to open the report, the function executes a print command.I am afraid my end if clauses are
    : : : : so confused that i canotn see what is wrong.
    : : : : Can somebody advise me of a better way for doing it?
    : : : :
    : : : : Below is my function:
    : : : :
    : : : :
    : : : : Public Function ProcessInvoice()
    : : : : ' the function is placed in the control CmdInvoices being the output for the ListInvoices
    : : : : Dim f As Form
    : : : : Set f = Forms![main]
    : : : :
    : : : : If IsNull(f![ListInvoices]) Then
    : : : : DoCmd.Beep
    : : : : MsgBox " Please select Invoice first ! "
    : : : : Exit Function
    : : : : Else
    : : : : Dim stLinkCriteria As String
    : : : : Dim stDocName As String
    : : : : stDocName = "Invoice"
    : : : : Dim intOriginal As Integer
    : : : : stLinkCriteria = "paymentid = " & f![ListInvoices]
    : : : :
    : : : : Dim intPrint As Integer
    : : : : If MsgBox(" Delete? ", vbQuestion + vbYesNo) = vbYes Then
    : : : : Application.Echo False
    : : : : Call CancelInvoice
    : : : : Else
    : : : : intOriginal = MsgBox(" original? ? ", vbQuestion + vbYesNo)
    : : : : intPrint = MsgBox("Print ?", vbQuestion + vbYesNo)
    : : : : If intOriginal = vbYes Then
    : : : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : : : VisibleInvoice
    : : : : Reports![invoice]![original].Visible = True
    : : : : Else
    : : : : DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria
    : : : : VisibleInvoice
    : : : : End If
    : : : : If intPrint = vbYes Then
    : : : : FncPrint
    : : : : End If
    : : : : End If
    : : : : End If
    : : : : End Function
    : : : :
    : : : :
    : : : :
    : : : :
    : : : :
    : : : :
    : : :
    : : :
    : :
    : :
    :
    :

  • Justin BibJustin Bib USAMember Posts: 0

    __ [ http://forcoder.org ] free ebooks and video tutorials about [ Scratch, R, Visual Basic .NET, Go, Python, Java, Assembly, JavaScript, C++, MATLAB, Perl, PL/SQL, Visual Basic, C#, Delphi, PHP, Ruby, Swift, Objective-C, C F#, ML, Ada, Crystal, Bash, VBScript, Erlang, FoxPro, Apex, Scala, Alice, COBOL, Rust, Logo, Lua, Clojure, LabVIEW, Dart, Julia, Awk, D, Prolog, Scheme, Kotlin, Hack, Lisp, Fortran, Transact-SQL, SAS, ABAP ] ____

Sign In or Register to comment.