I am able to successfully add my Excel "database" into my program. However, I'm a bit confused on how the Search function and Save feature work. I want to make it so that the user can edit text directly in the DataGridView and save their work (Which would save to the local excel file). I was following a tutorial, but my program is not working according to theirs, specifically the Search and Save button. Every time I type and click on "Search", I always get the error
Data type mismatch in criteria expression.
Link to tutorial I Was folloing: Save, Search, Edit Excel from VB.NET
Essentially, I want to make it so that the user can control the excel form through VB.NET. I know this seems a bit useless (Since the user can just open the excel file anyway...) but I'm just experimenting as of now (relatively new to VB.NET).
My code:
Imports System.Data.OleDb
Public Class Form1
Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim da As OleDbDataAdapter
Dim dt As New DataTable
Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs)
cn.Close()
End Sub
Private Sub FillDataGridView(ByVal Query As String)
da = New OleDbDataAdapter(Query, cn)
dt.Clear()
da.Fill(dt)
With DataGridView1
.DataSource = dt
.Columns(0).HeaderText = "Id"
.Columns(1).HeaderText = "Nama Agen"
.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
End With
End Sub
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Try
FillDataGridView("select * from [Data Agen$] where id='" & txtID.Text.ToString() & "'")
txtName.Text = dt.Rows(0).Item(1)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Try
With cm
.Connection = cn
.CommandText = "insert into [Data Agen$]values('" & txtID.Text & "','" & txtName.Text & "')"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [Data Agen$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
Return
End Try
MsgBox("Successfully updated!", MsgBoxStyle.Information, Text)
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
Try
With cm
.Connection = cn
.CommandText = "Update [Data Agen$] set [nama agen] = '" & txtName.Text & "' where id='" & txtID.Text & "'"
.ExecuteNonQuery()
End With
FillDataGridView("select * from [Data Agen$]")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
Return
End Try
MsgBox("Successfully updated!", MsgBoxStyle.Information, Text)
End Sub
Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
FillDataGridView("select * from [Data Agen$]")
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
cn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\Users\FEQH\Documents\Visual Studio 2012\Projects\exceltester\test.xls;Extended Properties=Excel 8.0;"
cn.Open()
FillDataGridView("select * from [Data Agen$]")
Button1.Visible = False
End Sub
End Class