Zdravím, mám prográmek na search, save, update a refresh záznamu z VB do excelu. Save mi neukládá data do excelu a při refresh, Search a Update mi to hází hlášku "Nesoulad datových typů ve výrazu". Zasílám i kód.
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.FormClosingEventArgs)
cn.Close()
End Sub
Private Sub Form1_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
\'OPEN CONNECTION TO test.xls
cn.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0; Data Source=c:\\2\\test.xls;Extended Properties=Excel 8.0;"
cn.Open()
\'Load file from test.xls into DataGridView1
FillDataGridView("select * from [Data Agen$]")
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_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSearch.Click
Try
FillDataGridView("select * from [Data Agen$] where id=\'" & TxtId.Text & "\'")
TxtName.Text = dt.Rows(0).Item(1)
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Critical, Text)
End Try
End Sub
Private Sub BtnSave_Click_1(ByVal sender As System.Object, ByVal e As System.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(ByVal sender As System.Object, ByVal e As System.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(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnRefresh.Click
\'Load file from test.xls into DataGridView1
FillDataGridView("select * from [Data Agen$]")
End Sub
End Class
Děkuju za pomoc. MArek