2014-11-17 57 views
0

代碼似乎是非常好的,好的組織也順序跟隨是好的,所以問題在哪裏?請幫我解決這個問題。它不能通過布爾函數

它從來沒有達到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 
+1

解決這些類型問題的最簡單方法之一是在代碼的開始處放置一個breakpiont - 然後您可以遍歷代碼並檢查要測試的值以查看發生了什麼(或不發生)以及爲什麼。 – Tim

+1

您的函數在所有代碼路徑上都沒有'Return'值。另外:[Select Case](http://msdn.microsoft.com/en-us/library/cy37t14y.aspx)。 –

+0

有一個返回值! –

回答

1

我想建議你使用「開關罩」,而不是嵌套的「IF」。

程序的性能會得到改善,您也可以更好地理解邏輯。

2

您沒有達到MsgBox(「2」),因爲您的函數savefnn()未返回true。在沒有獲得預期結果的情況下,您可以在代碼中使用breakpoints進行調試。您也可以應用watch查找當前值。

它更好用選擇案例爲您正在使用的嵌套類型。