David David - 3 months ago 15
Vb.net Question

Select Case & Try statement infinite loop

When trying to print a report in my vb.net project, I have some code to check whether all of the required fields have been filled in. If it isn't, a message box appears to notify the user. When they press 'OK', I need the program to stop executing the code to load the report. At the moment, it is stuck in an infinite loop, where it goes through a

Try
function and a
SELECT CASE
repeatedly instead.

What needs changing to stop this? I can't work out what the issue is, and why the following sections of code keep looping round one after the other

Try
Select Case checkwhat.ToUpper
Case "SUPPLIER"
If cmbSuppliers.Text.Trim = "" Then
MsgBox("Please select a supplier", MsgBoxStyle.OkOnly, "No Supplier Selected")
Return False
Exit Try
End If
Case "RB6B"
check("SUPPLIER")
If check("SUPPLIER") = True Then Else Exit Sub
createWorkTable("SUPPLIERS-TERRITORY-LS")
regReport("rTerritoryWTableCrosstabB.rpt", "", con, Me.MdiParent, cReport, True)
fillPms(cReport, "Sales by Territory by Supplier", "For " & cmbSuppliers.Text.Trim, "", "AOT02")

Answer

This is not a full answer but I can at least address some issues:

Select Case checkwhat.ToUpper
    Case "SUPPLIER"
        If cmbSuppliers.Text.Trim = "" Then
            MsgBox("Please select a supplier", MsgBoxStyle.OkOnly, "No Supplier Selected")
            Return False
            Exit Try '<- Redundant
        Else
            ' Perform other checks
            Return True
        End If

So this means you can at least get both return values assuming you enter the supplier case.

Now.. In your RB6B case, you run the check function twice.

Case "RB6B"    
    check("SUPPLIER")
    If check("SUPPLIER") = True Then

The two options you have are:

Dim supplierValid as Boolean = check("SUPPLIER")
If supplierValid = True

Or just removing the first line

If check("SUPPLIER") = True Then

Okay. So following that:

If check("SUPPLIER") = True Then Else Exit Sub

Try and avoid using this style, because it can hide code paths and you may miss some functionality without realising. Two options shown below. One avoids additional nesting

If check("SUPPLIER") = True Then  
    createWorkTable("SUPPLIERS-TERRITORY-LS")
    regReport("rTerritoryWTableCrosstabB.rpt", "", con, Me.MdiParent, cReport, True)
    fillPms(cReport, "Sales by Territory by Supplier", "For " & cmbSuppliers.Text.Trim, "", "AOT02")
Else 
    Exit Sub
End If

Or

If check("SUPPLIER") = False Then
    Exit Sub
End If  

createWorkTable("SUPPLIERS-TERRITORY-LS")
regReport("rTerritoryWTableCrosstabB.rpt", "", con, Me.MdiParent, cReport, True)
fillPms(cReport, "Sales by Territory by Supplier", "For " & cmbSuppliers.Text.Trim, "", "AOT02")
Comments