Creating a Database Programmatically
Please note:
The only use I can imagine for this is if you’ve tested to see if the databaseexists and it doesn’t – which in itself, suggests something is wrong. I would usuallyexpect the database to be designed and created prior to use of the program, since youdon’t want multiple copies of the database and this code won’t work once the databaseexists.
Private FunctionCreateAccessDatabase(ByValDatabaseFullPathAs String)
As Boolean DimblnSuccessAs Boolean
Try 'Make sure the folder 'provided in the path exists. If file name w/o path'is specified, the database will be created in your 'application folder.mstrCreateTable ="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="& DatabaseFullPathadoxCat.Create(mstrCreateTable) 'name table, append fields to tableadoxTable.Name ="Witnesses"adoxTable.Columns.Append("LastName",ADOX.DataTypeEnum.adVarWChar, 40)adoxTable.Columns.Append("FirstName",ADOX.DataTypeEnum.adVarWChar, 20)adoxTable.Columns.Append("Age", ADOX.DataTypeEnum.adInteger) 'append tables to databaseadoxCat.Tables.Append(adoxTable) 'internal index on two fieldsadoxIndex.Name ="TwoColumnsIndex" 'name of index
adoxIndex.Columns.Append("LastName")adoxIndex.Columns.Append("Age")adoxTable.Indexes.Append(adoxIndex)blnSuccess =True CatchExcepAsSystem.Runtime.InteropServices.COMException
blnSuccess =False 'do whatever else you need to do here, log,'msgbox etc. FinallyadoxCat =Nothing End Try
ReturnblnSuccess End Function
Private SubbtnCreateDatabase_Click(ByValsenderAsSystem.Object,ByVal
eAsSystem.EventArgs)HandlesbtnCreateDatabase.Click
Add a Comment