我的组合框在选择语句中没有工作,如果我输入我之后的值,语句本身就可以工作。我认为组合框本身可能有问题,或者有人看到我的代码有问题吗?
Private Sub btnViewTimeTable_Click(sender As Object, e As EventArgs) Handles btnViewTimeTable.Click
If (cbDay.Text = "Monday" Or cbDay.Text = "Tuesday" Or cbDay.Text = "Wednesday" Or cbDay.Text = "Thursday" Or cbDay.Text = "Friday" Or cbDay.Text = "Saturday" Or cbDay.Text = "Sunday") Then
Dim SqlQuery As String = "SELECT Time, Activity, Equipment FROM TimeTable WHERE Day = 'cbDay.text'"
Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQuery, conn)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Table")
Dim dt As DataTable = ds.Tables("Table")
With DGTimeTable
.AutoGenerateColumns = True
.DataSource = ds
.DataMember = "Table"
End With
With DGTimeTable
.Columns(0).Width = 108
.Columns(1).Width = 154
.Columns(2).Width = 278
Me.DGTimeTable.DefaultCellStyle.WrapMode = DataGridViewTriState.True
Me.DGTimeTable.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
End With
Else
MsgBox("Please Select a Day!")
End If
End Sub
答案 0 :(得分:0)
更改这些初始行
Dim SqlQuery As String = "SELECT Time, Activity, Equipment " & _
"FROM TimeTable WHERE Day = ?"
Dim da As OleDbDataAdapter = New OleDbDataAdapter(SqlQuery, conn)
da.SelectCommand.Parameters.AddWithValue("@p1", cbDay.text)
Dim ds As DataSet = New DataSet
da.Fill(ds, "Table")
....
在原始代码中,您尝试在单引号之间传递组合的名称及其属性文本,但是这样文本被解释为升级,这意味着数据库引擎搜索列{{1}的记录包含字符串值Day
而不是组合框文本的实际值
还有另一个问题。您使用cboDay.text
作为数据库,因此单词TIME和DAY是MS-Access
中的保留关键字,除非将它们括在方括号中,否则无法将它们用作列名。如果仍有可能,我建议更改这些列的名称,因为每次使用此表时都会遇到这个烦人的问题。
Jet-Sql