MS-Access / Getting Started

Calculating the Version Number

We're going to use a calculated version number to make it easier to enter. Because the property in the database will ultimately be text, however, you could choose to stamp your own version number in there. The algorithm for this calculation is as follows:

  1. Retrieve the major and minor versions from the source database.
  2. Using the start date of the application as defined in the source database, calculate the number of months the project has been active. This is the difference (in months) of the current date and the start date of the application.
  3. Append the day the build is being created. In other words, if today were November 5, we'd append 05 to the build number.
  4. Last, we'll append a revision number to the database. The revision number will be stored in the source database and then incremented.

Before we get into the code, let's look at an example. Say that the start date of your application was January 4, 2007 and today is November 5. For the purpose of example, we'll assume that the major and minor version is 1.0. The difference in months between November 5 and January 4 is 10. Because we've already said that today is November 5, we'll append 05. If this is the first revision, we'd leave it at 0. Therefore, the calculated version number is 1.0.1005.0.

Here's the code, beginning with the declarations:

private string CalculateVersion()
    string appVersion;
    DateTime startDate = appStartDate.Value;
    DateTime relDate = releaseDate.Value;

To calculate the number of months, we start by subtracting the start date from the release date, as shown, and then divide by 30. This is an approximation, but it's pretty close. We'll also append the day of the release.

    // diff the months and append the day
    TimeSpan diff = relDate.Subtract(startDate);
    string build = (diff.Days / 30).ToString("00");
    build += relDate.Day.ToString("00");

Last, we need to create the formatted string using the Format method of the string object.

    appVersion = string.Format("{0}.{1}", major, minor); // major.minor
    appVersion += string.Format(".{0}", build); // build
    appVersion += string.Format(".{0}", revision); // revision
    // return the formatted version
    return appVersion;
The ToString method can be used to format data in a particular format. In this case, we've used it to force two characters for the day and number of months.

When you run the application, you should be able to select a database and specify the target database. After setting properties and options, click the Build button. Open the target database when complete. You should see the new build properties for the target database in the Properties dialog box.

[Previous] [Contents] [Next]