Connecting to an Ms-Access 2003 database file using Visual Basic 2008 Express Edition

The simplest way to retrieve a data from an Ms-Access 2003 database file in VB 2008 Express Edition is to use Database Explorer. Database Explorer is a database management tool that enables user to access server-based or local-based databases. There are other handy database access tools such as OleDbDataAdapter or SqlDataAdapter but I prefer using Database Explorer because it is simpler to use and it allows you to link to a database file conveniently and effortlessly.

To connect to an Ms-Access database file, you should first specify the location and file name of the database file that you wanted to link to. The following steps show you how:

1. Create an Ms-Access 2003 database file.(If you are familiar with database programming, this one is just common sense to you).

2. Start Visual Basic 2008 Express Edition.

3. Click File>New Projects>Select Windows Forms Application from the Visual Studio installed templates option>Click the Ok button.
This will display a blank form on the screen.

4. Next, click the View menu>Select Database Explorer. This will display the Database Explorer panel on the left portion of your VB 2008 Express Edition Integrated Development Environment. Alternatively, you can press CTRL+ALT+S.

5.In the Database explorer panel, locate the Add Connections option>Right-click it, then select Add connection…

6. The Add Connection Dialog box will appear. Click the Change… button beside the data source label>Select Microsoft Access Database File form the data source list box>Click Ok.

7. Click the Browse… button beside the Database file name label then locate and double-click your (Ms-Access database file).mdb file>Click Ok.

Click the Test Connection button to test if the database connection is working properly. Just make sure that you database file is not password protected or else you will get a “Not a valid Password” error.

After choosing the file name of the Ms-Access file that you wanted to connect to, you will need to select the data source or the data that you wanted to work with your application. Data sources can be made from a database application such as Ms-Access, or from a web server.

To define the data source, follow these steps:

1.Click the Data menu>Click the Add new data source… option.

This will bring out the Data Source Configuration Wizard. Since we will be getting our data from a database application and not from a web-server and definitely not from any bound object embedded on a form,select database>Click the Next button.

2.In the next screen, you will see your database file name in the Data Connection list box>Just click the next button.

3. After clicking the next button, a message box containing the following prompt will appear:
“The connection you selected uses a local data file that is not in the current project. Would you like to copy the file to you project and modify the connection?
If you copy the file to your project, blah blah blah”. Just click the Yes button.

4.The connection string dialog box will appear. A connection string is simply a string containing information about the connection.Just click the Next button.

5.The Database object screen will be shown containing a tree control with Tables and Views parent items. Tables contains your actual table while Views stores virtual tables,To manipulate your actual table, just expand the Tables parent item, and check the table that you wanted to be available in you project. Alternatively, you can check the Tables parent item if you wanted all you tables to be made available on your project.

6.Finally, click Finish.

After designating the table where you wanted to get your records from, the next thing that you need to do is to show the record values on your form. These steps teach you how:

1.Click the Data menu> Select Show Data Sources. This will show the available data sources or tables in a tree control. Alternatively, you can also press Shift+Alt+D.

2. In the Data Sources Panel, expand the table that you wanted to view the data in a form>Click and drag the individual fieldnames from the data sources panel to your form.

3. A bound control will be displayed for each field name. Together with a binding navigator that contains next, top, bottom, previous, add, delete, and save buttons.

4. Press F5 to run your application.

Try to click any of the navigational buttons and see how they worked perfectly like a charm. If you missed any of the steps during the connection process then your application will definitely fail. But don't despair, as Henry Ford once said, "Failure is simply the opportunity to begin again, this time more intelligently".

A Simple Hello World Program (Windows Forms Application-Microsoft Visual C++ 2008 Express Edition)

From the name itself, a windows forms application is local-based application that uses a graphical user interface similar to windows. The following steps show you how to create a windows forms application and display a "Hello world!" text in it:

1. Start MSVC++ Express 2008 by clicking on the Start button, All Programs then clicking on the Microsoft Visual C++ Express Edition Start menu option.

2. Click File>New>Project. The new project dialog box will then come into view.

3. Select Windows Forms Application from the Visual Studio Installed templates, type the project name>Click Ok.
After clicking the Ok button, an IDE suited for windows forms application will then appear containing a form, a toolbox window, and a properties window.

4. Select your form by clicking its title bar. In the properties window, locate the Name property then assign “NameForm” as its value. In similar manner, locate the Text property then assign “Display Hello World” as its value. Both values should be entered without quotation marks.


5. In the toolbox window, Select the label tool then click and drag it into an area in your form where you wanted to appear your “Hello world!” text.

6. Select your label, in the properties window, locate the Name property then assign “HelloLabel” as its value. In the same manner, locate the Text property then assign “Hello world!” as its value. Both values should be typed without quotation marks.

7. Press F5 to execute your application. If all went well, you should now see you windows form application with a "Hello world!" text displayed in it.

Changing the value of a control property through codes (Windows Forms Application-Microsoft Visual C++ 2008 Express Edition)

In case you might not notice, Visual Programming otherwise known as Object-Oriented programming revolves around changing the values of a property of an object through codes. To change the value of a control property using codes in Visual C++ Express Edition, use the following syntax:

//enclosed the value in a quotation mark 
//if the value of a property is a string
this->ControlName->Property=”value”; 
//do not add quotation marks
//if the value of the property is Boolean or Number
this->ControlName->Property=value;

The following steps illustrate a sample application of this syntax:

1. Start MSVC++ Express 2008 by clicking on the Start button, All Programs then clicking on the Microsoft Visual C++ Express Edition Start menu option.

2. Click File>New>Project. The new project dialog box will then come into view.

3. Select Windows Forms Application from the Visual Studio Installed templates, type the project name>Click Ok.
After clicking the Ok button, an IDE suited for windows forms application will then appear containing a form, a toolbox window, and a properties window.

4. Design your interface similar to the one shown below:



Note: Those texts that appear beside each control are the suggested names for our form controls in this example. If you notice I am using .Net naming convention in naming controls because it is simpler to use than Leszynski naming convention. To change the value of the name property of each control, just click each individual control then locate the name property in the properties window then assign those descriptive names in our illustration correspondingly.


5. Double-click the control named FnameButton, then enter:

//this will assign John as an FnameLabel text
this->FnameLabel->Text=”John”; 

Your code will now look like this:

private: System::Void FnameButton_Click(System::Object^  sender, System::EventArgs^  e) {
this->FnameLabel->Text=”John”;
}

6. Double-click LnameButton, the following should then the following between the open and close curly brackets:

//this will assign John as an FnameLabel text
this->LnameLabel->Text=”Doe”; 


Your code will now look like this:


private: System::Void LnameButton_Click(System::Object^  sender, System::EventArgs^  e) {
this->LnameLabel->Text=”Doe”;
}

7. Double-click the control named ExitButton, then type this->Close(); between the open and curly brackets. The Close() method terminates the current form.

Your code will now look like this::


private: System::Void ExitButton_Click(System::Object^  sender, System::EventArgs^  e) {
this->Close();
}

8. Press F5 to run your application. If all went well, you will see our sample application instantly without build erros.

Using Math Operators (Windows Forms Applications-Microsoft Visual C++ 2008 Express Edition)

As you may already know, math operators are used to perform mathematical computations and just like any other languages, MSVC++ 2008 Express supports the following basic math operators:

(+)Addition
(-)Subtraction
(/)Division
(*)Multiplication

The following steps demonstrate these operators at work:

1. Start MSVC++ Express 2008 by clicking on the Start button, All Programs then clicking on the Microsoft Visual C++ Express Edition Start menu option.

2. Click File>New>Project. The new project dialog box will then come into view.

3. Select Windows Forms Application from the Visual Studio Installed templates, type the project name>Click Ok.
After clicking the Ok button, an IDE suited for windows forms application will then appear containing a form, a toolbox window, and a properties window.

4. Create a user interface similar to the one below:



Note: Those texts that appear beside each control are the suggested names for our form controls in this example. If you noticed I am using .Net naming convention in naming controls because it is simpler to use than Leszynski naming convention. To change the value of the name property of each control, just click each individual control then locate the name property in the properties window then assign those descriptive names in our illustration correspondingly.

5. Double-click your form then locate the following line:
#pragma endregion
6. Declare the following variables below it:
int intnum1, intnum2, intans;
Declaring these variables below the #pragma endregion line allows our variables to be available publicly and to be used by other procedures.

Your code should now appear just like this:
#pragma endregion
int intnum1, intnum2,intans;

7. Double-click the control named TimesButton,type the following:

intnum1=int::Parse(this->FnumTextBox->Text);
intnum2=int::Parse(this->SnumTextBox->Text);
intans=intnum1 * intnum2;
this->OperatorLabel->Text="*";
this->AnsLabel->Text=Convert::ToString(intans);

Your code should now appear just like this:

private: System::Void TimesButton_Click(System::Object^  sender, System::EventArgs^  e) {
intnum1=int::Parse(this->FnumTextBox->Text);
intnum2=int::Parse(this->SnumTextBox->Text);
intans=intnum1 * intnum2;
this->OperatorLabel->Text="*";
this->AnsLabel->Text=Convert::ToString(intans);
}

8. Double-click the control named DivButton,type the following:
intnum1=int::Parse(this->FnumTextBox->Text);
intnum2=int::Parse(this->SnumTextBox->Text);
intans=intnum1 / intnum2;
this->OperatorLabel->Text="/";
this->AnsLabel->Text=Convert::ToString(intans);

Your code should now appear just like this:
private: System::Void DivButton_Click(System::Object^  sender, System::EventArgs^  e) {
intnum1=int::Parse(this->FnumTextBox->Text);
intnum2=int::Parse(this->SnumTextBox->Text);
intans=intnum1 / intnum2;
this->OperatorLabel->Text="/";
this->AnsLabel->Text=Convert::ToString(intans);
}

9. Double-click the control named AddButton,type the following:
intnum1=int::Parse(this->FnumTextBox->Text);
intnum2=int::Parse(this->SnumTextBox->Text);
intans=intnum1 + intnum2;
this->OperatorLabel->Text="+";
this->AnsLabel->Text=Convert::ToString(intans);

Your code should now appear just like this:
private: System::Void AddButton_Click(System::Object^  sender, System::EventArgs^  e) {
intnum1=int::Parse(this->FnumTextBox->Text);
intnum2=int::Parse(this->SnumTextBox->Text);
intans=intnum1 + intnum2;
this->OperatorLabel->Text="+";
this->AnsLabel->Text=Convert::ToString(intans);
}

10. Double-click the control named DiffButton,type the following:
intnum1=int::Parse(this->FnumTextBox->Text);
intnum2=int::Parse(this->SnumTextBox->Text);
intans=intnum1 - intnum2;
this->OperatorLabel->Text="-";
this->AnsLabel->Text=Convert::ToString(intans);

Your code should now appear just like this:
private: System::Void DiffButton_Click(System::Object^  sender, System::EventArgs^  e) {
intnum1=int::Parse(this->FnumTextBox->Text);
intnum2=int::Parse(this->SnumTextBox->Text);
intans=intnum1 - intnum2;
this->OperatorLabel->Text="-";
this->AnsLabel->Text=Convert::ToString(intans);
}

11. Double-click the control named ClearButton,type the following:
this->FnumTextBox->Text="";
this->SnumTextBox->Text="";
this->OperatorLabel->Text="";
this->AnsLabel->Text="";

Your code will now look like this:
private: System::Void ClearButton_Click(System::Object^  sender, System::EventArgs^  e) {
this->FnumTextBox->Text="";
this->SnumTextBox->Text="";
this->OperatorLabel->Text="";
this->AnsLabel->Text="";
}


12.Double-click ExitButton, then type this->Close:
private: System::Void ExitButton_Click(System::Object^  sender, System::EventArgs^  e) {
this->Close();
}
13. Press F5 to execute your application. I will add comments to the codes above when I have time. Till then:)

Accepting and Displaying String Inputs (Windows Forms Applications-Microsoft Visual C++ 2008 Express Edition)

Visual C++ 2008 accepts and displays input in a manner that is very similar to java. In other words, the method used in accepting string data is different from the method used in accepting numeric data.
To accept a string data, and display it on your desired control, use the following syntax:

this->DestinationObjectName->Property=Convert::ToString (this-> SourceObjectName->Property);
//or simply
this->DestinationObjectName->Property= this-> SourceObjectName->Property;

Where;

1. DestinationObjectName is the name of the control where the value will be displayed.
2. SourceObjectName is the name of the control where the value will come from.

Convert::ToString is a method that converts a value inputted in a control to text or string. Using Convert::ToString method in accepting user’s input is not obligatory because all data inputted in a textbox are recognized as string.

A sample application of Convert::ToString method is demonstrated in the following steps:

1. Start MSVC++ Express 2008 by clicking on the Start button, All Programs then clicking on the Microsoft Visual C++ Express Edition Start menu option.

2. Click File>New>Project. The new project dialog box will then come into view.

3. Select Windows Forms Application from the Visual Studio Installed templates, type the project name>Click Ok.
After clicking the Ok button, an IDE suited for windows forms application will then appear containing a form, a toolbox window, and a properties window.

4. Create a user interface similar to the one shown below:



Note: Those texts that appear beside each control are the suggested names for our form controls in this example. If you noticed I am using .Net naming convention in naming controls because it is simpler to use than Leszynski naming convention. To change the value of the name property of each control, just click each individual control then locate the name property in the properties window then assign those descriptive names in our illustration correspondingly.

6. Double-Click the control named OkButton then key-in the following:

//Converts the value inputted in the StringtextBox to string
//Using Convert::ToString method is optional because
//The text entered in a textbox is already interpreted as string
this->EnteredLabel->Text=Convert::ToString(this->StringtextBox->Text); 
Your code should then appear like this:

private: System::Void OkButton_Click(System::Object^  sender, System::EventArgs^  e) {
this->EnteredLabel->Text= Convert::ToString (this->StringtextBox->Text);
}
7. Double-click ExitButton, then type this->Close(); between the open and close curly brackets. The this keyword refers to the current form while the Close() method ends the execution of the current form.

Your code should now like this:

private: System::Void ExitButton_Click(System::Object^  sender, System::EventArgs^  e) {
this->Close();
}
8. Press F5 to execute your application. If you have followed our steps carefully, our sample application should now appear without build errors.

Accepting and Displaying Numeric Data (Windows Forms Applications-Microsoft Visual C++ 2008 Express Edition)

Oftentimes, we create programs that needs to ask numbers from the user.
To accept a numeric data in Visual C++ Express Edition, use the following syntax:

Expression= int::Parse (this-> SourceObjectName->Property);

Where;

1. Expression can either be an ObjectName or a variable.
2. SourceObjectName is the name of the control where the value will come from.

int::Parse is a method that converts text or string into an integer. The int::Parse method is required in accepting numeric inputs. If you omit the int::Parse method, the numeric input will be interpreted as string.

To display a numeric data, use the following syntax:

this-> DestinationObjectName->Property=Convert::ToString(Expression);

Where;

1. Expression can either be an ObjectName or variable.
2. SourceObjectName is the name of the control where the value will come from.

Convert::ToString is a method that converts a value inputted in a control to text or string and is required in displaying numeric output to avoid parameter conversion build errors.

To see int::Parse and Convert::ToString methods in action, follow these steps:

1. Start MSVC++ Express 2008 by clicking on the Start button, All Programs then clicking on the Microsoft Visual C++ Express Edition Start menu option.

2. Click File>New>Project. The new project dialog box will then come into view.

3. Select Windows Forms Application from the Visual Studio Installed templates, type the project name>Click Ok.
After clicking the Ok button, an IDE suited for windows forms application will then appear containing a form, a toolbox window, and a properties window.

4. Create a user interface similar to the one shown below:



Note: Those texts that appear beside each control are the suggested names for our form controls in this example. To change the value of the name property of each control, just click each individual control then locate the name property in the properties window then assign those descriptive names in our illustration correspondingly.

6. Double-Click the control named OkButton, the following event procedure should then come into sight:

7. Double-click the control named OkButton then type the following between the open and close curly brackets:
//Converts the number inputed by the user into a numeric data
//and assign it to an integer variable named intusernum
int intusernum=int::Parse(this->StringtextBox->Text);
//Converts the numeric data into a string data and display it in a
//control named EnteredLabel
this->EnteredLabel->Text=Convert::ToString(intusernum);

Your code should now look like this:
private: System::Void OkButton_Click(System::Object^  sender, System::EventArgs^  e) {
int intusernum=int::Parse(this->StringtextBox->Text);
this->EnteredLabel->Text=Convert::ToString(intusernum);   
} 
8. Double-click the control named ExitButton then type this->Close();. This will terminate the execution of the current form. Your code will then look like this:
private: System::Void ExitButton_Click(System::Object^  sender, System::EventArgs^  e) {
this->Close();
}   
9. Press F5 to execute your application.