Results 1 to 9 of 9

Thread: VB6/VBA Creating Access Database via Code

Threaded View

  1. #1

    Thread Starter
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    VB6/VBA Creating Access Database via Code

    Inspired by this thread, I finally decided to put together a seperate thread on this.

    You can create an MDB Access database either with DAO or ADO. DAO is almost obselete now but some people still use it so I am including both the codes.

    ADO

    Set Reference to the following
    Microsoft ActiveX Data Objects x.xx Library
    Microsoft ADO Ext. x.xx for DDL and Security


    vb Code:
    1. Sub ADOSample()
    2.     Dim adoCat As ADOX.Catalog, adoTable As ADOX.Table
    3.     Dim tblCollection As Collection
    4.     Dim Filenm As String, strConn As String
    5.    
    6.     '~~> MDB to be created. Change this to relevant path and filename
    7.     Filenm = "C:\Temp\MyDatabase.mdb"
    8.    
    9.     '~~> Creating an instance of the ADOX-object.
    10.     Set adoCat = New ADOX.Catalog
    11.     '~~> Creating an instance of Collection-object.
    12.     Set tblCollection = New Collection
    13.     Set adoTable = New ADOX.Table
    14.    
    15.     strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    16.               "Data Source=" & Filenm & ";"
    17.    
    18.     '~~> Deleting the existing copy of the database, if any.
    19.     On Error Resume Next
    20.     Kill Filenm
    21.     On Error GoTo 0
    22.      
    23.     '~~> Create the MDB.
    24.     adoCat.Create (strConn)
    25.    
    26.     '~~> Replace "Table1" with a name you like for your table
    27.     tblCollection.Add "Table1"
    28.      
    29.     With adoTable
    30.         '~~> Name the table.
    31.         .Name = "Table1"
    32.         '~~> Creating a field which also is the Primary Key
    33.         .Columns.Append "ID", adInteger
    34.         '~~> Set the Parent Catalog.
    35.         .ParentCatalog = adoCat
    36.         .Columns("ID").Properties("AutoIncrement").Value = True
    37.         '~~> Append the PrimaryKey
    38.         .Keys.Append "PrimaryKey", adKeyPrimary, "ID"
    39.                
    40.         '~~> Add rest of the fields... I have taken 4 types
    41.         .Columns.Append "intField1", adInteger  '~~ number
    42.         .Columns.Append "numField2", adNumeric  '~~ number, decimal
    43.         .Columns("numField2").Precision = 2
    44.         .Columns.Append "dateFiled3", adDate    '~~ Date
    45.         .Columns.Append "txtFiled4", adWChar    '~~ text
    46.     End With
    47.    
    48.     '~~> Finally add the Table to the MDB.
    49.     adoCat.Tables.Append adoTable
    50.    
    51.     '~~> Release the objects from the memory.
    52.     Set adoTable = Nothing
    53.     Set tblCollection = Nothing
    54.     Set adoCat = Nothing
    55.      
    56.     '~~> Inform user.
    57.     MsgBox "New .MDB Created - '" & Filenm & "'", vbInformation
    58. End Sub

    DAO

    Set reference to Microsoft DAO x.x Object Library

    vb Code:
    1. Sub DAOExample()
    2.     Dim tdefMDB As TableDef, txtFieldone As Field
    3.     Dim dateFieldone As Field, memoFieldone As Field, dbDatabase As Database
    4.     Dim sNewDBPathAndName As String
    5.    
    6.     '~~> MDB to be created. Change this to relevant path and filename
    7.     sNewDBPathAndName = "C:\Temp\MyDatabase.mdb"
    8.     Set dbDatabase = CreateDatabase(sNewDBPathAndName, dbLangGeneral, dbEncrypt)
    9.  
    10.     '~~> Create new TableDef (I am creating a table Table1)
    11.     Set tdefMDB = dbDatabase.CreateTableDef("Table1")
    12.  
    13.     '~~> Add fields to MDB
    14.     '~~> for eample I am creating One text field, 1 date field and
    15.     '~~> 1 memo field. Amend as applicable
    16.     Set txtFieldone = tdefMDB.CreateField("txtField1", dbText, 20)
    17.     Set dateFieldone = tdefMDB.CreateField("dateField1", dbDate)
    18.     Set memoFieldone = tdefMDB.CreateField("memoField1", dbMemo)
    19.    
    20.     '~~> Append the field objects to the TableDef
    21.     tdefMDB.Fields.Append txtFieldone
    22.     tdefMDB.Fields.Append dateFieldone
    23.     tdefMDB.Fields.Append memoFieldone
    24.  
    25.     '~~> Save TableDef definition by appending it to TableDefs collection.
    26.     dbDatabase.TableDefs.Append tdefMDB
    27.    
    28.     '~~> Inform user.
    29.     MsgBox "New .MDB Created - '" & sNewDBPathAndName & "'", vbInformation
    30. End Sub

    Hope this helps...
    Last edited by Siddharth Rout; Sep 9th, 2009 at 09:04 AM. Reason: Spell Check
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

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