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.
Control | Property Name | Property Value |
---|---|---|
labell | Location | 3, 9 |
Text | Select database | |
textBox1 | (Name) | inputDatabase |
Location | 6, 25 | |
Size | 215, 21 | |
label2 | Location | 3, 49 |
Text | Destination | |
textBox2 | (Name) | outputDatabase |
Location | 6, 65 | |
Size | 215, 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.
Control | Property Name | Property Value |
---|---|---|
groupBox1 | (Name) | propertiesGroup |
Location | 6, 92 | |
Size | 296, 105 | |
Text | Build Properties | |
label3 | Location | 6, 17 |
Text | Application start date | |
label4 | Location | 6, 44 |
Text | Release date | |
label5 | Location | 6, 71 |
Text | Application version | |
dateTimePicker1 | (Name) | appStartDate |
Format | Short | |
Location | 122, 20 | |
Size | 168, 21 | |
dateTimePicker2 | (Name) | releaseDate |
Format | Short | |
Location | 122, 44 | |
Size | 168, 21 | |
TextBox3 | (Name) | appVersion |
Location | 122, 71 | |
Size | 168, 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.
Control | Property Name | Property Value |
---|---|---|
label6 | Location | 3, 200 |
Text | Delete data from tables | |
checkedListBox1 | (Name) | deleteDataListbox |
CheckOnClick | True | |
Location | 6, 216 | |
Size | 296, 100 |
Additional Options
Add two check box controls to the form and set its properties as shown in the table that follows.
Control | Property Name | Property Value |
---|---|---|
checkBox1 | (Name) | createACCDE |
Location | 6, 322 | |
Text | Create ACCDE/MDE | |
checkBox2 | (Name) | debugBuild |
Location | 6, 346 | |
Text | Create 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.
Control | Property Name | Property Value |
---|---|---|
button1 | (Name) | buildButton |
FlatStyle | System | |
Location | 146, 340 | |
Text | &Build | |
button2 | (Name) | exitButton |
FlatStyle | System | |
Location | 227, 340 | |
Text | E&xit | |
button2 | (Name) | browseSourceButton |
FlatStyle | System | |
Location | 227, 23 | |
Text | Br&owse... | |
button2 | (Name) | browseTargetButton |
FlatStyle | System | |
Location | 227, 63 | |
Text | B&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 Name | Property Value |
---|---|
(Name) | MainForm |
AcceptButton | buildButton |
CancelButton | exitButton |
Font | Tahoma |
FormBorderStyle | FixedDialog |
MaximizeBox | False |
MaximizeBox | False |
Size | 320, 397 |
StartPosition | CenterScreen |
Text | Build 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(); }
In this tutorial:
- Deployment
- Creating an Automated Build
- Design the Form
- Retrieving Information from the Source Database
- Building the Application
- Creating the Target Database
- Set Build Properties on the Target Database
- Setting Build Properties on the Source Database
- Deleting Data from Tables
- Calculating the Version Number
- Handling Application Dependencies
- Updating References
- Testing Reference Fix-Up
- Late Binding
- Licensing Your Applications
- Number of Records
- Restricting the Number of Times an Application is Launched
- Registering an Application
- Creating a Registration Web Service
- Validate the Registration Using the Web Service
- Miscellaneous Deployment Scenarios
- Create the Client Application
- Testing the Versioning Server
- Re-Linking Tables Automatically
- Programmatically Creating DSNs
- Creating a User DSN
- Ensuring an Application Runs Locally