Saturday, December 17, 2011

12-Setting up an Access 2007-2010 or 2003 Database with VB Studio.NET 2010, 2008 using XML (Revised 12/11/11)

12. Setting up an Access 2007-2010 or 2003 Database with VB Studio.NET 2010, 2008 using XML (Revised 12/11/11)
Click here for the Visual Basic 2010 version of the Address Book or here for the 2008 version. The program also includes pictures. If you have Access 2007 use PhoneBook.accdb if you have Access 2003 use PhoneBook.mdb. Either way, place the database in the root of your c: drive. Otherwise make the program as shown below.
Open Microsoft Access (if you do not have Access 2007 use Access 2005, or a supplied database) and select new blank database. You can download a free redistributable version of Access at http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=10910   You may also need the Access database engine at http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=c06b8369-60dd-4b64-a44b-84b371ede16d . You may not be able to directly make tables with what is provided in the links, but you will be able to access my database in the sample project. Depending on what version of Windows you have you may have to make some adjustments. There is an issue with 64 bit vs. 32 bit Windows7 (refer to http://support.microsoft.com/kb/942976) which says if you use a 64 bit operating system you must have a 32 bit datasource (database). The code here works in the 32 bit environment. If you wish to go to the 64 bit environment, it has been reported that if you use an older mdb database that you may be able to overcome this issue. Both PhoneBook.accdb written in Access 2010 format and PhoneBook.mdb written in a 2003 format are included in this project as well as all the graphics.:
Access Program New Project,
Click on the browse Button As shown below:
Click Browse
Navigate to your root directory of your c: drive as shown below and save the file as PhoneBook.accdb for Access 2007-2010 (or PhoneBook.mdb for Access 2003).
Save Database
To create fields in access for the first name and last name be careful. If you choose a key word in Access the program won't. Specifically it turns out first and last have become key words. For a list of key words that you can't use as column names refer to http://sqlserver2000.databases.aspfaq.com/what-are-reserved-access-odbc-and-sql-server-keywords.html . So make the columns LastName, FirstName, Phone, and fill in as follows:
Rename Column
Then go to file save, go with a default name of the column as Table1 as shown below:
Name Table
Exit access. Go into visual basic and create a project called vbPhoneBookDatabaseXML and click on Data and add data source in the menu as shown below:
Add New Data Source
Mdtupdtr.exe. Select add database as shown below and click next
Choose a Data Source Type
And select Microsoft Access Database File as shown below
Choose Data Source
Choose Data Source
And click continue, and click on browse as shown below and navigate and select Phonebook and click open
Choose a Data Source Type
Navigate to and select PhoneBook.accdb if using Access 2007 or PhoneBook.mdb if you are using Access 2003. Click test connection, and it should succeed, provided Access and your database is closed. Don't change anything else. Now click OK
Add Connection
In the following screen assure you have created a connection between either PhoneBook.accdb or PhoneBook.mdb and click next.
Choose Data Connection
Say no if Visual basic says it wants to copy the database to your project (we want it only on the c: root). Click yes to connection string and click next.
Save Connection
In the database connection wizard expand the +'s and select everything and click finish.
Choose Database Objects
Now in Visual Basic select Data and show data sources.
Show Data Sources
In the window that appears in Visual Basic to the left, click on the plus to expose all the fields in the database again. Now Click on each of the labels in the Data Sources window and drag them over to your application as shown below.
Adding Data Source items to the form
When you compile and run your program it should now automatically access your program. If you hit the right arrow it will send you through the people you entered, you can also go back, add people with the plus and delete them with the x. To finalize your changes click the disk object to actually save to file. If you wish to add someone, say Angela Jolie, first fill in all the fields, give a new id number that is bigger than the last id number used and click the disk object. Below is a running example of the program so far.
Test run of the connection
Add the following tools to your form, and set the PictureBox to size mode=stretch image and give it a Border Style of Fixed3d.
Form's setup
Load a default non picture image (you are welcome to use mine) and save this as "default.jpg". To add pictures to your project add jpg's saved as a person's first name+last name+".jpg". Put these pictures in the innermost debug directory of your debug folder.
Form's setup
Add the following Code:
Public Class Form1  
    Private Sub Table1BindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Table1BindingNavigatorSaveItem.Click
        Me.Validate()
        Me.Table1BindingSource.EndEdit()
        Me.TableAdapterManager.UpdateAll(Me.PhoneBookDataSet)
    End Sub  
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PhoneBookDataSet.Table1' table. You can move, or remove it, as needed.
        Me.Table1TableAdapter.Fill(Me.PhoneBookDataSet.Table1)
        loadpic()
    End Sub  
    Private Sub BindingNavigatorMoveNextItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BindingNavigatorMoveNextItem.Click  
    End Sub  
    Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Table1TableAdapter.Fill(PhoneBookDataSet.Table1)
        loadpic()
    End Sub
    Sub loadpic()  
        Dim FirstN As String = ""
        Dim LastN As String = ""
        Dim fileName As String = ""  
        FirstN = FirstNameTextBox.Text
        LastN = LastNameTextBox.Text
        fileName = FirstN & LastN & ".jpg"
        Try
            PictureBox1.Image = Image.FromFile(fileName)
        Catch
            PictureBox1.Image = Image.FromFile("default.jpg")
        End Try
    End Sub  
    Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Table1BindingSource.MoveNext()
        loadpic()
    End Sub  
    Private Sub Button3_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Table1BindingSource.MoveLast()
        loadpic()  
    End Sub  
    Private Sub Button4_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Table1BindingSource.AddNew()
        loadpic()
    End Sub  
    Private Sub Button5_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Validate()
        Table1BindingSource.EndEdit()
        Table1TableAdapter.Update(PhoneBookDataSet.Table1)
    End Sub  
    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
        Table1BindingSource.RemoveCurrent()
        loadpic()
    End Sub  
    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
        Table1BindingSource.CancelEdit()
        loadpic()
    End Sub  
    Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
        Table1BindingSource.RemoveFilter()
        Table1BindingSource.CancelEdit()
        loadpic()
    End Sub  
    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Dim first, last, phone As String
        first = FirstNameTextBox.Text
        last = LastNameTextBox.Text
        phone = ""
        Try
            If TextBox1.Text <> "" And TextBox2.Text <> "" Then
                Table1BindingSource.Filter = "LastName='" & TextBox2.Text & "' AND " + "FirstName='" + TextBox1.Text + "'"
            ElseIf TextBox1.Text.Equals("") Then
                Table1BindingSource.Filter = "FirstName='" & TextBox1.Text & "'"
            ElseIf TextBox2.Text <> Equals("") Then
                Table1BindingSource.Filter = "LastName='" & TextBox2.Text + "'"
            Else
            End If
        Catch
            PictureBox1.Image = Image.FromFile("default.jpg")
        End Try  
        Label5.Text = PhoneTextBox.Text 'loads the phone number
        If Label5.Text.Equals("") Then
            Label5.Text = "Not Found"
        End If
        loadpic()
    End Sub
End Class
The Finished program should look as follows:
Finished Database Program

0 comments:

Post a Comment