Jump to content

Access Database Creation Problem


habakay
 Share

Recommended Posts

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.

Link to comment
Share on other sites

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

...

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...