Hello,
I created a simple dll in VB2005 to use ADO.Net -- oleDBDataAdapte r 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 WriteTestToExce l() Implements
_IWriteEntireLi stToExcel.Write TestExcel
Dim daOle As OleDbDataAdapte r, ds As DataSet
Dim connOle As OleDbConnection
Dim strPath As String
Try
strPath = "C:\1A\testa123 .xls"
connOle = New OleDbConnection
connOle.Connect ionString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" &
strPath & ";Extended Properties=""Ex cel 8.0;HDR=YES"""
ds = New DataSet
If connOle.State = ConnectionState .Closed Then connOle.Open()
daOle = New OleDbDataAdapte r
daOle.SelectCom mand = New OleDbCommand
daOle.SelectCom mand.Connection = connOle
daOle.SelectCom mand.CommandTex t = "Create Table tbl1 (fld1 varchar(10), fld2
varchar(10),
fld3 varchar(10))"
daOle.SelectCom mand.ExecuteNon Query()
daOle.SelectCom mand.CommandTex t = "Select * From [tbl1$]"
daOle.Fill(ds, "oleTbl1")
Dim dr As DataRow
For i As Integer = 0 To 3
dr = ds.Tables("oleT bl1").NewRow
dr(0) = "bill"
dr(1) = "Sue"
dr(2) = "Tim"
ds.Tables("oleT bl1").Rows.Add( dr)
Next
daOle.InsertCom mand = New OleDbCommand
daOle.InsertCom mand.Connection = connOle
daOle.InsertCom mand.CommandTex t = "Insert Into [tbl1$](fld1, fld2, fld3)
Select @fld1, @fld2, @fld3"
daOle.InsertCom mand.Parameters .Add("@fld1", OleDbType.VarCh ar, 10, "fld1")
daOle.InsertCom mand.Parameters .Add("@fld2", OleDbType.VarCh ar, 10, "fld2")
daOle.InsertCom mand.Parameters .Add("@fld3", OleDbType.VarCh ar, 10, "fld3")
daOle.Update(ds , "oleTbl1") '<--- bombs out here when called from Access
connOle.Close()
Catch ex As Exception
MsgBox("Error from WriteTestToExce l " & ex.Message)
End Try
End Sub
End Class
Thanks,
Rich
I created a simple dll in VB2005 to use ADO.Net -- oleDBDataAdapte r 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 WriteTestToExce l() Implements
_IWriteEntireLi stToExcel.Write TestExcel
Dim daOle As OleDbDataAdapte r, ds As DataSet
Dim connOle As OleDbConnection
Dim strPath As String
Try
strPath = "C:\1A\testa123 .xls"
connOle = New OleDbConnection
connOle.Connect ionString = "Provider=Micro soft.Jet.OLEDB. 4.0;Data Source=" &
strPath & ";Extended Properties=""Ex cel 8.0;HDR=YES"""
ds = New DataSet
If connOle.State = ConnectionState .Closed Then connOle.Open()
daOle = New OleDbDataAdapte r
daOle.SelectCom mand = New OleDbCommand
daOle.SelectCom mand.Connection = connOle
daOle.SelectCom mand.CommandTex t = "Create Table tbl1 (fld1 varchar(10), fld2
varchar(10),
fld3 varchar(10))"
daOle.SelectCom mand.ExecuteNon Query()
daOle.SelectCom mand.CommandTex t = "Select * From [tbl1$]"
daOle.Fill(ds, "oleTbl1")
Dim dr As DataRow
For i As Integer = 0 To 3
dr = ds.Tables("oleT bl1").NewRow
dr(0) = "bill"
dr(1) = "Sue"
dr(2) = "Tim"
ds.Tables("oleT bl1").Rows.Add( dr)
Next
daOle.InsertCom mand = New OleDbCommand
daOle.InsertCom mand.Connection = connOle
daOle.InsertCom mand.CommandTex t = "Insert Into [tbl1$](fld1, fld2, fld3)
Select @fld1, @fld2, @fld3"
daOle.InsertCom mand.Parameters .Add("@fld1", OleDbType.VarCh ar, 10, "fld1")
daOle.InsertCom mand.Parameters .Add("@fld2", OleDbType.VarCh ar, 10, "fld2")
daOle.InsertCom mand.Parameters .Add("@fld3", OleDbType.VarCh ar, 10, "fld3")
daOle.Update(ds , "oleTbl1") '<--- bombs out here when called from Access
connOle.Close()
Catch ex As Exception
MsgBox("Error from WriteTestToExce l " & ex.Message)
End Try
End Sub
End Class
Thanks,
Rich
Comment