MS-Access / Getting Started

Set Build Properties on the Target Database

The target database will contain three properties:

  • AppVersion - The version number of the application
  • AppReleaseDate - The release date of the application
  • DEBUG - Indicates whether the database is a debug build

The BuildApplication method calls the method SetTargetProperties to set properties in the target database. Define this method as follows.

private void SetTargetProperties()
{
    SetDatabaseProperty(targetDatabase, DST_PRP_VERSION,
	DAO.DataTypeEnum.dbText, appVersion.Text);
    SetDatabaseProperty(targetDatabase, DST_PRP_RELDATE,
	DAO.DataTypeEnum.dbDate, releaseDate.Value.ToShortDateString());
    SetDatabaseProperty(targetDatabase, DST_PRP_DEBUG,
	DAO.DataTypeEnum.dbBoolean, debugBuild.Checked);
    // delete properties that may be in the target after doing a copy
    DeleteDatabaseProperty(targetDatabase, SRC_PRP_APPMAJOR);
    DeleteDatabaseProperty(targetDatabase, SRC_PRP_APPMINOR);
    DeleteDatabaseProperty(targetDatabase, SRC_PRP_APPREVISION);
    DeleteDatabaseProperty(targetDatabase, SRC_PRP_STARTDATE);
    DeleteDatabaseProperty(targetDatabase, SRC_PRP_TARGET);
}

Okay, so you noticed that this doesn't really do much - other than call another method. The SetDatabaseProperty method is used to set the actual property values. This method accepts the Database object so it is used to set properties in both the target and source databases. Add the following code to implement the SetDatabaseProperty method:

    private void SetDatabaseProperty(DAO.Database database, string propName,
DAO.DataTypeEnum propType, object propValue)
{
    DAO.Container container;
    DAO.Document document = null;
    try
    {
	// set a database property
	container = database.Containers["Databases"];
	document = container.Documents["UserDefined"];
	document.Properties[propName].Value = propValue;
    }

You'll notice here that we're setting a property in the UserDefined document of the Databases container in the database. Properties created here appear in the Properties dialog box for the database.

If the specified property is not found, we receive the Property Not Found error in DAO. In the .NET Framework, this translates to a COMException. If we receive this error, we need to create the property.

catch (COMException ex)
{
    switch (ex.ErrorCode)
    {
	case ERR_PROP_NOTFOUND:
	    // property not found - create it
	    if (!string.IsNullOrEmpty(propValue.ToString()))
	    {
		DAO.Property prp = targetDatabase.CreateProperty(
		    propName,
		    propType,
		    propValue,
	   	    Type.Missing);
		document.Properties.Append(prp);
	    }

If the property value is null, we're going to raise a new COMException to the caller to handle:

	    else
	    {

	    // raise this exception to the calling routine
	    throw new COMException("", ERR_PROP_ISNULL);
	}
	break;
    default:
	throw;
    }
}

If the target database did not get created, our code will receive a NullReferenceException so let's handle that as well:

catch (NullReferenceException)
{
    // set the error text
    errorText = "Target database does not exist. " +
		"Creating ACCDE/MDE failed." +
		"\n\nVerify that you can compile the " +
		"source database and that there are no errors.";
    }
}

If we made a copy of the file, the source database properties may be copied to the target database. The DeleteDatabaseProperty method is used to delete a property from the specified database.

private void DeleteDatabaseProperty(DAO.Database database, string propName)
{
    DAO.Container container;
    DAO.Document document = null;
    try
    {
	// delete a database property
	container = database.Containers["Databases"];
	document = container.Documents["UserDefined"];
	document.Properties.Delete(propName);
    }
    catch (COMException){ }
}
[Previous] [Contents] [Next]