Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating DB through code

Posted on 1998-07-06
1
Medium Priority
?
136 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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
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…
Suggested Courses

636 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