Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Creating Access 2007+ files

Creating Access 2007+ files

Scheduled Pinned Locked Moved Visual Basic
databasecomsecurityquestionannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JR212
    wrote on last edited by
    #1

    Hi, I try to create an access 2007 or higher file. I wrote this testcode just an simple form with 2 buttons.

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnMake.Click
    Dim dlg As New SaveFileDialog

    With dlg
      .Filter = "Access(\*.accdb)|\*.accdb"
      If .ShowDialog = DialogResult.OK Then
        Dim cat As New ADOX.Catalog()
        Try
          cat.Create("Provider=Microsoft.ACE.OLEDB.102.0;Data Source=" & .FileName & ";Persist Security Info=True")
          'cat.Create("Driver={Microsoft Access Driver (\*.mdb, \*.accdb)};DBQ=" & .FileName)
        Catch ex1 As Exception
          MsgBox(ex1.Message)
          If MsgBox("Is Microsoft Access Database Engine 2010 Redistributable installed?", vbQuestion Or MsgBoxStyle.YesNo) = MsgBoxResult.No Then
            Try
              Process.Start("https://www.microsoft.com/en-us/download/details.aspx?id=13255")              
              MsgBox("Do it :)", MsgBoxStyle.SystemModal)
            Catch ex2 As Exception
    
            End Try
          End If
        Finally
          cat = Nothing
        End Try
      End If
    End With
    

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
    Dim dlg As New OpenFileDialog

    With dlg
    
      .Filter = "Access(\*.accdb)|\*.accdb"
      If .ShowDialog = DialogResult.OK Then
    
        Dim OLEConnection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & .FileName & ";Persist Security Info=True")
        OLEConnection.Open()
    
        Dim OLECommand As New OleDb.OleDbCommand("", OLEConnection)
        ' Before this line you can create a string that holds your build for the table structure
        Randomize()
        Dim t As Int64 = Int(Rnd() \* 100000)
        Try
          OLECommand.CommandText = "CREATE TABLE mytable" & t & " (field1 CHAR,field2 NUMBER)"
          OLECommand.ExecuteNonQuery()
          MsgBox("Table " & t & " maded")
    
          OLECommand.CommandText = "insert into mytable" & t & " values(""" & t & """, " & t & ")"
          OLECommand.ExecuteNonQuery()
    
          OLECommand.Connection.Close()
        Catch ex As Exception
          MsgBox(ex.Message)
        End Try
    
      End If
    End With
    

    End Sub

    Create a table works. However making a file Always fails. Can anyone gives me idaes? I've tryed installing the 32 and 64 bit version of Microsoft Access Database Engine 2010 Redistributable I'm working on a W10 machine 64bit

    L M 3 Replies Last reply
    0
    • J JR212

      Hi, I try to create an access 2007 or higher file. I wrote this testcode just an simple form with 2 buttons.

      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnMake.Click
      Dim dlg As New SaveFileDialog

      With dlg
        .Filter = "Access(\*.accdb)|\*.accdb"
        If .ShowDialog = DialogResult.OK Then
          Dim cat As New ADOX.Catalog()
          Try
            cat.Create("Provider=Microsoft.ACE.OLEDB.102.0;Data Source=" & .FileName & ";Persist Security Info=True")
            'cat.Create("Driver={Microsoft Access Driver (\*.mdb, \*.accdb)};DBQ=" & .FileName)
          Catch ex1 As Exception
            MsgBox(ex1.Message)
            If MsgBox("Is Microsoft Access Database Engine 2010 Redistributable installed?", vbQuestion Or MsgBoxStyle.YesNo) = MsgBoxResult.No Then
              Try
                Process.Start("https://www.microsoft.com/en-us/download/details.aspx?id=13255")              
                MsgBox("Do it :)", MsgBoxStyle.SystemModal)
              Catch ex2 As Exception
      
              End Try
            End If
          Finally
            cat = Nothing
          End Try
        End If
      End With
      

      End Sub

      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
      Dim dlg As New OpenFileDialog

      With dlg
      
        .Filter = "Access(\*.accdb)|\*.accdb"
        If .ShowDialog = DialogResult.OK Then
      
          Dim OLEConnection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & .FileName & ";Persist Security Info=True")
          OLEConnection.Open()
      
          Dim OLECommand As New OleDb.OleDbCommand("", OLEConnection)
          ' Before this line you can create a string that holds your build for the table structure
          Randomize()
          Dim t As Int64 = Int(Rnd() \* 100000)
          Try
            OLECommand.CommandText = "CREATE TABLE mytable" & t & " (field1 CHAR,field2 NUMBER)"
            OLECommand.ExecuteNonQuery()
            MsgBox("Table " & t & " maded")
      
            OLECommand.CommandText = "insert into mytable" & t & " values(""" & t & """, " & t & ")"
            OLECommand.ExecuteNonQuery()
      
            OLECommand.Connection.Close()
          Catch ex As Exception
            MsgBox(ex.Message)
          End Try
      
        End If
      End With
      

      End Sub

      Create a table works. However making a file Always fails. Can anyone gives me idaes? I've tryed installing the 32 and 64 bit version of Microsoft Access Database Engine 2010 Redistributable I'm working on a W10 machine 64bit

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      jan212r wrote:

      Create a table works. However making a file Always fails.

      That does not make sense. You cannot create the table without first creating the file.

      1 Reply Last reply
      0
      • J JR212

        Hi, I try to create an access 2007 or higher file. I wrote this testcode just an simple form with 2 buttons.

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnMake.Click
        Dim dlg As New SaveFileDialog

        With dlg
          .Filter = "Access(\*.accdb)|\*.accdb"
          If .ShowDialog = DialogResult.OK Then
            Dim cat As New ADOX.Catalog()
            Try
              cat.Create("Provider=Microsoft.ACE.OLEDB.102.0;Data Source=" & .FileName & ";Persist Security Info=True")
              'cat.Create("Driver={Microsoft Access Driver (\*.mdb, \*.accdb)};DBQ=" & .FileName)
            Catch ex1 As Exception
              MsgBox(ex1.Message)
              If MsgBox("Is Microsoft Access Database Engine 2010 Redistributable installed?", vbQuestion Or MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                Try
                  Process.Start("https://www.microsoft.com/en-us/download/details.aspx?id=13255")              
                  MsgBox("Do it :)", MsgBoxStyle.SystemModal)
                Catch ex2 As Exception
        
                End Try
              End If
            Finally
              cat = Nothing
            End Try
          End If
        End With
        

        End Sub

        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
        Dim dlg As New OpenFileDialog

        With dlg
        
          .Filter = "Access(\*.accdb)|\*.accdb"
          If .ShowDialog = DialogResult.OK Then
        
            Dim OLEConnection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & .FileName & ";Persist Security Info=True")
            OLEConnection.Open()
        
            Dim OLECommand As New OleDb.OleDbCommand("", OLEConnection)
            ' Before this line you can create a string that holds your build for the table structure
            Randomize()
            Dim t As Int64 = Int(Rnd() \* 100000)
            Try
              OLECommand.CommandText = "CREATE TABLE mytable" & t & " (field1 CHAR,field2 NUMBER)"
              OLECommand.ExecuteNonQuery()
              MsgBox("Table " & t & " maded")
        
              OLECommand.CommandText = "insert into mytable" & t & " values(""" & t & """, " & t & ")"
              OLECommand.ExecuteNonQuery()
        
              OLECommand.Connection.Close()
            Catch ex As Exception
              MsgBox(ex.Message)
            End Try
        
          End If
        End With
        

        End Sub

        Create a table works. However making a file Always fails. Can anyone gives me idaes? I've tryed installing the 32 and 64 bit version of Microsoft Access Database Engine 2010 Redistributable I'm working on a W10 machine 64bit

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        You are missing quite a bit of logic in creating the database. The sequence of events should be:

        string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", tbFilename.Text);

        // Create the catalog
        ADOX.Catalog cat = new ADOX.Catalog();
        cat.Create(connectionString);

        // create a new table
        ADOX.Table adoxTable = new ADOX.Table();
        adoxTable.Name =

        // add columns to the table
        ADOX.DataTypeEnum dbType = adoxTable.Columns.Append(, dbType);

        // add the Table to the catalog
        cat.Tables.Append(adoxTable);

        // Close the database - thus creating the physical file
        ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
        con.Close();

        J 1 Reply Last reply
        0
        • J JR212

          Hi, I try to create an access 2007 or higher file. I wrote this testcode just an simple form with 2 buttons.

          Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnMake.Click
          Dim dlg As New SaveFileDialog

          With dlg
            .Filter = "Access(\*.accdb)|\*.accdb"
            If .ShowDialog = DialogResult.OK Then
              Dim cat As New ADOX.Catalog()
              Try
                cat.Create("Provider=Microsoft.ACE.OLEDB.102.0;Data Source=" & .FileName & ";Persist Security Info=True")
                'cat.Create("Driver={Microsoft Access Driver (\*.mdb, \*.accdb)};DBQ=" & .FileName)
              Catch ex1 As Exception
                MsgBox(ex1.Message)
                If MsgBox("Is Microsoft Access Database Engine 2010 Redistributable installed?", vbQuestion Or MsgBoxStyle.YesNo) = MsgBoxResult.No Then
                  Try
                    Process.Start("https://www.microsoft.com/en-us/download/details.aspx?id=13255")              
                    MsgBox("Do it :)", MsgBoxStyle.SystemModal)
                  Catch ex2 As Exception
          
                  End Try
                End If
              Finally
                cat = Nothing
              End Try
            End If
          End With
          

          End Sub

          Private Sub Button2_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
          Dim dlg As New OpenFileDialog

          With dlg
          
            .Filter = "Access(\*.accdb)|\*.accdb"
            If .ShowDialog = DialogResult.OK Then
          
              Dim OLEConnection As New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & .FileName & ";Persist Security Info=True")
              OLEConnection.Open()
          
              Dim OLECommand As New OleDb.OleDbCommand("", OLEConnection)
              ' Before this line you can create a string that holds your build for the table structure
              Randomize()
              Dim t As Int64 = Int(Rnd() \* 100000)
              Try
                OLECommand.CommandText = "CREATE TABLE mytable" & t & " (field1 CHAR,field2 NUMBER)"
                OLECommand.ExecuteNonQuery()
                MsgBox("Table " & t & " maded")
          
                OLECommand.CommandText = "insert into mytable" & t & " values(""" & t & """, " & t & ")"
                OLECommand.ExecuteNonQuery()
          
                OLECommand.Connection.Close()
              Catch ex As Exception
                MsgBox(ex.Message)
              End Try
          
            End If
          End With
          

          End Sub

          Create a table works. However making a file Always fails. Can anyone gives me idaes? I've tryed installing the 32 and 64 bit version of Microsoft Access Database Engine 2010 Redistributable I'm working on a W10 machine 64bit

          M Offline
          M Offline
          Maciej Los
          wrote on last edited by
          #4

          Check this: [Create Microsoft Access Database Programmatically using VB.NET](https://www.codeproject.com/Tips/813055/Create-Microsoft-Access-Database-Programmatically)

          1 Reply Last reply
          0
          • L Lost User

            You are missing quite a bit of logic in creating the database. The sequence of events should be:

            string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}", tbFilename.Text);

            // Create the catalog
            ADOX.Catalog cat = new ADOX.Catalog();
            cat.Create(connectionString);

            // create a new table
            ADOX.Table adoxTable = new ADOX.Table();
            adoxTable.Name =

            // add columns to the table
            ADOX.DataTypeEnum dbType = adoxTable.Columns.Append(, dbType);

            // add the Table to the catalog
            cat.Tables.Append(adoxTable);

            // Close the database - thus creating the physical file
            ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
            con.Close();

            J Offline
            J Offline
            JR212
            wrote on last edited by
            #5

            Thanks both for the help. My sequence is the same as yours :) Make file add table The difference is for making the tables: I use sql. But it seems that making files won't work with framework 2. Now with only changing to 4.52 it works Jan

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups