Hi
I’m trying to create a datagrid based on results from an Access database. I have achieved this using SQL but my new project uses Access. Basically, the user selects a part number from the main form and then clicks on a results button. A new form appears and a datagrid is dynamically built showing all records in a table where the part number = the one selected.
I get a data type mismatch error on my bindingSource1.DataSource code.
Here’s my code
Has anyone got any ideas on how to achieve this?
Thanks
I’m trying to create a datagrid based on results from an Access database. I have achieved this using SQL but my new project uses Access. Basically, the user selects a part number from the main form and then clicks on a results button. A new form appears and a datagrid is dynamically built showing all records in a table where the part number = the one selected.
I get a data type mismatch error on my bindingSource1.DataSource code.
Here’s my code
Code:
Private Sub InitializeDataGridView()
Dim varSQL As String
varSQL = ("SELECT * FROM [tblCost] WHERE [Part Number] = " & frmMain.cboPartNumber.Text & " ORDER BY [Part Number]")
Dim AccessConn As New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & varSource & ";Persist Security Info=True;Jet OLEDB ")
AccessConn.Open()
Dim AccessCommandPartNoRetrieve As New System.Data.OleDb.OleDbCommand(varsql)
With AccessCommandPartNoRetrieve
.Connection = AccessConn
.CommandType = CommandType.Text
End With
Try
With Me.dataGridView1 'configure the DataGridView
'set the datagrid allow permissions
.AllowUserToAddRows = False
.AllowUserToDeleteRows = False
.AllowUserToOrderColumns = True
.AllowUserToResizeRows = True
.AutoGenerateColumns = True
bindingSource1.DataSource = AccessCommandPartNoRetrieve.ExecuteNonQuery
.DataSource = bindingSource1 'bind the datasource to the datagrid
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
.BorderStyle = BorderStyle.Fixed3D
.EditMode = DataGridViewEditMode.EditOnEnter
End With
If bindingSource1.Count < 1 Then 'no records
MsgBox("There are no records matching criteria", MsgBoxStyle.Exclamation, "Filter Results")
'Me.Close()
End If
AccessConn.Close()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, "Data results error")
System.Threading.Thread.CurrentThread.Abort()
End Try
End Sub
Has anyone got any ideas on how to achieve this?
Thanks