none
Need help with 'unrecognized format error' when trying to create a database with the save file dialog RRS feed

  • Question

  • Hey this is my code for creating a database programmatically using Visual Basic. I have attempted to edit the code so that it tries the .accdb extension instead of .mdb however it doesn't really work either. It saves the file, but when I try to open using it Database Access, I receive an error that the format is unrecognized.

    ElseIf result = DialogResult.Yes Then
        Using databaseFile As New SaveFileDialog
            databaseFile.Filter = "Access Database File (*.mdb)|.mdb"
            databaseFile.ShowDialog()
            Dim writer As New IO.StreamWriter(databaseFile.FileName)
            writer.Close()
         End Using

    Anything that I am doing wrong? How can I get around this issue? I have installed the 2010 Access Database Engine redistributable. I have also tried creating the database programmatically without using the save file dialog and instead using this code below. It works perfectly but I would rather use the save file dialog. How do I go about this? Thank you all very much :)

    Dim creator As New ADOX.Catalog() creator.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\igerm\Documents\Computer Science\Computer Science Project\Visual Basic\Database Files\test.accdb") creator = Nothing

    Wednesday, January 8, 2020 11:52 AM

All replies

  • Hello,

    A better idea is to a) have a folder below the app folder with a blank database with no table or a blank database with empty tables then copy it to the selected location. Option B is to embed the database blank or with tables into the project resource and extract to the selected location.

    If you embed say a database named Database1.accdb in project resources then later extract a different name and path can be used e.g.

    Here the database in resources is access via My.Resources.Database1 (as that is the name used to embed it). Use File.WriteAllBytes to write to a location.

    Imports System.IO
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim someFileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Karen.accdb")
            File.WriteAllBytes(someFileName, My.Resources.Database1)
        End Sub
    End Class
    

    Using as SaveFileDialog

    File.WriteAllBytes(SaveFileDialog1.FileName, My.Resources.Database1)

    In project properties


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, January 8, 2020 1:07 PM
    Moderator
  • Hi, 

    Thank you for posting here.

    According to your question, you can use ADOX create method in SaveFileDialog to solve the problem.

    Here’s the code of my test.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim s As SaveFileDialog = New SaveFileDialog()
            s.Title = "SaveFile As..."
            s.Filter = "Access Documents (*.accdb)|*.accdb|Others Documents (*.*)|*.*;"
            s.InitialDirectory = "D:\\TestFile2\\"
           Dim filenames As String = ".accdb"
            s.FileName = filenames
    
            If s.ShowDialog() <> DialogResult.Cancel Then
                Dim cat As ADOX.Catalog = New ADOX.Catalog()
                cat.Create("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & s.FileName & ";")
                MessageBox.Show("Create the database successfully")
            End If
        End Sub
    

    Result of my test.

    Hope it can help you.

    Best Regards,

    Xingyu Zhao



    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, January 9, 2020 6:22 AM
    Moderator