0

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")
Blackwood
  • 4,504
  • 16
  • 32
  • 41
David
  • 2,298
  • 6
  • 22
  • 56
  • There is too much code missing. The supplier case never returns true and you attempt to exit the try after returning false. In the RB6B case you call check on "SUPPLIER", which I can only assume is the code we're looking at, which is the same code that can never equal true.... And then you just exit sub.. – A Friend Aug 10 '16 at 16:27
  • @ProGrammer So, in the Try statement I need an `Else Return True`? – David Aug 10 '16 at 16:30

1 Answers1

2

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")
A Friend
  • 2,750
  • 2
  • 14
  • 23