Results 1 to 6 of 6

Thread: Create And Manage Databases

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    SIENA
    Posts
    2

    Smile Create And Manage Databases

    Hi,

    I need to create a DB directely from VB6... is it possible?

    Can I create also different tables and queries?

    Thanx!

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Try something like...

    VB Code:
    1. Private NewDef As TableDef
    2. Private NewField As Field
    3. Private SQL As String
    4.  
    5. 'LocalDB is a database object
    6.  
    7.       SQL = "CREATE TABLE ReportTable "
    8.       LocalDb.Execute SQL
    9.  
    10.       With LocalDB
    11.          Set NewDef = .TableDefs!ReportTable
    12.          Set NewField = NewDef.CreateField("SerialNumber", dbText, 15)
    13.            NewField.AllowZeroLength = True
    14.            NewDef.Fields.Append NewField
    15.          Set NewField = NewDef.CreateField("ModelCode", dbText, 15)
    16.            NewField.AllowZeroLength = True
    17.            NewDef.Fields.Append NewField
    18.          Set MyField = NewDef.CreateField("AcceptBuildStatus", dbText, 8)
    19.            NewField.AllowZeroLength = True
    20.            NewDef.Fields.Append NewField
    21.          Set MyField = NewDef.CreateField("Station", dbText, 8)
    22.            NewField.AllowZeroLength = True
    23.            NewDef.Fields.Append NewField
    24.          Set MyField = NewDef.CreateField("BuildTask", dbText, 8)
    25.            NewField.AllowZeroLength = True
    26.            NewDef.Fields.Append NewField
    27.        End With

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    If you are using Access and DAO, you can use the following methods to create various objects:

    DBEngine.CreateDatabase() to create a database
    TableDefs collection to manage tables. The Add method of this collection lets you add a table to the collection, and in turn to the database, which eliminates the need for the "Create Table" SQL statement.
    Fields collection of a TableDef object lets you manage the fields in a table. The Add method of this collection lets you add a field to the collection, in turn to the respective table.

    In DAO, a database is represented by the Database object. You can create an empty database using the CreateDatabase method mentioned above. Use of DBEngine is optional. So you could just write CreateDatabase() instead of DBEngine.CreateDatabase().

    Each table in an Access database (including system tables) is represented by a TableDef object. For each database there is a TableDefs collection which lists out all the tables in the database.

    Each TableDef object has a Fields collection which holds various Field objects. Each Field object represents one field of the table.

    Each TableDef object can also hold an Indexes collection, which contains the various Index objects. Each Index object represents a specific index, either on one field or more than one fields, in the table.

    Then there is a QueryDef object that represents a query stored in the Access database.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Location
    SIENA
    Posts
    2

    Wink

    Thanks to everybody, but I need something clearer... I'm not so expert in databases managing.

    What do you mean with DBEngine?

    Please add some code to explain what I have to do...

    Before adding a row like CreateDatabase(), what have I to write down? What kind of refs do I need to select?

    Be patient!

  5. #5
    Fanatic Member RSINGH's Avatar
    Join Date
    May 2001
    Location
    London
    Posts
    522
    Do you have a DBMS to work against?
    The liver is bad. It must be punished.

  6. #6
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by cleverboy81
    Thanks to everybody, but I need something clearer... I'm not so expert in databases managing.

    What do you mean with DBEngine?

    Please add some code to explain what I have to do...

    Before adding a row like CreateDatabase(), what have I to write down? What kind of refs do I need to select?

    Be patient!
    With VB, you can use a number of techniques to connect to a variety of databases. The techniques can be DAO (Data Access Objects), RDO (Remote Data Objects) or ADO (ActiveX Data Objects) among the most popular, and the databases could be simple text files, Excel spreadsheets, MS Access databases (the most common), SQL Server or Oracle or even dBase.

    The two posts above, mine and Hack's, talk about VB and MS Access using DAO.

    Since DAO, RDO, ADO all are COM structures, they have different components arrenged in a hierarchy. So a Database component/object sits on top of all TableDef objects, a Field object is under a TableDef object and so on. The "mother of all objects" or the root of the DAO object hierarchy is the DBEngine object.

    In order to use the DAO technology, you have to add a reference to the appropriate DAO library availble on your machine. With VB5/6, you will typically have DAO 3.51 object library (which can connect to an Access 97 database), and if you have installed SP3 or higher, you will have DAO 3.6 (which can connect to Access 97 as well as Access 2000 databases).

    Once you add a reference to the DAO library, you can start using the various objects in the DAO COM hierarchy. The usual practice of using the objects is to specify a reference variable, and then create a new instance of an object and assign that instance to the reference variable:
    VB Code:
    1. Dim myDB As DAO.Field 'Declaring reference variable
    2. Set myDB = New DAO.Field 'Creating a new instance
    3.           'of the object and assigning it to the reference variable

    However, the DBEngine object does not need such instantiation. You can directly use it in code, as if an object of it was already created. Therefore you don't have to create a new instance of it using statements like : Set myEngine = New DBEngine. Simply use it in the code.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

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