MS-Access / Getting Started

Design the Form

With the references correctly set, we can start designing the form. To get an idea of where this is going, let's define the requirements for the application:

  • Must be able to select the database to be built
  • Must be able to specify the target database name
  • Need an option to create an MDE or ACCDE file
  • Should be able to stamp the build number and release date on the database
  • Should be able to delete data from tables in the event that you have test data in the database

Adding Controls

In total, there are 19 controls we need to add to the form so let's get started.

Database Paths

To ask for the path to the database and the destination database we need a couple of text boxes. Add two labels and two text boxes to the form. Set their properties as shown in the table that follows.

ControlProperty NameProperty Value
labellLocation3, 9
TextSelect database
textBox1(Name)inputDatabase
Location6, 25
Size215, 21
label2Location3, 49
TextDestination
textBox2(Name)outputDatabase
Location6, 65
Size215, 21

Build Properties

The build properties we'll set are the application start date, release date, and build number. The application start date is used to calculate the build number but is not required. Add a group box, three labels, two date time pickers, and one text box to the form and set their properties as shown in the table that follows.

ControlProperty NameProperty Value
groupBox1(Name)propertiesGroup
Location6, 92
Size296, 105
TextBuild Properties
label3Location6, 17
TextApplication start date
label4Location6, 44
TextRelease date
label5Location6, 71
TextApplication version
dateTimePicker1(Name)appStartDate
FormatShort
Location122, 20
Size168, 21
dateTimePicker2(Name)releaseDate
FormatShort
Location122, 44
Size168, 21
TextBox3(Name)appVersion
Location122, 71
Size168, 21

List of Tables

We're using a checked list box to display the list of tables to delete data from. Add one label and one checked list box to the form and set their properties as shown in the table that follows.

ControlProperty NameProperty Value
label6Location3, 200
TextDelete data from tables
checkedListBox1(Name)deleteDataListbox
CheckOnClickTrue
Location6, 216
Size296, 100

Additional Options

Add two check box controls to the form and set its properties as shown in the table that follows.

ControlProperty NameProperty Value
checkBox1(Name)createACCDE
Location6, 322
TextCreate ACCDE/MDE
checkBox2(Name)debugBuild
Location6, 346
TextCreate debug build

Buttons

Last, we'll add four buttons to the form. Two buttons will be used to browse for the source and destination files. Another will be used to create the actual build and the other to exit the application. Set their properties as shown in the table that follows.

ControlProperty NameProperty Value
button1(Name)buildButton
FlatStyleSystem
Location146, 340
Text&Build
button2(Name)exitButton
FlatStyleSystem
Location227, 340
TextE&xit
button2(Name)browseSourceButton
FlatStyleSystem
Location227, 23
TextBr&owse...
button2(Name)browseTargetButton
FlatStyleSystem
Location227, 63
TextB&rowse...

Setting Form Properties

Once you've added all of the controls, you need to go back and set properties of the form, as shown in the table that follows.

Property NameProperty Value
(Name)MainForm
AcceptButtonbuildButton
CancelButtonexitButton
FontTahoma
FormBorderStyleFixedDialog
MaximizeBoxFalse
MaximizeBoxFalse
Size320, 397
StartPositionCenterScreen
TextBuild Access Application

After you've set all the properties, you should have a form.

We didn't appear to use a naming convention for controls on the form but we're actually following the guidelines for .NET development. These guidelines differ greatly from the Access conventions of Hungarian notation, Reddick VBA (RVBA), or Leszynski Naming Convention (LNC). You can read more about the .NET guidelines from MSDN. More information can be found about these Access conventions in the Access 2007 VBA Programmer's Reference.

Control Events

To add functionality to the application, let's start adding code to the controls, beginning with the DateTimePicker controls.

Updating the Version Number

When the dates change in the form, we want to update the version number. The algorithm for this calculation will be described later in the section "Calculating the Version Number." For now, double-click on the appStartDate control to add an event handler for the ValueChanged event. Add the following code for the event:

private void appStartDate_ValueChanged(object sender, EventArgs e)
{
    // update the version
    appVersion.Text = CalculateVersion();
}

Next, do the same thing for the releaseDate control. Add the following code to the event handler for the ValueChanged event:

private void releaseDate_ValueChanged(object sender, EventArgs e)
{
    appVersion.Text = CalculateVersion();
}

Browsing for Files

To make finding and saving files easier, we'll use the Click event of the Browse buttons to display a file dialog box. To add the code, double-click on the browseSourceButton control to add the event handler for the Click event. Add the following code to the event:

private void browseSourceButton_Click(object sender, EventArgs e)
{
    // prompt for a file name
    using (OpenFileDialog fd = new OpenFileDialog())
    {
	fd.CheckFileExists = true;
	fd.CheckPathExists = true;
	fd.Multiselect = false;
	fd.ShowHelp = false;
	fd.Filter = "Microsoft Access Databases (*.accdb;*.mdb)" +
	    "|*.accdb;*.mdb";
	fd.Title = "Select Application to Build";
	if (DialogResult.OK == fd.ShowDialog())
	{
	    inputDatabase.Text = fd.FileName;
	    // get the information from the database
	    GetDatabaseInfo(fd.FileName);
	}
    }
}

When you double-click in the control, you should see the standard Open dialog box. Next, repeat the process for the Click event of the browseTargetButton control. Add the following code to this event:

private void browseTargetButton_Click(object sender, EventArgs e)
{
    // prompt for the destination database
    using (SaveFileDialog fd = new SaveFileDialog())
    {
	fd.CheckFileExists = false;
	fd.CheckPathExists = true;
	fd.ShowHelp = false;
	fd.Filter = "Microsoft Access 2007 Database (*.accdb)|*.accdb";
	fd.Filter += "|Microsoft Access 2000 Database (*.mdb)|*.mdb";
	fd.Filter += "|ACCDE File (*.accde)|*.accde";
	fd.Filter += "|MDE File (*.mde)|*.mde";
	if (DialogResult.OK == fd.ShowDialog())
	{
	    outputDatabase.Text = fd.FileName;
	}
    }
}

We've added more filters in this dialog box because we need the option to save in different formats.

The using block is different from the using statement in C#. The using block shown in the previous code is used to enforce garbage collection. Behind the scenes, this block of code generates a call to the Dispose method for the object in question.

Exit Button

As you would imagine, the Exit button enables you to exit the application. Add the following code to the Click event of the button:

private void exitButton_Click(object sender, EventArgs e)
{
    // exit
    Application.Exit();
}

Build Button

The Build button does the majority of the work to create the new database and set the properties. We'll use programming by intention here and define the code before it is implemented. Add the following code to Click event of the buildButton control:

private void buildButton_Click(object sender, EventArgs e)
{
    BuildApplication();
}
[Previous] [Contents] [Next]