Results 1 to 33 of 33

Thread: Creating an Access MDB from within VB

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    11

    Creating an Access MDB from within VB

    is there any way to create an Access MDB file from Scratch......

    i dont have any file structure or anything, that should only be created the first time when i run the app.

    is this possible through ADO ?

    point 2:

    how can i ENTER data into a MDB file , using ADO ??

    code would be helpful.....suggestions also welcomed

    thanks

  2. #2
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Thumbs up

    Yep. I have the code. But by the time i post the solution, 13 other people will have done the same thing.
    ~Peter


  3. #3
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by MrGTI
    Yep. I have the code. But by the time i post the solution, 13 other people will have done the same thing.
    hmmm... seems like u have to post it MrGTI
    -= a peet post =-

  4. #4
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Talking

    I guess so. Sorry about the delay .... i got busy here at work.

    Here is the code. It uses a DAO connection but you shouldn't have too much trouble converting it to ADO. I would, but like i said, .... i got busy here at work.

    Code:
    'Programmed by: Peter Temporale ([email protected])
    '               2000.01.25
    
    'Note:  This project required that these items be checked on in the references section (Project, References):
    '       *Microsoft DAO 2.5/3.51 Compatibility Library
    '       *OLE Automation
    
    'Variable           Type        Description
    '=========================================================================
    Dim sDatabaseName   As String   'I added this variable to make life easier
    
    Option Explicit
    
    Public Sub CreateNewDatabaseAlready()   'Now makes the database
    'All code in this section was what i copied from ERwin. Only made 2 changes - see below
    
    '  Starting Access Basic DAO Session...
    Dim ERwinWorkspace As Workspace
    Dim ERwinDatabase As Database
    Dim ERwinTableDef As TableDef
    Dim ERwinQueryDef As QueryDef
    Dim ERwinIndex As Index
    Dim ERwinField As Field
    Dim ERwinRelation As Relation
    
    Set ERwinWorkspace = DBEngine.Workspaces(0)
    
    Set ERwinDatabase = ERwinWorkspace.OpenDatabase(sDatabaseName)
    
    '  CREATE TABLE "Jobs"
    Set ERwinTableDef = ERwinDatabase.CreateTableDef("Jobs")
    
    '***Had to change DB_TEXT to dbText
    Set ERwinField = ERwinTableDef.CreateField("JobIDnum", dbText, 18)
    
    ERwinField.Required = True
    ERwinTableDef.Fields.Append ERwinField
    Set ERwinField = ERwinTableDef.CreateField("JobTitle", dbText, 18)
    ERwinTableDef.Fields.Append ERwinField
    Set ERwinField = ERwinTableDef.CreateField("JobDescription", dbText, 18)
    ERwinTableDef.Fields.Append ERwinField
    Set ERwinField = ERwinTableDef.CreateField("PayClass", dbText, 18)
    ERwinTableDef.Fields.Append ERwinField
    ERwinDatabase.TableDefs.Append ERwinTableDef
    
    '  CREATE TABLE "People"
    Set ERwinTableDef = ERwinDatabase.CreateTableDef("People")
    Set ERwinField = ERwinTableDef.CreateField("studIDnum", dbText, 18)
    ERwinField.Required = True
    ERwinTableDef.Fields.Append ERwinField
    Set ERwinField = ERwinTableDef.CreateField("JobIDnum", dbText, 18)
    ERwinField.Required = True
    ERwinTableDef.Fields.Append ERwinField
    Set ERwinField = ERwinTableDef.CreateField("FirstName", dbText, 18)
    ERwinTableDef.Fields.Append ERwinField
    Set ERwinField = ERwinTableDef.CreateField("LastName", dbText, 18)
    ERwinTableDef.Fields.Append ERwinField
    Set ERwinField = ERwinTableDef.CreateField("Address", dbText, 18)
    ERwinTableDef.Fields.Append ERwinField
    Set ERwinField = ERwinTableDef.CreateField("Postal", dbText, 18)
    ERwinTableDef.Fields.Append ERwinField
    ERwinDatabase.TableDefs.Append ERwinTableDef
    
    '  CREATE RELATIONSHIP "R/1"
    Set ERwinRelation = ERwinDatabase.CreateRelation("R/1", "Jobs", "People")
    Set ERwinField = ERwinRelation.CreateField("JobIDnum")
    ERwinField.ForeignName = "JobIDnum"
    ERwinRelation.Fields.Append ERwinField
    
    '***and had to rem this line out to make it work
    'ERwinDatabase.Relations.Append ERwinRelation
    
    '  Terminating Access Basic DAO Session...
    ERwinDatabase.Close
    ERwinWorkspace.Close
    
    End Sub
    
    Private Sub cmdCreateNew_Click()
    'All code in this procedure was missing, but probably contained in internal ERwin variables
    
    'Variable       Type
    '===========================
    Dim wrkDefault  As Workspace
    Dim dbsNew      As Database
    
    'Tells user what is about to happen
        MsgBox "A new database will be created in the directory as listed below:" & vbCrLf & vbCrLf & sDatabaseName, vbInformation, "Create New Database"
    'Get default Workspace.
        Set wrkDefault = DBEngine.Workspaces(0)
    'Make sure there isn't already a file with the name of the new database
        If Dir(sDatabaseName) <> "" Then Kill sDatabaseName
    'Create a new encrypted database with the specified collating order
        Set dbsNew = wrkDefault.CreateDatabase(sDatabaseName, dbLangGeneral, dbEncrypt)
    'Now uses the ERwin code section to finish off the database
        Call CreateNewDatabaseAlready
    'Beeps when done
        Beep
    End Sub
    
    Private Sub cmdExit_Click()
        End
    End Sub
    
    Private Sub Form_Load()
    'Assigns the name of the database including the base directory
        sDatabaseName = App.Path & "\CreateDatabase.mdb"
    End Sub
    ~Peter


  5. #5
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    ADO sample

    VB Code:
    1. 'Add a reference to Microsoft ADO Ext. X.X For DLL And Security
    2.  
    3. Option Explicit
    4. Dim cat As ADOX.Catalog
    5. Dim tbl As ADOX.Table
    6.  
    7. Private Sub Command1_Click()
    8.     Set cat = New ADOX.Catalog
    9.     Set tbl = New ADOX.Table
    10.  
    11.     ' create the db
    12.     cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\newDB.mdb"
    13.    
    14.     With tbl
    15.       .Name = "TestTable"
    16.       ' Create fields and append them to the
    17.       ' Columns collection of the new Table object.
    18.       With .Columns
    19.          .Append "ID COLUMN"
    20.          .Append "SetName", adVarWChar, 255
    21.          .Append "SetVal", adVarWChar, 255
    22.          .Append "Description", adVarWChar, 255
    23.       End With
    24.    End With
    25.    
    26.    ' Add the new Table to the Tables collection of the database.
    27.    cat.Tables.Append tbl
    28.    
    29.    Set cat = Nothing
    30.    Set tbl = Nothing
    31.    
    32. End Sub
    -= a peet post =-

  6. #6

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    11
    thanks peet, mrgti,

    i shall try out both the suggestions....

    is ADOX a part of the standard VB Distrib. ? ours is a college install....and we all know how stingy THEY are!


    is there no way to do it with plain-vanilla ADO ?

    thanks again

  7. #7
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    don't think u can create the database with plain ADO, but u can create and alter the tables using ADO and SQL,

    If u have ADO installed, ADOX is also there.
    -= a peet post =-

  8. #8
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    I tried yours peet, and when i try to open the newly created .MDB file in Access i get an error - Unrecognized database format

    Any ideas why? I have Access 97, but that shouldn't be a problem.
    ~Peter


  9. #9
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by MrGTI
    I tried yours peet, and when i try to open the newly created .MDB file in Access i get an error - Unrecognized database format

    Any ideas why? I have Access 97, but that shouldn't be a problem.
    this line
    VB Code:
    1. cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\newDB.mdb"
    generates an Access2000 db, not possible to open in Acc97 I think. (Microsoft.Jet.OLEDB.4.0 = acc2k)

    not sure if this will work, but try

    VB Code:
    1. cat.Create "Provider=Microsoft.Jet.OLEDB.3.5;Data Source=c:\newDB.mdb"
    (Microsoft.Jet.OLEDB.3.5 = acc97 well I hope )
    -= a peet post =-

  10. #10
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    oops

    sorry MrGTI

    Provider=Microsoft.Jet.OLEDB.3.51 not 3.5


    -= a peet post =-

  11. #11
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    Good news: The ADO database can be opened in Access 2000 (i sent it to my work account and tried it)

    Bad news: My work machine doesn't have Microsoft ADO Ext. X.X For DLL And Security on it, so i can run the program.

    It's strange because it's a Windows 2000 box, and it has almost everything on it from the factory, so you'd think MS would have included it.
    ~Peter


  12. #12
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Question

    Actually my home machine is Win2K as well. Maybe i need to install SS2K on my work machine ...... it's the only thing i have installed at home but not at work.
    ~Peter


  13. #13
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Its got ADO but not ADOX ?
    -= a peet post =-

  14. #14
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    Peet... I liked your code... but couldnt tried... ADOX? How do I know if I have that? I also have win2k and VB6 Enterprise Edition..... and this pc also had the ADO references... what do I need?
    "The difference between mad and genius is the success"

  15. #15
    Fanatic Member
    Join Date
    Apr 2001
    Posts
    843
    FOUND IT!!! It is a little bit below the ADO refrences....

    This is a good example... THANKS!
    "The difference between mad and genius is the success"

  16. #16
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by Andreex
    FOUND IT!!! It is a little bit below the ADO refrences....

    This is a good example... THANKS!
    glad u found it Andreex
    -= a peet post =-

  17. #17

    Thread Starter
    New Member
    Join Date
    Sep 2001
    Posts
    11

    thanks ppl

    peet. mgti,

    thanks a heap for all ur suggestions.

    1. found ADOX in the References section - has ADOX 2.1 in mine
    2. could NOT test out ur data, coz the Collg Net link was fried and i didnt have hard copy of ur code

    i use Access97 for base compatibility.....hope it works, will post as soon as it does, which i hope it will..

    thanks again

  18. #18
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277

    Exclamation

    I found the missing referance. It was marked as MISSING: so i unchecked it, saved, and went back in to referances and it was now in the list.

    Once i added it, everything worked. I did add in the extra code so i can switch between Access 97 and 2000

    Code:
    'Create the db using code for Access 2000
        adoCat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & sDatabaseNameADO
    'Create the db using code for Access 97
        'adoCat.Create "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & sDatabaseNameADO
    ~Peter


  19. #19
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629

    requested

    Ok mcsa as promissed, this sample shows how to use ADOX to create the Acc97DB, then using ADO and SQL to create the table.

    enjoy

    VB Code:
    1. 'Add a reference to Microsoft ADO Ext. X.X For DLL And Security
    2. 'Add a ref for ADO
    3. Option Explicit
    4. Private cat As ADOX.Catalog
    5. Private cnn As New ADODB.Connection
    6.  
    7. Private Sub Command1_Click()
    8.     Dim SQL As String
    9.     Set cat = New ADOX.Catalog
    10.     ' create the db
    11.     cat.Create "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=c:\newDB.mdb"
    12.     Set cat = Nothing
    13.    
    14.     'create table using SQL instead of table def
    15.     SQL = "CREATE TABLE TestTable (ID COUNTER, SetName TEXT(50), SetVal TEXT(255), Description TEXT(255))"
    16.    
    17.     'open the db and create the table using ADO and SQL
    18.     cnn.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=c:\newDB.mdb"
    19.     cnn.Execute SQL
    20.     cnn.Close
    21. End Sub
    -= a peet post =-

  20. #20
    Addicted Member
    Join Date
    Sep 2001
    Location
    Florida
    Posts
    213
    How do I set the DateSource to App path?

    This won't work.
    cat.Create "Provider=Microsoft.Jet.OLEDB.3.51;Data Source= App.Path & \new.mdb"

    Thanks

  21. #21
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by vbvbvbvb
    How do I set the DateSource to App path?

    This won't work.
    cat.Create "Provider=Microsoft.Jet.OLEDB.3.51;Data Source= App.Path & \new.mdb"

    Thanks
    think it should be like this (if u didn't just do a typo that is )

    VB Code:
    1. cat.Create "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=" & App.Path & "\new.mdb"
    -= a peet post =-

  22. #22
    Addicted Member
    Join Date
    Sep 2001
    Location
    Florida
    Posts
    213
    Thanks peet.

    And how do I add more fields into the table?

    VB Code:
    1. With new
    2.          .Name = "Table"
    3.          ' Create fields and append them to the
    4.          ' Columns collection of the new Table object.
    5.          With .Columns
    6.             .Append "ID COLUMN"
    7.             .Append "SetName", adVarWChar, 255
    8.             .Append "SetVal", adVarWChar, 255
    9.             .Append "Description", adVarWChar, 255
    10.          End With
    11. End With

    Much appreciated!

  23. #23
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by vbvbvbvb
    Thanks peet.

    And how do I add more fields into the table?

    ....
    what do u mean?
    -= a peet post =-

  24. #24
    Addicted Member
    Join Date
    Sep 2001
    Location
    Florida
    Posts
    213
    Using your code, I can create a new table but it is empty (no fields or columm). I want to create a table with some fileds say FieldA, fieldB....

  25. #25
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    Originally posted by vbvbvbvb
    Thanks peet.

    And how do I add more fields into the table?

    VB Code:
    1. With new
    2.          .Name = "Table"
    3.          ' Create fields and append them to the
    4.          ' Columns collection of the new Table object.
    5.          With .Columns
    6.             .Append "ID COLUMN"
    7.             .Append "SetName", adVarWChar, 255
    8.             .Append "SetVal", adVarWChar, 255
    9.             .Append "Description", adVarWChar, 255
    10.          End With
    11. End With

    Much appreciated!
    oh I see... been so many posts in this thread

    u do as u suggested u'r self abow, or u can use SQL and conn.Execute

    VB Code:
    1. 'create table using SQL instead of table def
    2.     SQL = "CREATE TABLE TestTable (ID COUNTER, SetName TEXT(50), SetVal TEXT(255), Description TEXT(255))"
    3.    
    4.     'open the db and create the table using ADO and SQL
    5.     cnn.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=c:\newDB.mdb"
    6.     cnn.Execute SQL
    7.     cnn.Close

    u'r choice
    -= a peet post =-

  26. #26
    Addicted Member
    Join Date
    Sep 2001
    Location
    Florida
    Posts
    213
    Well, when I open the table, nothing in there.

  27. #27
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    hmm... lets rewind... show me the exact code u use please
    -= a peet post =-

  28. #28
    Addicted Member
    Join Date
    Sep 2001
    Location
    Florida
    Posts
    213
    I used your own code:

    VB Code:
    1. Option Explicit
    2. Dim cat As ADOX.Catalog
    3. Dim tbl As ADOX.Table
    4.  
    5. Private Sub Command1_Click()
    6.     Set cat = New ADOX.Catalog
    7.     Set tbl = New ADOX.Table
    8.  
    9.     ' create the db
    10.     cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\newDB.mdb"
    11.    
    12.     With tbl
    13.       .Name = "TestTable"
    14.       ' Create fields and append them to the
    15.       ' Columns collection of the new Table object.
    16.       With .Columns
    17.          .Append "ID COLUMN"
    18.          .Append "SetName", adVarWChar, 255
    19.          .Append "SetVal", adVarWChar, 255
    20.          .Append "Description", adVarWChar, 255
    21.       End With
    22.    End With
    23.    
    24.    ' Add the new Table to the Tables collection of the database.
    25.    cat.Tables.Append tbl
    26.    
    27.    Set cat = Nothing
    28.    Set tbl = Nothing
    29.    
    30. End Sub

    Or your code is to create an empty table? If I want my table has these fields: ID, LastName, FirstName, how can I do that?

    Many thanks.

  29. #29
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    VB Code:
    1. Option Explicit
    2. Dim cat As ADOX.Catalog
    3. Dim tbl As ADOX.Table
    4.  
    5. Private Sub Command1_Click()
    6.     Set cat = New ADOX.Catalog
    7.     Set tbl = New ADOX.Table
    8.  
    9.     ' create the db
    10.     cat.Create "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\newDB.mdb"
    11.    
    12.     With tbl
    13.       .Name = "TestTable"
    14.       ' Create fields and append them to the
    15.       ' Columns collection of the new Table object.
    16.       With .Columns
    17.          .Append "ID"
    18.          .Append "LastName", adVarWChar, 255
    19.          .Append "FirstName", adVarWChar, 255
    20.       End With
    21.    End With
    22.    
    23.    ' Add the new Table to the Tables collection of the database.
    24.    cat.Tables.Append tbl
    25.    
    26.    Set cat = Nothing
    27.    Set tbl = Nothing
    28.    
    29. End Sub

    This creates a table called TestTable with the fields ID, LastName and FristName.

    I tested it to make sure
    -= a peet post =-

  30. #30
    Addicted Member
    Join Date
    Sep 2001
    Location
    Florida
    Posts
    213
    Thanks for being so patient, peet.

    When I click on the newDB.mdb, the "Open" button is not enable for me to open the table.

    Oh, and I don't see the table name "TestTable" any where.

    I must miss something here.

  31. #31
    -= B u g S l a y e r =- peet's Avatar
    Join Date
    Aug 2000
    Posts
    9,629
    no problem mate

    are u telling me u are not able to open the database ?

    this is probably a stupid question, but do u have Access installed ?

    what version, and what happen if u doubleclick the file ?

    I'm a bit confused
    -= a peet post =-

  32. #32
    Addicted Member
    Join Date
    Sep 2001
    Location
    Florida
    Posts
    213
    Ok, finally, your SQL works for me.

    VB Code:
    1. 'Add a reference to Microsoft ADO Ext. X.X For DLL And Security
    2. 'Add a ref for ADO
    3. Option Explicit
    4. Private cat As ADOX.Catalog
    5. Private cnn As New ADODB.Connection
    6.  
    7. Private Sub Command1_Click()
    8.     Dim SQL As String
    9.     Set cat = New ADOX.Catalog
    10.     ' create the db
    11.     cat.Create "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=c:\newDB.mdb"
    12.     Set cat = Nothing
    13.    
    14.     'create table using SQL instead of table def
    15.     SQL = "CREATE TABLE TestTable (ID COUNTER, FirstName TEXT(50), LastName TEXT(255), Description TEXT(255))"
    16.    
    17.     'open the db and create the table using ADO and SQL
    18.     cnn.Open "Provider=Microsoft.Jet.OLEDB.3.51;Data Source=c:\newDB.mdb"
    19.     cnn.Execute SQL
    20.     cnn.Close
    21. End Sub

    I have another Oracle table open (I can print to debug window to see the fields of this Orc Table)

    And I use this to insert export fields from Oracle to Access but it doesn't work. When debugging, I found that cat.Tables(".... <ADO could not find the object....> I still let cnn open

    VB Code:
    1. With OracleTable
    2.    .MoveFirst
    3.    Do until .EOF
    4.       cat.Tables("TestTable").Columns("FirstName") = .Fields("KeyType").Value
    5.       .............
    6.       .MoveNext
    7.    Loop
    Last edited by vbvbvbvb; May 3rd, 2002 at 03:34 PM.

  33. #33
    Addicted Member
    Join Date
    Sep 2001
    Location
    Florida
    Posts
    213
    I am almost there.... Some one please help !!!

    Thanks a bunch.

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