G
Guest
Hello,
I created a simple dll in VB2005 to use ADO.Net -- oleDBDataAdapter to write
data to an Excel file. The code (below) works fine from an Excel file
(invoke the dll from Excel to write to another Excel file)
But if I run the same dll (tlb) from MS Access - I get an error that I need
to use an updateable query. Why does the following code work fine in Excel
but not in MS Acces? Maybe because Excel doesn't use Jet? Any suggestions
appreciated what I can do to make this work from MS Access.
--------------------------------------------
Public Class ....
Public Sub WriteTestToExcel() Implements
_IWriteEntireListToExcel.WriteTestExcel
Dim daOle As OleDbDataAdapter, ds As DataSet
Dim connOle As OleDbConnection
Dim strPath As String
Try
strPath = "C:\1A\testa123.xls"
connOle = New OleDbConnection
connOle.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
strPath & ";Extended Properties=""Excel 8.0;HDR=YES"""
ds = New DataSet
If connOle.State = ConnectionState.Closed Then connOle.Open()
daOle = New OleDbDataAdapter
daOle.SelectCommand = New OleDbCommand
daOle.SelectCommand.Connection = connOle
daOle.SelectCommand.CommandText = "Create Table tbl1 (fld1 varchar(10), fld2
varchar(10),
fld3 varchar(10))"
daOle.SelectCommand.ExecuteNonQuery()
daOle.SelectCommand.CommandText = "Select * From [tbl1$]"
daOle.Fill(ds, "oleTbl1")
Dim dr As DataRow
For i As Integer = 0 To 3
dr = ds.Tables("oleTbl1").NewRow
dr(0) = "bill"
dr(1) = "Sue"
dr(2) = "Tim"
ds.Tables("oleTbl1").Rows.Add(dr)
Next
daOle.InsertCommand = New OleDbCommand
daOle.InsertCommand.Connection = connOle
daOle.InsertCommand.CommandText = "Insert Into [tbl1$](fld1, fld2, fld3)
Select @fld1, @fld2, @fld3"
daOle.InsertCommand.Parameters.Add("@fld1", OleDbType.VarChar, 10, "fld1")
daOle.InsertCommand.Parameters.Add("@fld2", OleDbType.VarChar, 10, "fld2")
daOle.InsertCommand.Parameters.Add("@fld3", OleDbType.VarChar, 10, "fld3")
daOle.Update(ds, "oleTbl1") '<--- bombs out here when called from Access
connOle.Close()
Catch ex As Exception
MsgBox("Error from WriteTestToExcel " & ex.Message)
End Try
End Sub
End Class
Thanks,
Rich
I created a simple dll in VB2005 to use ADO.Net -- oleDBDataAdapter to write
data to an Excel file. The code (below) works fine from an Excel file
(invoke the dll from Excel to write to another Excel file)
But if I run the same dll (tlb) from MS Access - I get an error that I need
to use an updateable query. Why does the following code work fine in Excel
but not in MS Acces? Maybe because Excel doesn't use Jet? Any suggestions
appreciated what I can do to make this work from MS Access.
--------------------------------------------
Public Class ....
Public Sub WriteTestToExcel() Implements
_IWriteEntireListToExcel.WriteTestExcel
Dim daOle As OleDbDataAdapter, ds As DataSet
Dim connOle As OleDbConnection
Dim strPath As String
Try
strPath = "C:\1A\testa123.xls"
connOle = New OleDbConnection
connOle.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
strPath & ";Extended Properties=""Excel 8.0;HDR=YES"""
ds = New DataSet
If connOle.State = ConnectionState.Closed Then connOle.Open()
daOle = New OleDbDataAdapter
daOle.SelectCommand = New OleDbCommand
daOle.SelectCommand.Connection = connOle
daOle.SelectCommand.CommandText = "Create Table tbl1 (fld1 varchar(10), fld2
varchar(10),
fld3 varchar(10))"
daOle.SelectCommand.ExecuteNonQuery()
daOle.SelectCommand.CommandText = "Select * From [tbl1$]"
daOle.Fill(ds, "oleTbl1")
Dim dr As DataRow
For i As Integer = 0 To 3
dr = ds.Tables("oleTbl1").NewRow
dr(0) = "bill"
dr(1) = "Sue"
dr(2) = "Tim"
ds.Tables("oleTbl1").Rows.Add(dr)
Next
daOle.InsertCommand = New OleDbCommand
daOle.InsertCommand.Connection = connOle
daOle.InsertCommand.CommandText = "Insert Into [tbl1$](fld1, fld2, fld3)
Select @fld1, @fld2, @fld3"
daOle.InsertCommand.Parameters.Add("@fld1", OleDbType.VarChar, 10, "fld1")
daOle.InsertCommand.Parameters.Add("@fld2", OleDbType.VarChar, 10, "fld2")
daOle.InsertCommand.Parameters.Add("@fld3", OleDbType.VarChar, 10, "fld3")
daOle.Update(ds, "oleTbl1") '<--- bombs out here when called from Access
connOle.Close()
Catch ex As Exception
MsgBox("Error from WriteTestToExcel " & ex.Message)
End Try
End Sub
End Class
Thanks,
Rich