Results 1 to 8 of 8
  1. #1
    Join Date
    May 2005
    Posts
    125
    Provided Answers: 1

    Unanswered: VB Code Help and Guidance

    Hi this thread has stemmed from an earlier post, so forgive me if I should of replied in the original thread, apologies aside I need help and guidance with some VB code, and my skills with VB code is poor tbh.

    I am using Access 2003, what I need help with is using code to create a temporary database, with several tables within it.

    Thee code Im using is this

    Public Function CreateDatabase()

    Dim db As Database
    Dim td As TableDef
    Dim f As Field

    On Error GoTo ErrorHandler
    Set db = DBEngine.CreateDatabase(c \ tmp \ mydb.mdb, dbLangGeneral)
    Set td = db.CreateTableDef(InitialTable)
    Set f = td.CreateField("ID", dbLong)
    td.Fields.Append f
    db.TableDefs.Append td

    CreateDatabase = True
    ErrorHandler:
    If Not db Is Nothing Then db.Close

    End Function

    Yes I ripped it of the internet, for I could not find anything in the code bank, but I do not have a clue as what to do with it

    Your help and guidance would be greatly appreciated

  2. #2
    Join Date
    Nov 2004
    Location
    out on a limb
    Posts
    13,692
    Provided Answers: 59
    ..and what's the problem
    ..where did the code crap out
    ..what steps did you take to resolve the problem
    .. straightaway I would guess
    Code:
    Set db = DBEngine.CreateDatabase(c \ tmp \ mydb.mdb, dbLangGeneral)
    is the problem
    I'd expect literals to be encapsulated in quote (') or speech marks (")
    Code:
    Set db = DBEngine.CreateDatabase("c\tmp","\mydb.mdb", dbLangGeneral)
    or perhaps
    Code:
    Set db = DBEngine.CreateDatabase("c\tmp\mydb.mdb", dbLangGeneral)
    Last edited by healdem; 03-10-09 at 10:09.
    I'd rather be riding on the Tiger 800 or the Norton

  3. #3
    Join Date
    Feb 2004
    Location
    One Flump in One Place
    Posts
    14,912
    It might be an idea to just backup a wee bit too before you get really deep in to this.
    It is not often that creating databases on the fly is too good an idea. What's the background?
    Testimonial:
    pootle flump
    ur codings are working excelent.

  4. #4
    Join Date
    May 2005
    Posts
    125
    Provided Answers: 1
    Thanks Healdem for your help so far

    Quote Originally Posted by healdem
    ..and what's the problem
    ..where did the code crap out
    ..what steps did you take to resolve the problem
    .. straightaway I would guess
    I have stripped the origional code back just to the create the database.

    Problem - Database not being created, tried your two solutions.!
    Code crap out - No idea and would not be able to tell you where or how.!
    Steps - Help from you guys hopefully

  5. #5
    Join Date
    May 2005
    Posts
    125
    Provided Answers: 1
    Quote Originally Posted by pootle flump
    It might be an idea to just backup a wee bit too before you get really deep in to this.
    It is not often that creating databases on the fly is too good an idea. What's the background?
    Hi Pootle

    Original Post http://www.dbforums.com/microsoft-ac...ual-table.html

    This is where this came about, I know it was back in February, but I have been working on other projects, and now come back to this, hence I tried to look in the code bank, and then used the internet for a reference.

    Thanks

    Mark

  6. #6
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    This works perfectly:

    Public Function CreateDatabase()

    Dim db As DAO.Database
    Dim td As DAO.TableDef
    Dim f As DAO.Field

    On Error GoTo ErrorHandler
    Set db = DBEngine.CreateDatabase("c:\tmp\mydb.mdb", dbLangGeneral)
    Set td = db.CreateTableDef("InitialTable")
    Set f = td.CreateField("ID", dbLong)
    td.Fields.Append f
    db.TableDefs.Append td
    CreateDatabase = True

    ErrorHandler:
    If Not db Is Nothing Then db.Close

    End Function

    It's always a good practice to explicitly reference the objects you declare (ie. DAO.Database instead of Database ...).

  7. #7
    Join Date
    May 2005
    Posts
    125
    Provided Answers: 1
    Sinndho

    Thankyou very much

    Mark

  8. #8
    Join Date
    Mar 2009
    Posts
    5,442
    Provided Answers: 14
    Mark,

    You'll find all the references and explanation you need on this site:

    FMS Technical Papers - DAO Advanced Programming

    Have a nice day!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •