Back To Top

MS Access VBA Create Database for versions 2000, 2002, 2003, 2007

Go here To Check if Table exists in VBA.
Go here To MS Access Create a Table in VBA
Follow the Delete a Table with VBA Link to delete a Table in VBA.


The following Ms Access VBA Create Database code snippet will use VBA to Make a new empty database.

The VBA Create Ms Access Database Sample is available for download here in a zipped MS Access Database.

You should be able to copy/paste the below code right into a Module.




Function
CreateDatabase( Optional strFile As String ) As Boolean
  
'References: Microsoft Access 11.0 Object Library, Microsoft DAO 3.6 Object Library
   'Set references by Clicking Tools and Then References in the Code View window
   ' Creates an empty MS Access Database
   '
   ' Accepts
   ' strFile: Name the new MS Access Database and Path
   ' Defaults to current database path and newDB.mdb
   '
   ' Returns True on success, false otherwise
   'USAGE With Optional: CreateDatabase "C:\PathtoDatabase\YourDatabase.mdb"
   'USAGE With Out: CreateDatabase 

  
On Error GoTo errhandler

  
Dim AccApp As Access.Application
  
Dim strDeFi As String , strSetting As String

  
'Set string to Access menu Option
  
strSetting = "Default File Format"

  
Set AccApp = New Access.Application
   strDeFi = AccApp.GetOption(strSetting)

  
'Uncomment next line to Create a Database in MS Access 2000 Format 
   'Call AccApp.SetOption(strSetting, "9")

   'The following line Creates a Database in MS Access 2002-2003 Format 
   'comment out if using the one above
  
Call AccApp.SetOption(strSetting, "10" )

  
'Set File path and MS Access Database Name
  
If Len(strFile) = 0 Then
     
strFile = CurrentProject.path & "\newDB.mdb"
  
End If
  
'Begin the MS Access Database Creation
  
Call AccApp.NewCurrentDatabase(strFile)
  
Call AccApp.SetOption(strSetting, strDeFi)

  
'If no errors set to true
  
CreateDatabase = True

Exithere:

  
Set AccApp = Nothing

   Exit Function

errhandler:

  
'If there is asn error set False
  
CreateDatabase = False
   With
Err
      MsgBox
"Error " & .Number & vbCrLf & .Description, _
            vbOKOnly
Or vbCritical, "CreateDatabase"
  
End With

   Resume
Exithere
End Function



You may post this tutorial on your website or in a forum. If you do please maintain a Link to Eraseve AP.



For further MS Access tutorials go here: MS Access tutorials

 

Did you find what you were looking for?
What would you suggest?
mail@eraserve.com
Your Name:
Your Message:
Your Email:

    

 

Custom Search