habakay Posted May 5, 2016 Posted May 5, 2016 Hi, I'm new at VB.NET. I want to create Access database which is at the same folder with my active drawing. Here is my codes: Public Class UserControl1 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim AcadApp As Application Dim AcadDoc As Document AcadDoc = AcadApp.DocumentManager.MdiActiveDocument Dim hs As HostApplicationServices = HostApplicationServices.Current Dim DwgPath As String = hs.FindFile(AcadDoc.Name, AcadDoc.Database, FindFileHint.Default) Dim FolderPath As String FolderPath = Path.GetDirectoryName(DwgPath) & "\testDB.accdb" CreateAccessDatabase(FolderPath) End Sub Public Function CreateAccessDatabase(ByVal DatabaseFullPath As String) As Boolean Dim bAns As Boolean Dim cat As New ADOX.Catalog() Try Dim sCreateString As String sCreateString = "Microsoft.ACE.OLEDB.12.0;Data Source=" & DatabaseFullPath [color=red]cat.Create(sCreateString)[/color] bAns = True Catch Excep As System.Runtime.InteropServices.COMException bAns = False Finally cat = Nothing End Try Return bAns End Function End Class But it gives an InvalidCastException Error at "cat.Create(sCreateString)" line I use VS2015, Office 2013. I add "Microsoft ADO Ext. 6.0 for DLL and Security" reference from COM References and add "Imports System.Data.OleDb"to my project I could not figure out where the error. Quote
habakay Posted May 11, 2016 Author Posted May 11, 2016 I found the answer finally. I forgot to write "Provider=" word at that line before "Microsoft..." ... sCreateString = "Microsoft.ACE.OLEDB.12.0;Data Source=" & DatabaseFullPath ... Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.