none
VB2008 Combobox查詢問題 RRS feed

  • 問題

  • 親愛的各位大大:

            我有一個combobox1和1個Checkbox及兩個Button,其中Combobox1下拉式選項有A與B,當我剛開始formload的時後選Combobox1的A或B去查詢都沒問題(因為重新執行一次FormLoad)。checkbox1被選取時是combobox1無作用如下程式的ELSE。即全部查詢不分combobox1的A或B
    但是我一按重新查詢時它好像就會跳過Combobox1的選項執行ELSE全部查詢,請問我如何恢復有如剛開始formload的狀態?

    程式碼如下:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'button1是查詢
            
    If CheckBox1.Checked = 0 Then '0是沒有打勾
    Dim ws As New SqlDataAdapter(" SELECT  PURTA.TA001, PURTB.TB002, PURTB.TB003, PURTA.TA012,PURTA.TA003, PURTB.TB004, PURTB.TB005, PURTB.TB006,INVMB.MB064, INVMB.MB036, INVMB.MB039, PURTB.TB007,PURTB.TB011, PURTB.TB009, PURTB.TB016, PURTB.TB017,PURTB.TB032, INVMB.MB067, PURMA.MA002 FROM  PURTA INNER JOIN PURTB ON PURTA.TA001 = PURTB.TB001 AND    PURTA.TA002 = PURTB.TB002 INNER JOIN   INVMB ON PURTB.TB004 = INVMB.MB001 AND PURTB.TB005 = INVMB.MB002 LEFT JOIN  PURMA ON PURTB.TB010 = PURMA.MA001   where    (NOT EXISTS   (SELECT  *   FROM   dbo.PURTD WHERE  TD026 = dbo.PURTA.TA001 AND TD027 = dbo.PURTB.TB002 AND  TD028 = dbo.PURTB.TB003))  AND ((CONVERT(datetime,PURTA.TA003) ) between '" + DTP1.Value.Date + "' and  '" + DTP2.Value.Date + "')  and MB067='" + ComboBox1.SelectedItem + "' order by TA003 ASC, TA001 ASC ,TB002 ASC ,TB003 ASC,TB004 ASC", strDbCon)
    ws.Fill(hhs, "PURTC")
    myDatatable0 = hhs.Tables("PURTC")
    Else  '打勾是全選
    Dim ws As New SqlDataAdapter(" SELECT  PURTA.TA001, PURTB.TB002, PURTB.TB003, PURTA.TA012,PURTA.TA003, PURTB.TB004, PURTB.TB005, PURTB.TB006,INVMB.MB064, INVMB.MB036, INVMB.MB039, PURTB.TB007,PURTB.TB011, PURTB.TB009, PURTB.TB016, PURTB.TB017,PURTB.TB032, INVMB.MB067, PURMA.MA002 FROM  PURTA INNER JOIN PURTB ON PURTA.TA001 = PURTB.TB001 AND    PURTA.TA002 = PURTB.TB002 INNER JOIN   INVMB ON PURTB.TB004 = INVMB.MB001 AND PURTB.TB005 = INVMB.MB002 LEFT JOIN  PURMA ON PURTB.TB010 = PURMA.MA001   where (NOT EXISTS   (SELECT  *   FROM   dbo.PURTD WHERE  TD026 = dbo.PURTA.TA001 AND TD027 = dbo.PURTB.TB002 AND  TD028 = dbo.PURTB.TB003))  AND  ((CONVERT(datetime,PURTA.TA003) ) between '" + DTP1.Value.Date + "' and  '" + DTP2.Value.Date + "')   order by TA003 ASC, TA001 ASC ,TB002 ASC ,TB003 ASC,TB004 ASC", strDbCon)
    ws.Fill(hhs, "PURTC")
    myDatatable0 = hhs.Tables("PURTC")

    End If

     

    Private Sub Button6_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click  '是重新查詢
            Button1.Enabled = True
            myDatatable1.Clear() 
            myDatatable2.Clear()
            Button6.Enabled = False
            ComboBox1.Enabled = True
            ComboBox1.ResetText()
            CheckBox1.Refresh()

    感謝各位大大

     

     

    2010年8月9日 上午 03:25

解答

  • Hello,

    我猜原因是出在
    ComboBox1.ResetText() 這一行.

    刪掉該行, 改設您希望預設的項目, 例如
    ComboBox1.SelectedIndex = 0

    請看看是否有幫助.

    2010年8月9日 上午 04:24

所有回覆