Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Too many IFs?

Status
Not open for further replies.

metrodub

Technical User
Dec 29, 2004
82
US
I have a form (frmProjects) for adding money to a PO fund for projects.

On the form I have a combobox (cboStatus) with three options ("Approved", "In Process", "Rejected").

When a user selects "Approved", the amount inputed (txtAmount) is added to the PO (txtPOAmount).

When a user selects "In Process", the amount inputed is added to the Pending Amount field (txtPendingAmount).

This is my code:
Code:
Private Sub cboStatus_AfterUpdate()
Dim intResponse As Integer
    If Me.cboStatus.Value = "Approved" Then
        intResponse = MsgBox("You are about to set the Project Status to Approved.  By doing so, you will add the Project Amount to the chosen PO.  Do you wish to continue?", vbYesNo, "Project Approval")
            If intResponse = vbYes Then
                Me.txtPOAmount = Me.txtPOAmount + Me.txtAmount
                Me.txtApprovalDate.SetFocus
             End If
    End If
    
    If Me.cboStatus.Value = "In Process" Then
        intResponse = MsgBox("You are about to set the Project Status to In Process.  By doing so, the PO's funds will not increase until the Status is set to Approved.  Continue?", vbYesNo, "Project Pending")
            If intResponse = vbYes Then
                Me.txtAmountPending = Me.txtAmountPending + Me.txtAmount
                Me.cmdSaveProject.SetFocus
            Else
                Me.cboStatus.SetFocus
            End If
    End If
End Sub

I separated the IF structures because when they were together the functionality disappeared (if I chose "In Process", nothing happened).

This was that code:
Code:
Private Sub cboStatus_AfterUpdate()
Dim intResponse As Integer
    If Me.cboStatus.Value = "Approved" Then
        intResponse = MsgBox("You are about to set the Project Status to Approved.  By doing so, you will add the Project Amount to the chosen PO.  Do you wish to continue?", vbYesNo, "Project Approval")
            If intResponse = vbYes Then
                Me.txtPOAmount = Me.txtPOAmount + Me.txtAmount
                Me.txtApprovalDate.SetFocus
    Else    
    If Me.cboStatus.Value = "In Process" Then
        intResponse = MsgBox("You are about to set the Project Status to In Process.  By doing so, the PO's funds will not increase until the Status is set to Approved.  Continue?", vbYesNo, "Project Pending")
            If intResponse = vbYes Then
                Me.txtAmountPending = Me.txtAmountPending + Me.txtAmount
                Me.cmdSaveProject.SetFocus
            Else
                Me.cboStatus.SetFocus
            End If
    End If
End If
End If
End Sub

Another functionality that I would like to add is if a user comes back to an "In Process" record and changes the status to "Approved", the amount in txtAmount is added to txtPOAmount and taken from txtAmountPending.

Would that be too many IF statements and, if not, where in the first embedded IF would I place the statement?

On a side note, would I code that statement as such?
Code:
If cboStatus.OldValue = "In Process" Then
       Me.txtPOAmount = Me.txtPOAmount + Me.txtAmount
       Me.txtAmountPending = Me.txtAmountPending - Me.txtAmount
 
Put that if statement inside the "If intResponse = vbYes" block. And no, you can never have too many if statements.

Your code should work fine if you lay it out like this:

Code:
If Me.cboStatus.Value = "Approved" Then
    ...code...
ElseIf Me.cboStatus.Value = "In Process" Then
    ...code...
ElseIf Me.cboStatus.Value = "Whatever Else" Then
    ...code...
End If 'done
 
looks like you might want to investigate select case. It could make it look a little cleaner for you


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top