I have a un-explain problem when work with VBA and VB.NET. I start a project in VBA with a necessary of access data. I used ADO (Active Data Object 6.1), everything work fine with this example code:
Option Explicit
Private uConnect As ADODB.Connection
Private uCommand As ADODB.Command
Private uRecordset As ADODB.Recordset
Public Sub openConnection()
On Error GoTo ERRHANDLE:
Dim sConnection As String
sConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & ThisWorkbook.Path & "\" & "database.xlsx;" & _
"Extended Properties=Excel 12.0;"
'declare
Set uConnect = New ADODB.Connection 'give memory
uConnect.ConnectionString = sConnection
uConnect.Open
ERRHANDLE:
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical, "Error"
End If
End Sub
When I start another project with VB.NET with same situation. First I try COM reference to ADO ( active data object 6.1 ) or using assembly reference ADODB both not work with error "Provider not found", code:
Public Class Input
Private Const connstring As String = "Provider=Microsoft.ACE.OLEDB.12.0;" &
"Data Source=" & sSource & ";" &
"Extended Properties=Excel 12.0;"
Private Sub btnGetData_Click(sender As Object, e As EventArgs) Handles btnGetData.Click
Dim cn As New Connection
Dim da As New Recordset
Dim sSQL As String
Try
My.Computer.Audio.PlaySystemSound(SystemSounds.Exclamation)
da = New Recordset
cn = New Connection
cn.ConnectionString = connstring
cn.Open()
MessageBox.Show(cn.State.ToString)
cn.Close()
Catch ex As Exception
MessageBox.Show(ex.Message, "Database Input Error")
End Try
End Sub
End Class
My system win 10. 64 bit. office 64 bit.
I found in internet the solution is to install, Access Database Engine. Problem solve for the second case. But why there is this difference in VBA and VB.NET, why VBA do not need to install more engine to run.
This is not a direct answer, but it looks like youre trying to move from vba to vb.net with database connections. Here is waht I use in a couple of different vb.net projects. Hopefully itll help you transition smoothly.
I learned the hard way that not everything in vba directly translates into vb
Imports System.Data.OleDb
Dim provider As String
Dim dataFile As String
Dim connString As String
Public myConnection As OleDbConnection = New OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = "pathto.accdb"
connString = provider & dataFile
myConnection.ConnectionString = connString
End Sub
Private Sub NextSub()
str = "sql statement;"
Dim cmd As OleDbCommand = New OleDbCommand(str, myConnection)
dr = cmd.ExecuteReader
While dr.Read()
'do stuff with teh reader
End While
myConnection.Close()
End Sub
Sorry for posting in an old post!
I solve the same problem by install database engine provided in this link
Worked on my win10 64bit machine.
Hope this help anyone have the same problem.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.