Create Empty Database?

G

Graham R Seach

Josh,

Public Function CreateNewDatabase(strDBName As String, _
CollatingOrder As CollatingOrderEnum, _
DatabaseType As DatabaseTypeEnum) As
Database
Dim ws As Workspace

'Get the default Workspace.
Set ws = DBEngine(0)

'Make sure there isn't already a database with the same filespec.
If Len(Dir(strDBName)) > 0 Then
DoCmd.Beep
If vbYes = MsgBox("A database with this name already exists." &
vbCrLf & _
"Do you want to replace it with the new
database?", _
vbYesNo + vbQuestion, "Duplicate database name")
Then

Kill strDBName
Else
Set CreateNewDatabase = Nothing
GoTo Proc_Exit
End If
End If

'Create the new database, and return a reference to it.
Set CreateNewDatabase = ws.CreateDatabase(strDBName, CollatingOrder,
DatabaseType)

Proc_Exit:
Set ws = Nothing
End Function

Regards,
Graham R Seach
Microsoft Access MVP
Canberra, Australia
 
A

Allen Browne

This example shows how to create a database and set its essential properties
(Access 2000 and later) to stop it corrupting:

Sub CreateDatabaseDAO()
Dim dbNew As DAO.Database
Dim prp As DAO.Property
Dim strFile As String

'Create the new database.
strFile = "C:\SampleDAO.mdb"
Set dbNew = DBEngine(0).CreateDatabase(strFile, dbLangGeneral)

'Create example properties in new database.
With dbNew
Set prp = .CreateProperty("Perform Name AutoCorrect", dbLong, 0)
.Properties.Append prp
Set prp = .CreateProperty("Track Name AutoCorrect Info", _
dbLong, 0)
.Properties.Append prp
End With

'Clean up.
dbNew.Close
Set prp = Nothing
Set dbNew = Nothing
Debug.Print "Created " & strFile
End Sub
 
G

Graham R Seach

Bugger!! I went from memory, but should have tested it:

Allan's code will work, and mine will too if you make the following changes:
Public Function CreateNewDatabase(strDBName As String, _
DatabaseType As DatabaseTypeEnum) As
Database

Set CreateNewDatabase = ws.CreateDatabase(strDBName, dbLangGeneral,
DatabaseType)

Regards,
Graham R Seach
Microsoft Access MVP
Canberra, Australia
---------------------------
 
Top