Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating/Deleting An Access Database At Run Time 1

Status
Not open for further replies.

NeilFrank

Programmer
Mar 12, 2000
167
0
0
CA
Although I know how to:<br><br>(a) at design time, write code so that a <u>Table</u> is added and/or deleted to a pre-existing MS Access Database at run time;&nbsp;&nbsp;and<br><br>(b) at design time, create an MS Access Database (using VisData)<br><br>I'd like to find out how to: <br><br>(c) write code so that an MS Access <u>Database</u>,<br><i>ie</i> not simply a Table, is added and/or deleted at run time.<br><br><br>Any hints/leads ??<br><br>TIA<br><br>/Frank
 
hi Frank all you need to do is to create the database (I'll use DAO as its Access)&nbsp;&nbsp;object and&nbsp;&nbsp;use<br>a normal SQL script to create the table and then using the tabledef object set the various field <br>properties such as AllowZeroLength and DefaultValue<br><br>Dim oMacroDatabase&nbsp;&nbsp;as Database<br>Dim&nbsp;&nbsp;oTableDef as tabledef<br>Dim&nbsp;&nbsp;sPath as String<br><br>'sPath is the path and Filename of your database<br>&nbsp;Set oMacroDatabase = DBEngine.Workspaces(0).CreateDatabase(sPath, dbLangGeneral)<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>'Create the table fields, Constraints,Index's,Primary Key<br><br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = &quot; CREATE TABLE ClinicalTrial ( ClinicalTrialId INTEGER ,&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot; ClinicalTrialName&nbsp;&nbsp;TEXT(15),&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot; ClinicalTrialDescription&nbsp;&nbsp;TEXT (255),&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot; PhaseId SMALLINT , StatusId SMALLINT ,&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot; Keywords TEXT (255), ExpectedRecruitment INTEGER ,&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot; ActualRecruitment INTEGER ,&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot; TrialTypeId&nbsp;&nbsp;SMALLINT ,&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot; CONSTRAINT ClinicalTrialName UNIQUE (ClinicalTrialName),&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot; CONSTRAINT ClinTrial_PrimaryKey PRIMARY KEY&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot; (ClinicalTrialID))&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;oMacroDatabase.Execute sSQL, dbFailOnError<br><br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = &quot; CREATE UNIQUE INDEX idx_ClinicalTrialId &quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot;ON ClinicalTrial ( ClinicalTrialId )&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;oMacroDatabase.Execute sSQL, dbFailOnError<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = &quot; CREATE UNIQUE INDEX idx_ClinicalTrialName &quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot;ON ClinicalTrial ( ClinicalTrialName )&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;oMacroDatabase.Execute sSQL, dbFailOnError<br>&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = &quot; CREATE INDEX idx_PhaseId &quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot;ON ClinicalTrial ( PhaseId )&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;oMacroDatabase.Execute sSQL, dbFailOnError<br><br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = &quot; CREATE INDEX idx_StatusId &quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;sSQL = sSQL & &quot;ON ClinicalTrial ( StatusId )&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;oMacroDatabase.Execute sSQL, dbFailOnError<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>'-------------------------------------------------------------------------------------<br>'To access the field properties refresh the database so that tabledefs collection<br>' recognises the ClinicalTrial tabel as a valid table<br>'-------------------------------------------------------------------------------------<br>oMacroDatabase.TableDefs.Refresh<br><br>&nbsp;&nbsp;&nbsp;Set oTableDef = oMacroDatabase.TableDefs(&quot;ClinicalTrial&quot;)<br>&nbsp;&nbsp;&nbsp;&nbsp;With oTableDef<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;ClinicalTrialId&quot;).DefaultValue = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;ClinicalTrialId&quot;).Required = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;ClinicalTrialName&quot;).AllowZeroLength = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;ClinicalTrialDescription&quot;).AllowZeroLength = True<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;ClinicalTrialDescription&quot;).Required = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;PhaseId&quot;).DefaultValue = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;PhaseId&quot;).Required = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;StatusId&quot;).DefaultValue = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;StatusId&quot;).Required = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;Keywords&quot;).AllowZeroLength = True<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;Keywords&quot;).Required = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;ExpectedRecruitment&quot;).DefaultValue = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;ExpectedRecruitment&quot;).Required = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;ActualRecruitment&quot;).DefaultValue = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;ActualRecruitment&quot;).Required = False<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;TrialTypeId&quot;).DefaultValue = 0<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.Fields(&quot;TrialTypeId&quot;).Required = False<br>&nbsp;&nbsp;&nbsp;&nbsp;End With<br><br>I've just used one of the tables from my database so just change the field names and <br>datatypes as required...<br><br>To delete a table just use the Drop table statement but this will mean you lose all data in the<br>table as well.<br><br>Good luck <br>Will<br>
 
You can you the CreateDatabase using either ADO or DAO through code <br><br>The DAO example is in the help files in Visual Basic <br><br>This is the source for doing the same thing in ADO <br><br><br><br><br><b><font color=blue>Option Explicit</font></b><br><font color=green>'Dimension the objects</font><br><b><font color=blue>Dim</font></b> cn As ADODB.Connection<br><b><font color=blue>Dim</font></b> nCat As ADOX.Catalog<br><br><b><font color=blue>Private Sub Form_Load()</font></b><br><br><br><font color=green>'Set the objects</font><br><br><b><font color=blue>Dim</font></b> ntab As New ADOX.Table<br>&nbsp;&nbsp;<br><b><font color=blue>Set</font></b> nCat = New ADOX.Catalog<br><br><font color=green>'Set the connection object = the return value of the Catalogue Create method</font><br>&nbsp;&nbsp;&nbsp;&nbsp;<br><b><font color=blue>Set</font></b> cn = nCat.Create(&quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\testcreate.mdb&quot;)<br><br><font color=green>'Append the columns and properties to the table object</font>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>ntab.Name = &quot;test&quot;<br>ntab.Columns.Append &quot;TestID&quot;<br>ntab.Columns.Append &quot;Testing&quot;<br><br><font color=green>'Append the table to the catalogue</font><br>nCat.Tables.Append (ntab)<br><br><b><font color=blue>End Sub</font></b><br>
 
Thanks Will and Vince for your speedy and informative responses.&nbsp;&nbsp;I'm about to try them out.&nbsp;&nbsp;More to follow...<br><br>/Frank
 
Another way is to declare an access object within vb.&nbsp;&nbsp;<br>(You can do the same thing with an excel object in vb.)<br>Make sure that your project references include access.<br>Then you can open the access object and use its<br>methods (vb will list them for you) to create, populate,<br>and delete the database.
 
Could you be a little more specific, Listener22, or suggest a source that discusses this technique?<br><br>
 
Vince,<br><br>How do I set things up so that I can create a ADOX.Catalog Object?&nbsp;&nbsp;Nothing intuitive I've tried under Project/References or Project/Components seems to work.<br><br>In Help, Catalog Objects are discussed under Microsoft Data Access Components 2.5 SDK Beta - ADOX.&nbsp;&nbsp;Does this not come with the Microsoft VB6 Professional Edition?<br><br>/Frank
 
A follow-up question for Will:<br><br>I'm able to create my Database (Chronology) and populate it with a Table, as you described. I can then set up my connection to this Database as folows: <br><br>&nbsp;&nbsp;&nbsp;&nbsp;Set mcnChronology = New ADODB.Connection<br>&nbsp;&nbsp;&nbsp;&nbsp;mcnChronology.ConnectionString = &quot;Provider = &quot; _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& &quot;Microsoft.Jet.OLEDB.3.51; &quot; _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& &quot;Data Source=C:\Frank\PPPWizard\Chronology.mdb&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;mcnChronology.CursorLocation = adUseClient<br><br><br>However, when I next attempt to open the connection, ie:<br><br>&nbsp;&nbsp;&nbsp;&nbsp;mcnChronology.Open<br><br>I get the following error message:<br><br>'could not use &quot;; file is already in use'<br><br>Any idea as to what's going on?<br><br>BTW, I'm using Access 2000 in my ap, but notice that the database created by your code is the Access '97 vintage.&nbsp;&nbsp;In response to this, when I set up the ConnectionString property for mcnChronology, I used <br><br>'Provider = Microsoft.Jet.OLEDB.3.51;'<br><br>instead of <br><br>'Provider = Microsoft.Jet.OLEDB.4.0;'<br><br>but this didn't help.<br><br>TIA<br><br>/Frank<br><br><br><br>
 
&nbsp;&nbsp;Back to the use of an access object, in visual basic 5 help<br>under 'CreateObject Function', there is an example using<br>excel, rather than access.&nbsp;&nbsp;The important thing is to include<br>Microsoft Access Object Library as a project reference.<br><br>&nbsp;&nbsp;The code would start with:<br><br>Dim myAccess As Access.Application&nbsp;&nbsp;&nbsp;[or As Object]<br>Set myAccess = CreateObject(&quot;Access.Application&quot;)<br><br>&nbsp;&nbsp;&nbsp;Thereafter, methods of the access object such as<br><br>myAccess.DBEngine.CreateDatabase<br>myAccess.myAccess.CodeDb.CreateTableDef.CreateField<br><br>can be used, and&nbsp;&nbsp;.visible can be set to true during development.<br><br>&nbsp;&nbsp;It's been awhile, but I have seen this method used to make many features<br>of access available from within vb5.&nbsp;&nbsp;One disadvantage of the technique<br>is that the version of access you have included in the project references<br>must exist on the target computer.<br>
 
<br>It's Frank, again.<br><br>I've figured out how to prevent the 'File already in use' error I described yesterday when attempting to open a connection to my newly created 'Chronology' Database.<br>All I needed to do was to close this Database as soon as it was created, ie<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;mobjTempDatabase.Close<br><br>I can now connect to this Database<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Set mcnChronology = New ADODB.Connection<br>&nbsp;&nbsp;&nbsp;&nbsp;mcnChronology.ConnectionString = &quot;Provider = &quot; _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& &quot;Microsoft.Jet.OLEDB.3.51; &quot; _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& &quot;Data Source=C:\Frank\PPPWizard\Chronology.mdb&quot;<br>&nbsp;&nbsp;&nbsp;&nbsp;mcnChronology.CursorLocation = adUseClient<br>&nbsp;&nbsp;&nbsp;&nbsp;mcnChronology.Open<br><br>and open my Recordset<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;Set mrsChronology = New ADODB.Recordset<br>&nbsp;&nbsp;&nbsp;&nbsp;Call mrsChronology.Open(&quot;Chronology&quot;, mcnChronology, adOpenStatic, adLockPessimistic, adCmdTable)<br><br><br>However, one problem remains to be solved.&nbsp;&nbsp;I need to delete (not simply close) my Database after I've finished using it.&nbsp;&nbsp;How do I do this????<br><br>TIA<br><br>/Frank (<A HREF="mailto:nfrank@pol.net">nfrank@pol.net</A>)<br><br><br>
 
sorry it took so long to get back <br><br>The reference to use the catalogue object ADOX is the <br><br>Microsoft ADO Ext. 2.1 for DDL and Security <br><br>although why it's called this I don't know <br><br>hope this is usefull
 
Thanks for the catalogue object reference, Vince.&nbsp;&nbsp;It did the trick, to an extent.<br><br>However, permit me 2 additional questions:<br><br>1 - Using the precise code you suggested, I can see that a Database is created, and the code to define a Table and add fields seems to fly.&nbsp;&nbsp;&nbsp;However, when the line <br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;nCat.Tables.Append(ntab) <br><br>is executed, it causes a run time error: 'the application is using arguments of the wrong type, are out of acceptable range, or in conflict with one another'.&nbsp;&nbsp;What gives?<br><br>2 - In my program, I need to delete the Database after it has served its purpose (namely, printing out the contents of a ListView).&nbsp;&nbsp;Is there some way to delete the entire Database at runtime?<br><br>I've hit my creative wall with both of these problems.<br><br><br>/Frank<br><br><br><br>
 
Surely in order to delete the database you only need to delete the mdb file?&nbsp;&nbsp;Using the Scripting.FileSystemObject (you need a reference to the Scripting Runtime library to use this code, but you could do it some other way)...<br><br>Dim fso As Scripting.FileSystemObject<br><br>Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)<br><br>fso.DeleteFile(&quot;C:\Frank\PPPWizard\Chronology.mdb&quot;, True)<br><br>Set fso = Nothing<br><br>Hope this is useful,<br>Jon <p> Jonathan<br><a href=mailto:j.w.george@virginnet.co.uk>j.w.george@virginnet.co.uk</a><br><a href= > </a><br>Working with: Visual Basic 6, Access 97, Visual Interdev 6, VBScript, Active Server Pages, SQL Server 6.5, Oracle 7
 
Let's see how far I can stuff my foot...<br><br>The other way for a deletion is a plain and simple KILL<br><br><FONT FACE=monospace><br>IF (&quot;&quot; &lt;&gt; DIR(&quot;C:\Frank\PPPWizard\Chronology.mdb&quot;)) THEN<br>&nbsp;&nbsp;&nbsp;KILL &quot;C:\Frank\PPPWizard\Chronology.mdb&quot;<br>END IF<br></font><br> <p>Wil Mead<br><a href=mailto:wmead@optonline.net>wmead@optonline.net</a><br><a href= > </a><br>
 
Here's some code I found in help. <br><br>Sub CreateDatabaseX()<br><br>&nbsp;&nbsp;&nbsp;&nbsp;Dim wrkDefault As Workspace<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim dbsNew As Database<br>&nbsp;&nbsp;&nbsp;&nbsp;Dim prpLoop As Property<br><br>&nbsp;&nbsp;&nbsp;&nbsp;' Get default Workspace.<br>&nbsp;&nbsp;&nbsp;&nbsp;Set wrkDefault = DBEngine.Workspaces(0)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;' Make sure there isn't already a file with the name of<br>&nbsp;&nbsp;&nbsp;&nbsp;' the new database.<br>&nbsp;&nbsp;&nbsp;&nbsp;If Dir(&quot;c:\NewDB.mdb&quot;) &lt;&gt; &quot;&quot; Then Kill &quot;c:\NewDB.mdb&quot;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;' Create a new encrypted database with the specified<br>&nbsp;&nbsp;&nbsp;&nbsp;' collating order.<br>&nbsp;&nbsp;&nbsp;&nbsp;Set dbsNew = wrkDefault.CreateDatabase(&quot;c:\NewDB.mdb&quot;, _<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dbLangGeneral, dbVersion20)<br><br>&nbsp;&nbsp;&nbsp;&nbsp;dbsNew.Close<br><br>End Sub<br><br>I tried using &quot;dbVersion30&quot; and &quot;dbVersion40&quot; and when I tried to open the mdb with access, got &quot;incompatible file format&quot;.<br><br>Also, I had to include &quot;Microsoft DAO 3.6 Object Library&quot; as a reference.<br><br>hth<br><br> <p> Ron<br><a href=mailto: > </a><br><a href= > </a><br>Independent Consultant<br>
Tampa Bay, Florida
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top