<% Option Explicit Class OFCDB Public DBName Public MetaDBName Public MetaTableName Private Conn, MetaConn Private RS Private SQL Private TableName(10) Private NumTables Private Sub Class_Initialize() MetaDBName = "OFC.MDB" DBName = "test.mdb" MetaTableName = "tblMeta" End Sub Private Sub Class_Terminate() RS.close MetaConn.Close Conn.Close End Sub ' get table names Private sub GetTableName dim i sql = "SELECT distinct myTableName FROM " & MetaTableName & ";" Set rs= Server.CreateObject("ADODB.Recordset") rs.open sql, MetaConn, 1, 1 ' the array, TableName, is predifined that has 10 elements ' if the actual number of tables greater than 10 ' redim the array to accommodate more table name if rs.recordcount > 10 then redim TableName(rs.recordcount) i = 1 do while not (rs.eof or rs.bof) TableName(i)=rs("myTableName") rs.movenext i = i + 1 loop NumTables = i -1 rs.close set rs=nothing End Sub ' Create a new database Public Sub NewDB Dim appAccess, dbs Dim strDB dim i,j ' Initialize string to database path. strDB = server.mappath(DBNAME) ' Create new instance of Microsoft Access. Set appAccess = CreateObject("Access.Application.9") ' Open database in Microsoft Access window. appAccess.NewCurrentDatabase strDB ' Get Database object variable. Set dbs = appAccess.CurrentDb ' Create tables according to definitions in META DataBese OpenMetaDB GetTableName for i = 1 to NumTables ' Create new table. sql = "SELECT distinct * FROM " & MetaTableName & " WHERE mytablename= '" & TableName(i) & "';" Set rs= Server.CreateObject("ADODB.Recordset") rs.open sql, MetaConn, 1, 1 SQL = "CREATE TABLE " & TableName(i) & " (" j = 1 do while not (rs.eof or rs.bof) ' create SQL command if j > 1 then SQL = SQL + ", " SQL = SQL + rs("myFieldName") + " " + rs("myFieldType") + " " if rs("myFieldSize")<>0 then SQL = SQL + "(" + cstr(rs("myFieldSize")) +")" if rs("myPrimarykey") then SQL =SQL + " Primary key " ' move to next record rs.movenext j = j + 1 loop SQL = SQL + ");" rs.close set rs=nothing dbs.Execute SQL next CloseMetaDB ' close the new created database appAccess.CloseCurrentDataBase set dbs = Nothing appAccess.Quit Set appAccess = Nothing End Sub Private Sub OpenMetaDB dim ConnStr connstr = "DBQ="+server.mappath(MetaDBName)+";defaultdir=;DRIVER={Microsoft Access Driver (*.mdb)};" set MetaConn=server.createobject("ADODB.Connection") MetaConn.open connstr End Sub Private Sub CloseMetaDB MetaConn.close set MetaConn = nothing End Sub Public Sub OpenDB Dim ConnStr connstr = "DBQ="+server.mappath(DBName)+";defaultdir=;DRIVER={Microsoft Access Driver (*.mdb)};Username=;Password=;" set conn=server.createobject("ADODB.Connection") conn.open connstr End Sub Public Sub CloseDB conn.close set conn = nothing End Sub End Class %> <html> <body> <% Dim myOFCDB Set MyOFCDB = New OFCDB MyOFCDB.DBName = "test1.mdb" MyOFCDB.NewDB %> Test1.mdb has been created successfully in your web server. </body> </html>
Download source code