MS-Access / Getting Started

Retrieving Information from the Source Database

The browseSourceButton defined a moment ago calls the method GetDatabaseInfo, which retrieves information from the source database when you choose a file to build. The source database will store certain properties as well as the target database. In the source database, we'll store the following properties:

  • AppStartDate - Used to calculate the version number
  • AppVersionMajor - Major version number
  • AppVersionMinor - Minor version number
  • AppVersionRevision - Revision number of the last build
  • BuildTarget - The destination database for the build

Write the following code to implement the GetDatabaseInfo method. First things first though - we need to open the database:

private void GetDatabaseInfo(string databaseName)
{
try
{
    // open the database
    sourceDatabase = myEngine.OpenDatabase(databaseName,
	false,
	false,
	Type.Missing);

Next, retrieve the properties from the database and set control values on the form. We've used the Parse methods to make sure that we have valid data:

// read database properties from the source database
try
{
    appStartDate.Value = DateTime.Parse(
	GetDatabaseProperty(sourceDatabase, SRC_PRP_STARTDATE));
    outputDatabase.Text = GetDatabaseProperty(
	sourceDatabase, SRC_PRP_TARGET);
    major = int.Parse(GetDatabaseProperty(
	sourceDatabase, SRC_PRP_APPMAJOR));
   minor = int.Parse(GetDatabaseProperty(
	sourceDatabase, SRC_PRP_APPMINOR));
    revision = int.Parse(GetDatabaseProperty(
	sourceDatabase, SRC_PRP_APPREVISION));
}
catch (COMException) { }

We need to list the tables in the database to delete data from. To do this, first clear the checked list box as shown:

// clear the listbox
deleteDataListbox.Items.Clear();

Now, enumerate the TableDefs collection in the source database and add non-system tables:

// list the tables
DAO.TableDefs tables = sourceDatabase.TableDefs;
foreach (DAO.TableDef td in tables)
{
    if (!IsSystemTable(td))
    {
	deleteDataListbox.Items.Add(td.Name);
    }
}

We also want to default the release date to the current date and then close the method:

    // default the release date
    releaseDate.Value = DateTime.Today;
    // close the source database
    sourceDatabase.Close();
}
catch (Exception ex)

    {
    // display an error
    MessageBox.Show(ex.ToString());
    }
}

This method includes a call to another method called GetDatabaseProperty. This method is used to read a property value from the specified database. In our case, we're creating user-defined properties that appear in the Properties dialog box for the database. Add the following code to implement the GetDatabaseProperty method:

private string GetDatabaseProperty(DAO.Database database, string propName)
{
    DAO.Container container;
    DAO.Document document = null;
    try
    {
	// get a database property
	container = database.Containers["Databases"];
	document = container.Documents["UserDefined"];
	return document.Properties[propName].Value.ToString();
    }
    catch (COMException ex)
    {
	throw ex;
    }
}

We also defined the method IsSystemTable to determine whether a TableDef object is a system table. Add the following code to implement this helper method:

private bool IsSystemTable(DAO.TableDef td)
{
    return (0 != (td.Attributes &
		(int)DAO.TableDefAttributeEnum.dbSystemObject));
}
[Previous] [Contents] [Next]