Search Tools Links Login

Dynamically Create Databases (.MDB's) in code

Posted: 2002-06-01
By: ArchiveBot
Viewed: 78

Filed Under:

VB6 Code Cache

No attachments for this post


This code creates a Microsoft Access MDB dynamically.

Original Author: Kenny Sendel

Assumptions

This sample code will create a database in the temp directory with the following fields:
fldForeName
fldSurname
fldDOB
fldFurtherDetails

API Declarations

NONE

Code

''
'' PUT THIS BEHIND A COMMAND BUTTON TO TEST
''
' Declarations
Dim tdExample      As TableDef
Dim fldForeName     As Field
Dim fldSurname     As Field
Dim fldDOB       As Field
Dim fldFurtherDetails  As Field
Dim dbDatabase     As Database
Dim sNewDBPathAndName  As String
' Set the new database path and name in string (using time:seconds for some randomality
sNewDBPathAndName = "c: empNewDB" & Right$(Time, 2) & ".mdb"
' Create a new .MDB file (empty at creation point!)
Set dbDatabase = CreateDatabase(sNewDBPathAndName, dbLangGeneral, dbEncrypt)
' Create new TableDef (table called 'Example')
Set tdExample = dbDatabase.CreateTableDef("Example")
' Add fields to tdfTitleDetail.
Set fldForeName = tdExample.CreateField("Fore_Name", dbText, 20)
Set fldSurname = tdExample.CreateField("Surname", dbText, 20)
Set fldDOB = tdExample.CreateField("DOB", dbDate)
Set fldFurtherDetails = tdExample.CreateField("Further_Details", dbMemo)
' Append the field objects to the TableDef
tdExample.Fields.Append fldForeName
tdExample.Fields.Append fldSurname
tdExample.Fields.Append fldDOB
tdExample.Fields.Append fldFurtherDetails
' Save TableDef definition by appending it to TableDefs collection.
dbDatabase.TableDefs.Append tdExample
MsgBox "New .MDB Created - '" & sNewDBPathAndName & "'", vbInformation
' Now look for the new .MDB using File Manager!


Comments on this post

No comments have been added for this post.

You must be logged in to make a comment.