它不能传递布尔函数

时间:2014-11-17 01:06:11

标签: vb.net

代码看起来非常好,组织顺序也很好,所以问题出在哪里?

它永远不会到达msgbox(" 2")......

     Public Function savefnn() As Boolean
    If tco.Text = "" Then
        MsgBox("Please Select Product")
        tco.Focus()
    ElseIf tco.Text = "Bags" Then
        If tname.Text = "" Then
            MsgBox("Please Enter Name")
            tname.Focus()
        ElseIf ttype.Text = "" Then
            MsgBox("Please Choose Type")
            ttype.Focus()
        ElseIf Tcolor.Text = "" Then
            MsgBox("Please Choose Color")
            Tcolor.Focus()
        End If
    ElseIf tco.Text = "Balls" Then
        If ttype.Text = "" Then
            MsgBox("Please Choose Type")
            ttype.Focus()
        End If
    ElseIf tco.Text = "Blades" Then
        If tname.Text = "" Then
            MsgBox("Please Enter Name")
            tname.Focus()
        ElseIf ttype.Text = "" Then
            MsgBox("Please Choose Type")
            ttype.Focus()
        End If
    ElseIf tco.Text = "Glues" Or tco.Text = "Cleaners" Then
        If tname.Text = "" Then
            MsgBox("Please Enter Name")
            tname.Focus()
        ElseIf tsize.Text = "" Then
            MsgBox("Please Choose Size")
            tsize.Focus()
        End If
    ElseIf tco.Text = "Rubbers" Then
        If tname.Text = "" Then
            MsgBox("Please Enter Name")
            tname.Focus()
        ElseIf Tcolor.Text = "" Then
            MsgBox("Please Choose Color")
            Tcolor.Focus()
        End If
    ElseIf tco.Text = "Shoes" Or tco.Text = "Socks" Then
        If tname.Text = "" Then
            MsgBox("Please Enter Name")
            tname.Focus()
        ElseIf tsize.Text = "" Then
            MsgBox("Please Choose Size")
            tsize.Focus()
        ElseIf Tcolor.Text = "" Then
            MsgBox("Please Choose Color")
            Tcolor.Focus()
        End If
    ElseIf tco.Text = "Shorts" Or tco.Text = "TShirts" Or tco.Text = "Towels" Or tco.Text = "TrainigSuits" Then
        If tname.Text = "" Then
            MsgBox("Please Enter Name")
            tname.Focus()
        ElseIf tsize.Text = "" Then
            MsgBox("Please Choose Size")
            tsize.Focus()
        ElseIf Tcolor.Text = "" Then
            MsgBox("Please Choose Color")
            Tcolor.Focus()
        End If
    ElseIf tco.Text = "Tables" Or tco.Text = "Nets" Then
        If tname.Text = "" Then
            MsgBox("Please Enter Name")
            tname.Focus()
        End If
    Else

        Return True

    End If

End Function
Public Function isrecordvalid() As Boolean
    If tco.Text = "Bags" Then
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM [AllProducts] WHERE [ProductName] LIKE '%" & tname.Text & "%' And [Type] LIKE '%" & ttype.Text & "%' And [Color] LIKE '%" & Tcolor.Text & "%' ", cnxnString)
        Dim mytable As New DataTable
        myadapter.Fill(mytable)
        If mytable.Rows.Count > 0 Then
            MsgBox("Product Is Already Saved", vbCritical, "Error")
        End If
    ElseIf tco.Text = "Balls" Then
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM [AllProducts] WHERE [Type] LIKE '%" & ttype.Text & "%' ", cnxnString)
        Dim mytable As New DataTable
        myadapter.Fill(mytable)
        If mytable.Rows.Count > 0 Then
            MsgBox("Product Is Already Saved", vbCritical, "Error")
        End If
    ElseIf tco.Text = "Blades" Then
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM [AllProducts] WHERE [ProductName] LIKE '%" & tname.Text & "%' And [Type] LIKE '%" & ttype.Text & "%' ", cnxnString)
        Dim mytable As New DataTable
        myadapter.Fill(mytable)
        If mytable.Rows.Count > 0 Then
            MsgBox("Product Is Already Saved", vbCritical, "Error")
        End If
    ElseIf tco.Text = "Glues" Or tco.Text = "Cleaners" Then
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM [AllProducts] WHERE [ProductName] LIKE '%" & tname.Text & "%' And [Size] LIKE '%" & tsize.Text & "%'", cnxnString)
        Dim mytable As New DataTable
        myadapter.Fill(mytable)
        If mytable.Rows.Count > 0 Then
            MsgBox("Product Is Already Saved", vbCritical, "Error")
        End If
    ElseIf tco.Text = "Shoes" Or tco.Text = "Socks" Or tco.Text = "Shorts" Or tco.Text = "TShirts" Or tco.Text = "Towels" Or tco.Text = "TrainingSuits" Then
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM [AllProducts] WHERE [ProductName] LIKE '%" & tname.Text & "%' And [Size] LIKE '%" & tsize.Text & "%' And [Color] LIKE '%" & Tcolor.Text & "%'", cnxnString)
        Dim mytable As New DataTable
        myadapter.Fill(mytable)
        If mytable.Rows.Count > 0 Then
            MsgBox("Product Is Already Saved", vbCritical, "Error")
        End If
    ElseIf tco.Text = "Rubbers" Then
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM [AllProducts] WHERE [ProductName] LIKE '%" & tname.Text & "%' And [Color] LIKE '%" & Tcolor.Text & "%'", cnxnString)
        Dim mytable As New DataTable
        myadapter.Fill(mytable)
        If mytable.Rows.Count > 0 Then
            MsgBox("Product Is Already Saved", vbCritical, "Error")
        End If
    ElseIf tco.Text = "Tables" Or tco.Text = "Nets" Then
        Dim myadapter As New OleDbDataAdapter("SELECT * FROM [AllProducts] WHERE [ProductName] LIKE '%" & tname.Text & "%' ", cnxnString)
        Dim mytable As New DataTable
        myadapter.Fill(mytable)
        If mytable.Rows.Count > 0 Then
            MsgBox("Product Is Already Saved", vbCritical, "Error")
        End If
    Else
        Return True
    End If
End Function
Private Sub bs_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bs.Click
    MsgBox("1")
    If savefnn() = True Then
        MsgBox("2")
        If isrecordvalid() = True Then
            MsgBox("3")
            con.Open()
            sql = "SELECT * FROM AllProducts"
            da = New OleDb.OleDbDataAdapter(sql, con)
            da.Fill(ds, "AllProducts")
            da = New OleDb.OleDbDataAdapter(sql, con)
            con.Close()
            start = System.DateTime.Now.ToString
            Dim cb As New OleDb.OleDbCommandBuilder(da)
            cb.QuotePrefix = "["
            cb.QuoteSuffix = "]"
            Dim dsNewRow As DataRow
            dsNewRow = ds.Tables("AllProducts").NewRow()
            dsNewRow.Item("Product") = tco.Text
            If tname.Enabled = True Then
                dsNewRow.Item("ProductName") = tname.Text
            Else
                dsNewRow.Item("ProductName") = "None"
            End If
            If Button2.Enabled = True Then
                dsNewRow.Item("Image") = Label40.Text
            Else
                dsNewRow.Item("Image") = "None"
            End If
            If tch.Enabled = True Then
                dsNewRow.Item("Charac") = tch.Text
            Else
                dsNewRow.Item("Charac") = "None"
            End If
            If ttype.Enabled = True Then
                dsNewRow.Item("Type") = ttype.Text
            Else
                dsNewRow.Item("Type") = "None"
            End If
            If tsize.Enabled = True Then
                dsNewRow.Item("Size") = tsize.Text
            Else
                dsNewRow.Item("Size") = "None"
            End If
            If Tcolor.Enabled = True Then
                dsNewRow.Item("color") = Tcolor.Text
            Else
                dsNewRow.Item("Color") = "None"
            End If
            dsNewRow.Item("Date") = start
            ds.Tables("AllProducts").Rows.Add(dsNewRow)
            da.Update(ds, "AllProducts")
            s = 1
            MsgBox("New Product Saved")
            store = tname.Text
            store1 = start
            bus.Enabled = True
            clear1()
            MsgBox("4")
        End If
    End If
End Sub

2 个答案:

答案 0 :(得分:2)

你没有到达MsgBox(“2”),因为你的函数savefnn()没有返回true。如果您未获得预期结果,则可以在代码中使用 breakpoints 进行调试。您也可以应用 watch 来查找当前值。

最好使用 Select Case 来处理您正在使用的嵌套类型。

答案 1 :(得分:1)

我建议您使用'SWITCH CASE'而不是嵌套'IF'。

程序的性能会得到改善,你也可以更好地清除逻辑。