Solved

Creating DB through code

Posted on 1998-07-06
1
132 Views
Last Modified: 2010-05-03
I am learning VB on my own and I came across a book that gives a modified example(pseudo code) on how to create a database in Vb using code to create it.  I have the professional edition of VB 5.0 and would be greatly appreciative if someone could give me the code to create a database with one table and one fld w/in that table.  This is work related and it would be helpful.  I can only find pseudo code in books.  I need  help.
0
Comment
Question by:timpeters
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 5

Accepted Solution

by:
yronnen earned 100 total points
ID: 1465038
First, go to the reference menu in the VB enviorment and add a reference to Microsoft DAO 3.5 library.

This code will create a database with one table, two fields and a primary key:

sub createDB()
Dim DB As Database
Dim TBL As TableDef
Dim FLD1 As Field
Dim FLD2 As Field
Dim IDX As Index

DBEngine.DefaultType = dbUseJet
Set DB = DBEngine.Workspaces(0).CreateDatabase("c:\mydb.mdb", dbLangGeneral)
Set TBL = DB.CreateTableDef("MyTable")
Set FLD1 = TBL.CreateField("Field1", dbInteger)
Set FLD2 = TBL.CreateField("Fields2", dbText, 20)
TBL.Fields.Append FLD1
TBL.Fields.Append FLD2
Set IDX = TBL.CreateIndex("Prime")
IDX.Primary = True
Set FLD1 = IDX.CreateField("Field1", dbInteger)
IDX.Fields.Append FLD1
TBL.Indexes.Append IDX
DB.TableDefs.Append TBL
DB.Close
end sub

If you know some SQL, you can use a shorter procedure (this one does exactly the same):

sub CreateDBSQL()
dim DB as database
dim SQL as string

DBEngine.DefaultType = dbUseJet
Set DB = DBEngine.Workspaces(0).CreateDatabase("c:\mydb.mdb", dbLangGeneral)
SQL="CREATE TABLE NewTable (Field2 TEXT 20,  "Field1  INTEGER CONSTRAINT Prime PRIMARY KEY)"
db.execute sql
db.close
end sub
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question