Results 1 to 4 of 4

Thread: [RESOLVED] Access 2007 database creation at runtime

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    37

    Resolved [RESOLVED] Access 2007 database creation at runtime

    Hia all,

    In the past I've used this code to create a database at runtime :-
    Code:
           Dim Cat As New Catalog
            Dim Cn As New ADODB.Connection
            Dim objTable As New ADOX.Table
    
            ' Create Database
            Cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Data\MyNewDB.mdb;" & "Jet OLEDB:Engine Type=5")
            Cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Data\MyNewDB.mdb")
            Cat.ActiveConnection = Cn
    
            ' Create Tables
            objTable.Name = "EmpMas"
    
            objTable.Columns.Append("column1")
    
            Cat.Tables.Append(objTable)
    
            ' Release objects and Free memory
            objTable = Nothing
            Cat = Nothing
            Cn.Close()
            Cn = Nothing
    
            MsgBox("Created")
    I've been unable to find anything similar for OLEDB & .accdb files. Has anyone been working with anything like this that can point me in the right direction ?

    Thanks

    Charlie

  2. #2
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Access 2007 database creation at runtime

    Try this out, requires a reference to ADOX.

    Code:
    Private adoxFileName As String = Application.StartupPath & "\AdoxDatabase.mdb"
    Private ConnectionString As String = String.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};", adoxFileName)
    Code:
    If Not IO.File.Exists(adoxFileName) Then
        CreateNewAccessDatabase(adoxFileName)
    End If
    Code:
    Sub CreateNewAccessDatabase(ByVal FileName As String)
        Dim dbCatalog As New ADOX.Catalog()
        dbCatalog.Create(ConnectionString)
    End Sub

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,714

    Re: Access 2007 database creation at runtime

    Create database, table and populate
    Code:
    If Not IO.File.Exists(adoxFileName) Then
        CreateNewAccessDatabase(adoxFileName)
    End If
    
    Using cn As New OleDb.OleDbConnection(ConnectionString)
        Dim cmd As New OleDb.OleDbCommand( _
        <text>
            CREATE TABLE persons ([Identifier] int identity ,
                                  UserName NVarchar(50), 
                                  [Password] NVarchar(50),
                                  CONSTRAINT [pk_AutoId] PRIMARY KEY ([Identifier])) 
        </text>.Value, cn)
    
        cn.Open()
    
        Try
            cmd.ExecuteNonQuery()
        Catch ex As OleDb.OleDbException
            MessageBox.Show(ex.Message, "OleDbException")
            Exit Sub
        Catch ex As Exception
            MessageBox.Show(ex.Message, "GeneralException")
            Exit Sub
        End Try
    
        Application.DoEvents()
    
        cmd.CommandText = _
        <Text>
            INSERT INTO persons 
            (UserName,[Password])
            VALUES 
            ("Gallagher","pass1word")
        </Text>.Value
        cmd.ExecuteNonQuery()
    
        cmd.CommandText = _
        <Text>
            INSERT INTO persons 
            (UserName,[Password])
            VALUES 
            ("Jones","Fluffy2")
        </Text>.Value
        cmd.ExecuteNonQuery()
    
        cmd.CommandText = _
        <Text>
            INSERT INTO persons 
            (UserName,[Password])
            VALUES 
            ("Smith","abv123w")
        </Text>.Value
        cmd.ExecuteNonQuery()
    
        cmd.CommandText = _
        <Text>
            INSERT INTO persons 
            (UserName,[Password])
            VALUES 
            ("Student1","College")
        </Text>.Value
        cmd.ExecuteNonQuery()
    
        cmd.CommandText = _
        <Text>
            INSERT INTO persons 
            (UserName,[Password])
            VALUES 
            ("Gallagher","pass1word")
        </Text>.Value
        cmd.ExecuteNonQuery()
    
        cmd.CommandText = _
        <Text>
            INSERT INTO persons 
            (UserName,[Password])
            VALUES 
            ("Simpson","Homer1")
        </Text>.Value
        cmd.ExecuteNonQuery()
    End Using

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2011
    Posts
    37

    Re: Access 2007 database creation at runtime

    Thats brilliant - thanks !!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width