How to display data from database based on selected combobox value

johnkosten

New member
Joined
Aug 28, 2005
Messages
2
Programming Experience
1-3
Hello,

I'm having trouble in displaying the information based on the combobox selection. Here is my code so far

Private Sub cmbBoxIncidentDate_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbBoxIncidentDate.SelectedIndexChanged
Dim dateValue As String
dateValue = cmbBoxIncidentDate.SelectedValue
Me.txtBoxIncidentType.Text = dateValue

'*------------------------------------------------------------------------------------------------------

Dim connString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Documents and Settings\Kasturi\My Documents\Visual Studio Projects\WindowsApplication1\db\test.mdb"
Dim Cmd As OleDbCommand
Dim Reader As OleDbDataReader
Dim SQL As String

Dim Con = New OleDbConnection(connString)

SQL = "SELECT * FROM [Incident] WHERE [Incident_Date] = " & dateValue & ""
Cmd = New OleDbCommand(SQL, Con)

Con.Open()


Con.Close()



End Sub

How do i reference the information needed to display in combo boxes? These are the fields in my database :- Incident_ID, Incident_Date, Incident_Name. Thanks in advance.
 
Note that if you want to specify a date literal in an SQL statement you should either enclose it in hash characters, e.g. #30/08/2005#, or use the DATEVALUE function (which may be Access-specific, although I'm not sure), e.g. DateValue('30/08/2005').

You can create a parameterised query and set the values of the parameters based on the selections from your ComboBoxes, e.g.
VB.NET:
Expand Collapse Copy
Dim selectCommand As New OleDbCommand("SELECT * FROM [Incident] WHERE [Incident_Date] = @Incident_Date", Con)

selectCommand.Parameters.Add("@Incident_Date", OleDbType.DBDate)

selectCommand.Parameters("@Incident_Date").Value = dateValue
Note that the command and parameter can be created at the class-level and thus reused. Then the only thing you need to do is set the parameter Value each time you want to perform a query.
 
Back
Top