Solved

How to create a MS Access Database from Visual Basic 6.0

Posted on 2003-11-12
9
579 Views
Last Modified: 2013-11-25
I am developing a small package using VB 6.0 and MSAccess 2000 Database.  In that application, I am using two databases.  One database, I am always refreshing the data (deleting) whenever I transfer the data but the database file size is keep on bulging (increasing the size).  I would like to know how to OPTIMIZE that database from VB. (Note: In MSAccess there is a option to optimize the database but I wanted to do through VB).  It is possible for me to advise me on this.

Another request is how to create a MS Access Database from the Visual Basic Application. My requirement is whenever the application is launched the database has to be created and when the applications shuts down that database has to be deleted.  Please advise for the these two queries.  
Thanks, Boopalan K.
0
Comment
Question by:boopalank
9 Comments
 
LVL 2

Expert Comment

by:MaxPol
ID: 9730665
Well,

as a trick, in order to create a new DB, you could have a real clear DB file as a temporary element that you use to make a copy where you need, use the copy, and then dispose it (always keeping the "template" like empy DB).

This is just an idea. I did once create a DB, but forgot it. I'll try to look for it and let you know.

For the optimization no idea for now.

Bye,

Max
0
 

Accepted Solution

by:
twili earned 84 total points
ID: 9730827
Do something like this to create an access database in VB:

Dim skyDB As Database
Dim wrkDefault As Workspace
Dim tabell As TableDef
Dim idxNr As Index

 sdbFile = App.Path & "\DB.mdb"
 
 Set wrkDefault = DBEngine.Workspaces(0)
 
'***Delete DB if it exists
 If Dir(sdbFile) <> "" Then Kill sdbFile

'***Create the MDB file
 Set skyDB = wrkDefault.CreateDatabase(sdbFile, _
        dbLangSwedFin)

'***Create a table in the MDB file
 Set tabell = skyDB.CreateTableDef("person")
 
 With tabell
   .Fields.Append .CreateField("Name", dbText)
   .Fields.Append .CreateField("Company", dbText)
   .Fields.Append .CreateField("Address", dbText)
   .Fields.Append .CreateField("Zip", dbText)
   .Fields.Append .CreateField("City", dbText)
   .Fields.Append .CreateField("State", dbText)
   .Fields.Append .CreateField("Class", dbInteger)
 End With
 
 skyDB.TableDefs.Append tabell
 tabell.Fields!name.AllowZeroLength = True
 tabell.Fields!Company.AllowZeroLength = True
 tabell.Fields!Address.AllowZeroLength = False
 tabell.Fields!Zip.AllowZeroLength = False
 tabell.Fields!City.AllowZeroLength = False
 
skyDB.Close

/O.
0
 
LVL 53

Assisted Solution

by:Dhaest
Dhaest earned 83 total points
ID: 9730911
Microsoft Office 2000/Visual Basic Programmer's Guide (by microsoft)
See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odeopg/html/deovrcreatingmodifyingaccesstables.asp
Here you find everything you need

Another useful link
http://www.vb-helper.com/index_database.html 
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/VB_Databases/Q_20089182.html 
http://www.freevbcode.com/ShowCode.Asp?ID=803

To create a table in acces:
1) dim Sql as string

Sql= "CREATE TABLE [tablename] (" _
   & "ATTRIBUTE_VALUE number _
   & "ATTRIBUTE_DESCRIPTION text(50))"
connection.execute sql

2) Another possible approach (but it's more complex)

It's ADOX you need. Try the following links :
Microsoft Office 2000/Visual Basic Programmer's Guide (by microsoft)
   http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odeopg/html/deovrcreatingmodifyingaccesstables.asp

   http://user.fundy.net/dking/access/ADOXcreateTable.htm
   http://www.able-consulting.com/ADOX_Faq.htm

Examples:
    http://www.freevbcode.com/ShowCode.Asp?ID=803

Another useful link
http://www.vb-helper.com/index_database.html 

0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9730914

' add a reference to dao 3.x to your project, if not already done
Compact:

Sub DoCompact(sPath As String, sFilename as string)

On Error GoTo errH

DBEngine.CompactDatabase sPath & "\" & sFilename, "c:\tmp\db.mdb"
Kill sPath & "\" & sFilename
Name "c:\tmp\db.mdb" As sPath & "\" sFilename
Exit Sub
errH:

MsgBox "Error: Database was not compacted.", vbCritical
End Sub
0
 
LVL 26

Assisted Solution

by:EDDYKT
EDDYKT earned 83 total points
ID: 9731096
Use ADO to compact ( the way I prefer)

Add MS Jet and REplication Object 2.x library (msjro.dll)

Dim je As New JRO.JetEngine
Dim DBfile As String, TEMPDBfile As String

On Error Resume Next
DBfile = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Yourdb.mdb"
TEMPDBfile = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Yourtempdb.mdb"    je.CompactDatabase DBfile, TEMPDBfile
Kill DBfile
Name TEMPDBfile As DBfile
0
 
LVL 9

Expert Comment

by:Dang123
ID: 9732804
boopalank,
    Obviously a number of ways exist. Here is a link that shows you how to create a new Access database and define the tables in XML to allow you to easily reuse the code again in the future.

http://www.vbip.com/xml/XML2Access/XML2Access-01.asp

Dang123


0
 
LVL 49

Expert Comment

by:DanRollins
ID: 10537317
Moderator, my recommended disposition is:

    Split points between: twili and Dhaest and EDDYKT

Dan Rollins -- EE database cleanup volunteer
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

828 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