Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  .NET Newbie  »  My First Access Database Program. Part 2
 »  Home  »  Data Programming  »  My First Access Database Program. Part 2
 »  Home  »  Data Programming  »  Microsoft Access  »  My First Access Database Program. Part 2
My First Access Database Program. Part 2
by George Poth | Published  01/21/2004 | .NET Newbie Data Programming Microsoft Access | Rating:
George Poth

I have been teaching English in Brazil since 1994 and always wanted to do more for learners than common textbooks can offer. This started with web sites that couldn't reach most students as computers and the Internet are not standard for most people in this country.

Computer tools to help Brazilian students learn a complex language like English are practically non-existent and so I sent some suggestions to software companies. Since Brazil is neither a target market for English textbooks nor for software of this kind, the rejection seemed natural.

As a result, I tried some free developer tools such as Borland's free C++ compiler, Free Pascal, and Envelope's Visual Basic. Envelope's Visual Basic, which is a Microsoft Visual Basic 1.0 clone and still available, suited my taste but I knew it was obsolete technology. In March 2003, I bought a copy of Microsoft Visual Basic .NET Standard and have been hopelessly contaminated with the programming virus ever since.

I mostly write programs for educational purposes. Having discovered the wonderful world of DirectX recently, I am diving into the most entertaining part of programming: games. One can connect teaching with pure entertainment, learning, and culture.

 

View all articles by George Poth...
My First Access Database Program. Part 2

Article source code: my1st_accessdb.zip

Continued from My First Access Database Program. Part 1

Binding the Controls

Click the "lblNavNum" label which reads "No Records" once to select it as shown in Figure 24.

(Figure 24)

Go to the Properties window and find the property "(DataBindings)" as shown in Figure 25.

(Figure 25)

Expand this node by clicking the "+" sign. This should look as shown in Figure 26.

(Figure 26)

Click the "(None)" on the right from the "Text" property once to activate the drop down box. Click the arrow button to expand the property as shown in Figure 27.

(Figure 27)

Expand the "dsContacts" node by clicking the "+" sign. This should look as shown in Figure 28.

(Figure 28)

Expand the "tblContacts" node and click "ID" as Figure 29 suggests.

(Figure 29)

Scroll to see the "Text" property. If you can see a golden cylinder as shown in Figure 30, then you have made everything correctly.

(Figure 30)

Repeat the same procedure with the text boxes - not the labels. We used the label here because it is going to display the current entry number and the number of total entries. It cannot be edited, that's why we used a label for this item and not a text box. The binding of the remaining controls should be according to what data the controls are supposed to hold. For example, the txtFirstName text box should be linked to FirstName of our table. When you're through with this, it would be a good idea to build the solution. To do so, press Ctrl + Shift + B.

Coding

I do hope you are a patient programmer because the amount of code here can be hefty for some beginners. So if things won't work, then you have made something different from what's written here. In the past, I received tons of e-mails telling me my code wouldn't work. Those people just didn't tell me that they had named things differently from the suggestions, or that they had written code in a different place, etc., etc. If you absolutely insist on naming things differently or writing code wherever you think appropriate, fine! But please don't blame me if it's not working.

Note: Although the buttons for a backup utility, an about form, and a help file are present, I haven't included the necessary code. Neither have I included the forms/file. An earlier article on how to make a help file should get you going with at least one of the three missing parts. Including the three missing parts here would make this tutorial awfully large, so I have omitted them for today. That doesn't mean that I will leave you on your own forever in terms of making an About form or a backup utility.

Press F7 to go into code view. From the Class Name list, shown in Figure 31, select "(Base Class Events)".

(Figure 31)

From the Method Name list, shown in Figure 32, select "Load".

(Figure 32)

Edit the code so that it looks like Code 01.

(Code 01)

Private Sub frmSample_Load(ByVal sender As Object_
    ByVal e As System.EventArgsHandles MyBase.Load

    '[Change dbconnection to application startup path]
    Try
        Me.odcContacts.ConnectionString = _
          "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source= dbSample.mdb"
    Catch eConnection As System.Exception
        MessageBox.Show(eConnection.Message)
    End Try
    '[Change dbconnection to application startup path/]

End Sub

Looking at Code 01

Code 01 changes the database connection to the application startup path. The application startup path is the directory in which your application is actually built. It's what we call the bin folder. Doing so will avoid some severe headaches later. If you don't change the path, the program will try to connect to your database with the generated path. The generated path could look like this on Windows XP:

C:\Documents and Settings\UserName\My Documents\Visual Studio Projects\Database Sample Program\bin\dbSample.mdb

On a different machine, a person might not have the C:\ directory. It's certain that not all systems have this "Documents and Settings". Even if they do, the user name will be different. And how about those who have a different language installed? As for instance, "My Documents" is "Meus documentos" in Portuguese, or "Eigene Dateien" in German. Hardly anybody will have a "Visual Studio Projects" folder. You see, it's really necessary because the application startup path is a virtual path. It doesn't matter whether the user installs this program on drive C, F, or some other weird location.

The "Try", "Catch", "End Try" is simple error handling. In cases where the code can't do what it is supposed to do, it will display a message box telling the user about the error instead of crashing the entire program. Yes, this could be done much better and with more details, but our subject here is database programming, not error handling. But I could think of getting back to that subject in the future.

Add Code 02 right before the "End Sub" of Code 01.

(Code 02)

    '[Load the database]
    Try
        odaContacts.Fill(dsContacts)
        lblNavNum.Text = _
          Me.BindingContext(dsContacts.tblContacts).Position
    Catch eLoad As System.Exception
        MessageBox.Show(eLoad.Message)
    End Try
    '[Load the database/]

Looking at Code 02

Simplified speaking, this code will fill the text boxes with data from the table. Press F5 to run the program. If you did everything correctly, you will see Figure 33.

(Figure 33)

Close the program to get back to the code window. You can collapse the Sub by clicking the "-" sign on the left as shown in Figure 34.

(Figure 34)

Type "# Region "Custom Procedures"" between the "Windows Form Designer generated code" region and the "Private Sub frmSample_Load" and press Enter. Actually, where you put this region doesn't matter; it's just my sense of order. This might be totally different with you. Add Code 03 so that it goes between the "# Region "Custom Procedures"" and the "# End Region".

(Code 03)

Private Sub dsContacts_PositionChanged()

    '[Display the changed position]
    Me.lblNavNum.Text = _
      (((Me.BindingContext(dsContacts_
      "tblContacts").Position + 1).ToString + " of "+ _
      Me.BindingContext(dsContacts_
      "tblContacts").Count.ToString)
    '[Display the changed position/]

End Sub

Looking at Code 03

It will display the changed position in the label. Since we need this code quite often, it is better to write it once and then call it where we need it. Otherwise we would have to type this code over and over again. How we call the code you will see when we need to do so. Important is that you remember the expression that goes after "Private Sub".

Add Code 04 right after the "End Sub" of Code 03.

(Code 04)

Private Sub ReadOnlyOn()

    '[Enable read-only mode]
    txtFirstName.ReadOnly = True
    txtSurname.ReadOnly = True
    txtEmail.ReadOnly = True
    tbrSave.Enabled = False
    tbrCancel.Enabled = False
    tbrEdit.Enabled = True
    '[Enable read-only mode/]

End Sub

Looking at Code 04

We need to protect data from being overwritten by accident. That's why we have to make all the text boxes read-only. When data is read-only, there is no good reason to let the user click the save or cancel button. But of course, the edit button must be enabled so that a user can make changes to an existing entry. Also here, we will need this code tons of times, so we write it once and call it when needed.

Add Code 05 right after the "End Sub" of Code 05.

(Code 05)

Private Sub ReadOnlyOff()

    '[Cancel read-only mode]
    txtFirstName.ReadOnly = False
    txtSurname.ReadOnly = False
    txtEmail.ReadOnly = False
    tbrSave.Enabled = True
    tbrCancel.Enabled = True
    tbrEdit.Enabled = False
    '[Cancel read-only mode/]

End Sub

Looking at Code 05

It would be incredibly smart if I said that this code is exactly the opposite of Code 04, wouldn't it? So let me spare you my dumb comments and let's go ahead.

Add Code 06 right after the "End Sub" of Code 05.

(Code 06)

Private Sub SetNavButtons(ByVal sender As System.Object_
    ByVal e As System.EventArgs)

    '[Enable or disable navigation buttons when appropriate]
    If Me.BindingContext(dsContacts_
      "tblContacts").Position = _
      Me.BindingContext(dsContacts_
      "tblContacts").Count - 1 Then
        tbrNext.Enabled = False
        tbrLast.Enabled = False
    Else
        tbrNext.Enabled = True
        tbrLast.Enabled = True
    End If
    If Me.BindingContext(dsContacts_
      "tblContacts").Position = 0 Then
        tbrPrevious.Enabled = False
        tbrFirst.Enabled = False
    Else
        tbrPrevious.Enabled = True
        tbrFirst.Enabled = True
    End If
    '[Enable or disable navigation buttons when appropriate/]

End Sub

Looking at Code 06

Now, this seems logical, doesn't it? If the user is at the first record, why should the first/previous button be enabled? I don't have the slightest idea. If you do, let me know. And if the user is at the last record ... C'mon, you know the rest.

We will call this procedure in a different way when we need it. It's called a delegate procedure then. But I don't want to bore you stiff with technical vocabulary. Ah, you find that interesting? Interesting is driving a car in Dallas - boy, I bet you've never seen that many middle fingers in your life than you can see there within the first ten minutes.

This region is complete, so please close it. It works the same way as you previously closed the Sub.

Now we can go back to the "Private Sub frmSample_Load". Add Code 07 right before the "End Sub" in the "Private Sub frmSample_Load".

(Code 07)

    '[See custom procedures]
    dsContacts_PositionChanged()
    '[See custom procedures/]

    '[See custom procedures]
    ReadOnlyOn()
    '[See custom procedures/]

    '[Enable or disable the navigation buttons]
    Dim bmContacts As BindingManagerBase = _
      Me.BindingContext(dsContacts"tblContacts")
    AddHandler bmContacts.PositionChanged_
      AddressOf SetNavButtons
    '[Enable or disable the navigation buttons/]

    '[Manually disable these buttons at startup]
    tbrFirst.Enabled = False
    tbrPrevious.Enabled = False
    '[Manually disable these buttons at startup/]

Looking at Code 07

The code of the first two items is actually written in the region we named "Custom Procedures". We just call the code by writing the same expression we used after the "Private Sub". Remember that we wrote "Private Sub dsContacts_PositionChanged ()" and "Private Sub ReadOnlyOn ()"? No? Go into the corner and say, "I'm a hell of a programmer!" Repeat that about 150 times and you'll be forgiven.

The next item is our binding manager, responsible for enabling or disabling our navigation buttons. We previously wrote a Sub in the region "Custom Procedures", right? I told you that we will use this in a different way. It also uses the Sub you wrote. Important to know here is that it will enable/disable the navigation buttons when the position has changed. And that's where the last piece of code comes in handy.

How can the navigation buttons be enabled/disabled when you haven't clicked them? Guess what! They can't. If you don't click them, the position can't change - unless you have a little built-in wizard to do that. You have to disable the buttons manually. This code shows you how it can be done. However, it's not the perfect solution. Why? I'm sure you'll discover it soon and (try to) improve it.

The "Private Sub frmSample_Load" is complete. If you like, write a region around this part and close the region. Those regions really do look organized when they're closed, don't they?

Now, we want that a user provides complete data without interrupting him/her with nagging message boxes. C'mon, some programs have more message boxes than anything else. I wonder when people are going to switch to error providers. They certainly will if those guys at Microsoft improve the error provider. One way to guide the user is to disable the save button as long as the necessary information is not complete.

Create a new region named "Text Boxes". Add Code 08 to the region.

(Code 08)

Private Sub Textbox_TextChanged(ByVal sender As Object_
    ByVal e As System.EventArgsHandles _
    txtFirstName.TextChangedtxtSurname.TextChanged_
    txtEmail.TextChanged

    '[Guide the user to provide all information]
    If txtFirstName.TextLength = 0 Or _
      txtSurname.TextLength = 0 Then
        tbrSave.Enabled = False
    Else
        tbrSave.Enabled = True
    End If

    If txtEmail.Text.Length = 0 Then
        tbrEmail.Enabled = False
    Else
        tbrEmail.Enabled = True
    End If
    '[Guide the user to provide all information/]

End Sub

Looking at Code 08

The essential information here is the first name and the last name. Some people still haven't stepped into the new century and don't have an e-mail address, so we can't simply disable the save button until the user invents an e-mail address just because the programmer thought it a good idea. But we can disable the "Send E - mail" button if there is no text. That way, a user couldn't accidentally click and open an e-mail to be sent to no one.

Now we've come so far, and it's about time we coded the buttons. However, we will not code them directly; we will call our code for the buttons later similar to the way we did before. Why? Once our toolbar is coded, you can forget it for most of the program's life. Any changes you might want to make will be in the actual code. The bottom line is that it actually doesn't matter. It's in fact my preferred way to code. I usually do it that way with every single bit of code. I've also started putting things into classes and modules whenever possible so I don't get confused. But in a tutorial, I change habits to make my readers' lives easier; especially at the beginning.

I believe the code is sufficiently commented so you can understand what's going on. Great! So I can spare you further brainless comments.

Create a region named "Common Buttons". The completed region should look like Code 09.

(Code 09)

#Region "Common Buttons"

    Private Sub Add()

        '[See custom procedures]
        ReadOnlyOff()
        '[See custom procedures/]

        '[Add a new entry]
        Try
            Me.BindingContext(dsContacts_
              "tblContacts").EndCurrentEdit()
            Me.BindingContext(dsContacts_
              "tblContacts").AddNew()
        Catch eAdd As System.Exception
            MessageBox.Show(eAdd.Message)
        End Try
        '[Add a new entry/]

        '[See custom procedures]
        Me.dsContacts_PositionChanged()
        '[See custom procedures/]

        '[Focus the first textbox]
        txtFirstName.Focus()
        '[Focus the first textbox/]

    End Sub

    Private Sub Edit()

        '[See custom procedures]
        ReadOnlyOff()
        '[See custom procedures/]

    End Sub

    Private Sub Save()

        '[Update]
        Try
            Me.BindingContext(dsContacts_
              "tblContacts").EndCurrentEdit()
            odaContacts.Update(dsContacts_
              "tblContacts")
        Catch eSave As System.Exception
            MessageBox.Show(eSave.Message)
        End Try
        '[Update/]

        '[See custom procedures]
        Me.dsContacts_PositionChanged()
        '[See custom procedures/]

        '[See custom procedures]
        ReadOnlyOn()
        '[See custom procedures/]

    End Sub

    Private Sub Cancel()

        '[Ask for confirmation]
        If MessageBox.Show("This will cancel the current entry." _
          & ControlChars.NewLine & "Do you want to continue?"_
          "Sample"MessageBoxButtons.YesNo_
          MessageBoxIcon.Question= DialogResult.Yes Then
            '[Ask for confirmation/]

            '[If the user confirms, cancel the entry]
            Me.BindingContext(dsContacts_
            "tblContacts").CancelCurrentEdit()
            '[If the user confirms, cancel the entry/]

            '[See custom procedures]
            Me.dsContacts_PositionChanged()
            '[See custom procedures/]

            '[See custom procedures]
            ReadOnlyOn()
            '[See custom procedures/]

        End If

    End Sub

    Private Sub Delete()

        '[Ask for confirmation]
        If MessageBox.Show("This will delete the current entry." _
          & ControlChars.NewLine & "Do you want to continue?"_
          "Sample"MessageBoxButtons.YesNo_
          MessageBoxIcon.Question= DialogResult.Yes Then
            '[Ask for confirmation/]

            '[Delete the entry]
            If (Me.BindingContext(dsContacts_
              "tblContacts").Count > 0Then
                Me.BindingContext(dsContacts_
                "tblContacts").RemoveAt(Me.BindingContext(dsContacts_
                "tblContacts").Position)
                '[Delete the entry/]

                '[See custom procedures]
                Me.dsContacts_PositionChanged()
                '[See custom procedures/]

                '[See custom procedures]
                ReadOnlyOn()
                '[See custom procedures/]

            End If

        End If

    End Sub

    Private Sub SendMail()

        '[Open an empty e - mail with address]
        System.Diagnostics.Process.Start("mailto:" _
          & txtEmail.Text)
        '[Open an empty e - mail with address/]

    End Sub

    Private Sub Shutdown()

        '[See Private Sub Save( )]
        Save()
        '[See Private Sub Save( )/]

        '[Close the program]
        Dispose(True)
        '[Close the program/]

    End Sub

#End Region

Create another region named "Navigation Buttons". The completed region should look like Code 10.

(Code 10)

#Region "Navigation Buttons"

    Private Sub First()

        '[Go to the first entry]
        Me.BindingContext(dsContacts_
          "tblContacts").Position = 0
        '[Go to the first entry/]

        '[See custom procedures]
        Me.dsContacts_PositionChanged()
        '[See custom procedures/]

        '[See custom procedures]
        ReadOnlyOn()
        '[See custom procedures/]

    End Sub

    Private Sub NavNext()

        '[Go to the next entry]
        Me.BindingContext(dsContacts_
          "tblContacts").Position = _
        (Me.BindingContext(dsContacts_
          "tblContacts").Position + 1)
        '[Go to the next entry/]

        '[See custom procedures]
        Me.dsContacts_PositionChanged()
        '[See custom procedures/]

        '[See custom procedures]
        ReadOnlyOn()
        '[See custom procedures/]

    End Sub

    Private Sub Previous()

        '[Go to the previous entry]
        Me.BindingContext(dsContacts_
          "tblContacts").Position = _
        (Me.BindingContext(dsContacts_
          "tblContacts").Position - 1)
        '[Go to the previous entry/]

        '[See custom procedures]
        Me.dsContacts_PositionChanged()
        '[See custom procedures/]

        '[See custom procedures]
        ReadOnlyOn()
        '[See custom procedures/]

    End Sub

    Private Sub Last()

        '[Go to the last entry]
        Me.BindingContext(dsContacts_
        "tblContacts").Position = _
        (Me.dsContacts.Tables("tblContacts").Rows.Count - 1)
        '[Go to the last entry/]

        '[See custom procedures]
        Me.dsContacts_PositionChanged()
        '[See custom procedures/]

        '[See custom procedures]
        ReadOnlyOn()
        '[See custom procedures/]

    End Sub

#End Region

Note: You might find it strange that I used "Private Sub NavNext" and not "Private Sub Next". "Next" is a keyword in programming and would appear blue. That's why you can't name a Sub "Next".

We can now come to our toolbar. Some people love to use the If ... Then syntax. Hmm, if we did that here, we would get into the Guinness Book of Records for writing so many "ifs" and "thens". Another possibility is to use the index number of each toolbar button. But honestly, if you add or delete a button later, you will regret having done it that way. Or worse, you change the sequence of your buttons entirely. Oh-oh, to get that going again will take you longer than it took me to write this article. Hey, writing articles takes more than a few hours, believe me. Remember that I asked you at the beginning to look at least at the tag properties of the toolbar? Hmm, I hope you did that, otherwise you might not understand the comment in my code.

Create another region named "Toolbar". The completed region should look as shown in Code 11.

(Code 11)

#Region "Tool Bar"

    Private Sub tbrSample_ButtonClick(ByVal sender As _
        System.ObjectByVal e As _
        System.Windows.Forms.ToolBarButtonClickEventArgs_
        Handles tbrSample.ButtonClick

        '[Use tag property to select button]
        Select Case e.Button.Tag

            Case "Add" 'This is the tag property
                Add() 'This is the previously written Sub
            Case "Edit"
                Edit()
            Case "Save"
                Save()
                '[See custom procedures]
                SetNavButtons(sendere)
                '[See custom procedures/]
            Case "Cancel"
                Cancel()
                '[See custom procedures]
                SetNavButtons(sendere)
                '[See custom procedures/]
            Case "Delete"
                Delete()
            Case "First"
                First()
            Case "Previous"
                Previous()
            Case "Next"
                NavNext()
            Case "Last"
                Last()
            Case "Email"
                SendMail()
            Case "Exit"
                Shutdown()
                '[Use tag property to select button/]

        End Select

    End Sub

#End Region

You can now run the program and try it out. Now, is that okay? No, it isn't. What if the user clicks the X Button? The thing would close and it would not save a possible current entry.

It's up to you if you want to interrupt the user with one of those pesky message boxes or not. Option 1 is if you just want to save any changes and exit without maddening the user.

Option 1

Select "(Base Class Events)" from the Class Name list and "Closing" from the Method Name list. Edit the code so that it looks like Code 12.

(Code 12)

Private Sub frmSample_Closing(ByVal sender As Object_
    ByVal e As System.ComponentModel.CancelEventArgs_
    Handles MyBase.Closing

    '[See Private Sub Save ()]
    Save()
    '[See Private Sub Save ()/]

End Sub

Option 1

This option is great if you want to save any changes but want to irritate the user with a message box. Edit the code so that it looks like Code 13.

(Code 13)

Private Sub frmSample_Closing(ByVal sender As Object_
    ByVal e As System.ComponentModel.CancelEventArgs_
    Handles MyBase.Closing

    '[Show a message when the X is clicked]
    If MessageBox.Show("Do you want to save and exit?"_
      "Sample"MessageBoxButtons.YesNo_
      MessageBoxIcon.Information= DialogResult.No Then
        '[Show a message when the X is clicked/]

        '[If user click NO then let form open]
        e.Cancel = True
        '[If user click NO then let form open]
    Else
        '[See Private Sub Save ()]
        Save()
        '[See Private Sub Save ()/]
    End If

I hope that this tutorial helped you with the very basics. Play with the code - it won't bite you - and see how you can improve it so that it works the way you want. As I already said, the About form and the backup utility for the database are not included here. Those will soon find their way to DevCity, so stay tuned.

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.51020408163265 out of 5
 49 people have rated this page
Article Score33129
Related Articles
Comments    Submit Comment

Comment #1  (Posted by George Maffey on 02/15/2004)

Well done George,

I am a new starter to vb.net and have followed all your articles so far completing all of them. I have found them extremely helpful and your teaching style is very good, it is very obvious what profession you are working in.

Keep up the good work and please continue to produce many more very user friendly articles as you have done so far.

Many Thanks
George

 
Comment #2  (Posted by Randy Robinson on 02/19/2004)

TYPO in source for VB.Net 2003 Pro.
In the example code for #Region "Tool Bar", "Tool Bar" must be one word, "ToolBar" -- #Region "ToolBar" not two words as in the source code example "Tool Bar". The instructor does in fact tell you to create a region named "ToolBar" (one word)) but the example code displays - #Region "Tool Bar" with a space, which is what I was paying more attention to and not the instructors actual instructions (my bad). As a newbie to VB.Net 2003 (one week) this drove me up a wall trying to find where I might have went wrong.

These tutors are the best I've seen by far on the internet. They are what I call " Idiot Proof " !!!
Thanks a bunch for your help.
 
Comment #3  (Posted by GP on 02/20/2004)

It doesn't make a difference in the 2002 edion whether you type # Region ToolBar, # Region Tool Bar, or # Region T ool b ar since a region is no instruction code. I didn't know that this is different in the newer edition and will pay more attention to it.
 
Comment #4  (Posted by Richard W on 02/20/2004)

Hi,

My compliments for writing this fine article. I am a beginning VB programmer, and this article gave an insight in using an Acces database in my application. I have a 'suggestion' though...

In this article I missed someting like "Where do we go from here". By that I mean examples for using datases with more then one table, links to other articles/websites containing detailed information about the subject and maybe some general tips for newbies starting to work with Acces databses.

I found that after reading this article I wanted to know more about the subject. But as I am new to programming I didn't know where to start. For example the MSDN library contains so much articles with information that, generally speaking, every newbie's head starts to spinn.

Thanks again.

Richard
 
Comment #5  (Posted by GP on 02/21/2004)

Thanks for your kind words, Richard. I will have to disappoint you if you expected links to articles and/or websites. I do not and will not include links in my articles unless these are absolutely essential. One simple reason for this position is that web pages may be moved and web sites may be closed. I would not revisit a site that uses a lot of outdated links, so I do this in thought of many other readers.

Searching for something that’s really worth reading is time consuming, and honestly, it’s really not my job. When I started programming almost a year ago, I got upset at how little sites do for the absolute beginner. That’s why I turned to writing.

I’m glad you got curious, that’s exactly what I want to achieve with my articles. It’s like a slide show: if you want to see more pictures when the show is over – great. It’s bad if you look at the first picture and leave.

The MSDN library is large and confusing, and not only for newbies. However, there are many sites out there where you might find what you’re looking for. Good luck!
 
Comment #6  (Posted by Martin Vaupell on 02/23/2004)

It works Great on 2002 edition,
but when i try on my newly invested 2003(yepiii) edition.
Major compile errors, and wont run at all..

So just make it in 2002, upgrade it with the wizard 2003,
then its all good. :-)
 
Comment #7  (Posted by Gil on 02/26/2004)

Hi !
Thanks from the good sample
I see in your program that you do all the edit's method (edit,add,delete)
by the BindingContext object like
Me.BindingContext(dsContacts, "tblContacts").AddNew()
my question is why you didn't do it by the DataView object like
DataView.AddNew
I see in MSDN that it is not recommended

"You typically use this property only if you are creating your own control incorporating the CurrencyManager. Otherwise, to add items, if the data source is a DataView, use the AddNew method of the DataView class. If the data source is a DataTable, use the NewRow method and add the row to the DataRowCollection."

Thanks again.
Gil.





 
Comment #8  (Posted by GP on 02/26/2004)

Hmm, my code is based on Microsoft's generated code from the Data Form Wizard - with some changes. How come they don't like what they do, huh?
 
Comment #9  (Posted by Julian Muscat Double on 02/28/2004)

Hi George,

This is the fisrt time that i have read one of your articels, i must say that i have been looking for some helpfull means from which i can learn how to do databse connections for quite a while now but finally i came across your artical and i must say that it was very easy to understand and implement.

I have to say that thisis the best artical i found on this subject. Keep up the good work and thank you very much for your help.
 
Comment #10  (Posted by coateslr on 03/15/2004)

Great program George, even I a totally new programmer was able to accomplish this tutuorial with no problems. You steps were very simple and clear and the screenshots were perfect. Thanks for building my confidence.

One quick question. How do you search for a particular address?
 
Comment #11  (Posted by Amer on 03/22/2004)

Great tutorial!!!... Saved my arse in a test.. Thanks!?!
 
Comment #12  (Posted by muhammad Shahzad on 04/07/2004)

hi its really fantastic tutorila for bigginer of Vb. Net . it works deadly acurately. keep it up man.
 
Comment #13  (Posted by Vikas on 04/16/2004)

It was a really nice article. Great help with this one. I have an assignment which asks to do the same and this will help me a lot. thanks
 
Comment #14  (Posted by TAVI on 04/26/2004)

Hi
Please help me on this problem
i heve a access database whith multiple relationships in this i have a table ARTICLE in this i have : BARCODE, ART_NO, DESCR, SELL_PR etc
in an text box i want to type the BARCODE and pushing a button to search the product in the database and in another 4 textboxes to display ART_NO, DESCR
SELL_PR, etc...

this works only if i create an INDEX field whith AutoNumber in table article and in the serch textbox i serch after that
But i want to serch after the BARCODE FIELD

Please Help me

 
Comment #15  (Posted by Sofia on 04/26/2004)

Hi
I really liked your tutorial. It helped me to grasp some concepts. Thanks. :)
But a problem came up. I used the code in the tutorial in an access databse (one table only), and on and off when i started the application and added a new record, saved it, and then later tried to delete it, it gave me the dbconcurrency error - deletecommand affected 0 records. I went back to the sample and it gave me the same error. I've checked and re-checked the code and can't find the error. If you could help, it would be reaally nice. I've been at this for some five hours and can't fix it . Newbie despair ;)
Thanks

Here's the code: (left out some buttons code)
Region "Common Buttons"

Private Sub Add()

'[See custom procedures]
ReadOnlyOff()
'[See custom procedures/]

'[Add a new entry]
Try
Me.BindingContext(dsContacts, _
"tblContacts").EndCurrentEdit()
Me.BindingContext(dsContacts, _
"tblContacts").AddNew()
Catch eAdd As System.Exception
MessageBox.Show(eAdd.Message)
End Try
'[Add a new entry/]

'[See custom procedures]
Me.dsContacts_PositionChanged()
'[See custom procedures/]

'[Focus the first textbox]
txtFirstName.Focus()
'[Focus the first textbox/]

End Sub

Private Sub Edit()

'[See custom procedures]
ReadOnlyOff()
'[See custom procedures/]

End Sub

Private Sub Save()

'[Update]
Try
Me.BindingContext(dsContacts, _
"tblContacts").EndCurrentEdit()
odaContacts.Update(dsContacts, _
"tblContacts")
Catch eSave As System.Exception
MessageBox.Show(eSave.Message)
End Try
'[Update/]

'[See custom procedures]
Me.dsContacts_PositionChanged()
'[See custom procedures/]

'[See custom procedures]
ReadOnlyOn()
'[See custom procedures/]

End Sub
Private Sub Delete()

'[Ask for confirmation]
If MessageBox.Show("This will delete the current entry." _
& ControlChars.NewLine & "Do you want to continue?", _
"Sample", MessageBoxButtons.YesNo, _
MessageBoxIcon.Question) = DialogResult.Yes Then
'[Ask for confirmation/]

'[Delete the entry]
If (Me.BindingContext(dsContacts, _
"tblContacts").Count > 0) Then
Me.BindingContext(dsContacts, _
"tblContacts").RemoveAt(Me.BindingContext(dsContacts, _
"tblContacts").Position)
'[Delete the entry/]

'[See custom procedures]
Me.dsContacts_PositionChanged()
'[See custom procedures/]

'[See custom procedures]
ReadOnlyOn()
'[See custom procedures/]

End If

End If

End Sub
 
Comment #16  (Posted by djean on 05/01/2004)

Great job! I think you've saved my life! (but really.. .) my classmates & I have been struggling with our final project for our vb class and have had great difficulty getting our info to appear in our bound boxes... I will be sending each of them a link to this article! You are great!
 
Comment #17  (Posted by Ronald on 05/04/2004)


 
Comment #18  (Posted by Felix Guerrero on 05/10/2004)

A great article, so easy to explain the access and the other things with data access, i'm a new started at dot net , ADO, ASP.NET, i'll hope for others news, tanks from Merida - Venezuela
 
Comment #19  (Posted by Mike on 05/12/2004)

It's the perfect tutorial I was looking for for two days : complete and good explainations

thanx.
 
Comment #20  (Posted by Era on 05/24/2004)

Dear George,
I am basically a typical VB/VBA programmer and was really satisfied with those recordsets and all that stuff in DB programming but the way taught this thing data adapter it realy makes a fun of doing that painfull job db programming but look i am still doubtfull about these type of facilities cause once i used that hopeless data controll in project and it kicked my butt on the completion of the project Thanking u once again hope i'll get as best as this from u ever.
Gollum
 
Comment #21  (Posted by RS10 on 05/28/2004)

George,
thanks for the greatest tutorials for newbies I have ever seen
please look at this piece of code (enables button Save only when both First name and Surname are filled in)

If txtFirstName.TextLength = 0 Or _
txtSurname.TextLength = 0 Then
tbrSave.Enabled = False
Else
tbrSave.Enabled = True
End If

Can it be rewritten like this?
If txtFirstName.TextLength <> 0 And txtSurname.TextLength <> 0 Then
tbrSave.Enabled = True
Else
tbrSave.Enabled = False
End If
 
Comment #22  (Posted by Mauricio Zarate on 06/20/2004)

I have to congratulate for this tutorial, Really i'm a beginner in this, and i was finding the code to manipulate a access database, but it's more dificult, whatever, thanks, thats nice.

Improving that moment, i have a problem, because i want to divide a text, if the program find's a ' \ ' for example, i don't know how to do that; I spect someone help me with that.
 
Comment #23  (Posted by Maninder on 08/09/2004)

George Poth, you have done very food job. I am very new to VB. And today databases are the main edges used in programes. So your article is very good source for new starter guys like me. Thanks for your extremely valuable articles. Looking more from you.....
 
Comment #24  (Posted by Kaushik on 11/09/2004)

Excellent !!! No book writer could do better. Hope to learn more from you. Thanks a lot.
 
Comment #25  (Posted by Nickson on 11/18/2004)

Thanks for ur gr8 work.This wat i m looking for.Thank god i got it.This article is veri useful to me.thanks again for ur work.i hope to c more form
 
Comment #26  (Posted by John Carter on 11/20/2004)

This is a great tutorial. I've completed parts one & two within an hour and found everything works perfectly, despite a few differences between my .net environment and yours. ( I have put the code in FrmSample Events as there isn't a "Base Class Events" entry)

I have only had VB.net for about a week and I was struggling with converting my Access/Excel database applications to VB.NET - not now.

Thanks a lot and I am now going to Part 3
 
Comment #27  (Posted by Aung Ye Thu on 12/01/2004)

Dear Sir,

 
Comment #28  (Posted by Aung Ye Thu on 12/01/2004)

Dear Sir,
Thinks a lot,
I hope like this always.
 
Comment #29  (Posted by Barry Deen on 12/24/2004)

Perfect Tutorial. My first day programming in vb.net and I understood everything. You are a gifted tutor :)
 
Comment #30  (Posted by an unknown user on 01/05/2005)
Rating
I wish all tutorials on the web were written this well. Thanks
 
Comment #31  (Posted by John La Thekkemuriyil on 01/05/2005)
Rating
this helps a lot to be aware of DB connections...
 
Comment #32  (Posted by an unknown user on 01/27/2005)
Rating
Great info. Very helpful
 
Comment #33  (Posted by Steve on 02/01/2005)
Rating
Thanks for taking the time to do this simple form. So many demos use data grids to demonstrate data access which is not nearly as useful (in the real world) as a single record form with info on how to navigate the dataset, etc. Bravo!
 
Comment #34  (Posted by an unknown user on 02/05/2005)
Rating
Good for a starting primer on database. Well written George.
 
Comment #35  (Posted by an unknown user on 02/05/2005)
Rating
Pretty great stuff''

I still would like a walkthrough created on developing a simple insert new record form into an Access and/or SQL data base.
 
Comment #36  (Posted by an unknown user on 02/06/2005)
Rating
The tutorial is great for teaching how to make a Email database, but severly lacking in explaining what commands are used to update, delete, find, display what are on the rows. The connection material is good - although it doesn't explaint what OleDbDataAdaptor1 actually is - which to really understand why I need those commands etc inside the program, would be nice to know.

Further more - it seems to concentrate more upon activating and deactivating buttons in the toolbar, which has absoloutely no bearing on learning what code is needed to update or manipulate data inside the database.

I fail to even see a part where it states "This is the code that you will use to update the database dependant upon the information you entered in your fields".

The first page was great although light on explanation, this second one is full of information only relevent to the email program, and leaves people looking for information upon updating rows, inputting information and manipulating entries out in the cold.
Since people refer to this as the tutorial to learn how to use MSAccess with VB, its left me being able to connect to a database by means unknown to me, and thats it. I still cannot find out/understand what commands will be needed to enter/update information and to delete it as well.

I hate to seem like an overcritical and moaning git - but after being referred to this twice, I am still none the wiser as to the basic controls/explanations of what they do.
 
Comment #37  (Posted by Anne on 02/15/2005)
Rating
I am getting an error when trying to save after editing and deleting a record. It works fine for adding. The error is:
Concurrency Violation: the DeleteCommand affected 0 records.
Any ideas?
 
Comment #38  (Posted by an unknown user on 03/18/2005)
Rating
I've tried a variety of variations on this code and keep getting the same error ... Youdo not have permission to open the file or it is in use by anohter user when I try to open the mdb. According to Microsoft Knowledge base, I need to set access permissions on the MDB and/or the directory, but it doesn't tell me how. Any ideas?
 
Comment #39  (Posted by an unknown user on 03/18/2005)
Rating
I've tried a variety of variations on this code and keep getting the same error ... Youdo not have permission to open the file or it is in use by anohter user when I try to open the mdb. According to Microsoft Knowledge base, I need to set access permissions on the MDB and/or the directory, but it doesn't tell me how. Any ideas?
 
Comment #40  (Posted by an unknown user on 03/28/2005)
Rating
I came looking for help with Access Databases and got information on a better way to do toolbars. Thank you very much.
 
Comment #41  (Posted by an unknown user on 04/04/2005)
Rating
its very useful for beginner.. excellent. Thanks.

-Balakumar
 
Comment #42  (Posted by an unknown user on 04/06/2005)
Rating
Told me everything I needed to know
 
Comment #43  (Posted by an unknown user on 04/12/2005)
Rating
Excellent tutorial...shows some great techniques for organizing the code.
 
Comment #44  (Posted by an unknown user on 04/12/2005)
Rating
Excellent tutorial...shows some great techniques for organizing the code.
 
Comment #45  (Posted by an unknown user on 04/18/2005)
Rating
Am I the only one who had a problem with Code 02? The 4th (continued line) goes . . . (dsContacts.tblContacts) . . . which was not acceptable to my system. I changed it to . . .(dsContacts."tblContacts") . . . and all seems ok.
Not complaining, I assure you, just trying to learn!
 
Comment #46  (Posted by an unknown user on 05/01/2005)
Rating
I'm flabbergasted that it works on my .NET Standard Edition!

This article was extremely helpful in unveiling the mystery of the Datagrid.

Thanks for your effort.
 
Comment #47  (Posted by an unknown user on 05/02/2005)
Rating
Well done!
 
Comment #48  (Posted by an unknown user on 05/09/2005)
Rating
nice one
 
Comment #49  (Posted by an unknown user on 05/10/2005)
Rating
I hardly do any comments on any website but i need to say thanks for this one.
 
Comment #50  (Posted by an unknown user on 05/13/2005)
Rating
it is great help can you please write an example where you use MDI form and make connection through commands rather then wizard then it will be uniq help on the wib
 
Comment #51  (Posted by an unknown user on 06/22/2005)
Rating
Really good... I learn a lot.. =)
 
Comment #52  (Posted by an unknown user on 06/23/2005)
Rating
good for learn
 
Comment #53  (Posted by an unknown user on 06/29/2005)
Rating
The code was very clear and well documented.
 
Comment #54  (Posted by an unknown user on 07/30/2005)
Rating
you are very good teacher
with special tanks
arash maali
 
Comment #55  (Posted by Gerardo Silva on 08/27/2005)
Rating
Thank you for the worth tutorial that you shared with all of us.

Congratulations
 
Comment #56  (Posted by an unknown user on 10/01/2005)
Rating
BECAUSE EVERY STEP IS SO CLEARLY SPECIFIED
 
Comment #57  (Posted by Slim on 11/30/2005)
Rating
Great Job, Thank you!
 
Comment #58  (Posted by terence on 12/08/2005)
Rating
quite educative
 
Comment #59  (Posted by an unknown user on 12/15/2005)
Rating
Well done, Keep up the good job. This tutorial is really very helpful!!!!Thanxs a lot!!
 
Comment #60  (Posted by an unknown user on 12/26/2005)
Rating
You could give some insight on the properties and methods of the objects related to database programming. Though generally ok, i reckon.
 
Comment #61  (Posted by an unknown user on 02/09/2006)
Rating
easy to read and understand codes
 
Comment #62  (Posted by an unknown user on 03/20/2006)
Rating
Thanks a lot
 
Comment #63  (Posted by an unknown user on 10/11/2006)
Rating
The best tutorial for newbies like myself. Very easy to follow & it goes with the programming principle of learning by going through the examples. To George Poth, I hope you do more tutorial applets of this kind. Thank you very much. You made programming easier to learn!!!!
 
Comment #64  (Posted by an unknown user on 02/20/2007)
Rating
Finally and article that spells out Windows Forms data binding in a way that everyone can learn from! Great Job!

Thanks
Scott
 
Comment #65  (Posted by an unknown user on 04/06/2007)
Rating
Muy Bueno, claro y sencillo.

 
Comment #66  (Posted by an unknown user on 04/16/2007)
Rating

Yep - this is exactly the level I needed to get going. thanks George....and it worked
 
Comment #67  (Posted by an unknown user on 07/23/2007)
Rating
ei aung ye thu, check your grammar pls..
 
Comment #68  (Posted by an unknown user on 11/27/2007)
Rating
Cheers mate this tutorial was a real life saver.
 
Comment #69  (Posted by an unknown user on 12/26/2007)
Rating
Fantastic work i am new in vb.net, every thing was clear and quit simple,full marks

10 stars for u

10********************************

thanks for this (for new bies)
 
Comment #70  (Posted by an unknown user on 02/20/2008)
Rating
Great job Gorge
My background is mostly language programming with some VBA in the CAM side of CAD/CAM. All this .NET and databases is new to me.
You did a very good job of showing how to move around in a database and how to update records.
I did the program in VB.NET 2008 Express Edition. At first I was stumped by the different screen interfaces but from your comments I was able to figure-out which screens to use.
There were some differences in syntax of the Express edition I am using and the version you used but IntelliSense and the Object browser helped me find what I needed.

I have the code, If you are interested in doing an article for VB.Net 2008 Express my VbCity userID is bobo13


Thanks for a job well done.
Bob O

 
Comment #71  (Posted by Ranel Carreon on 05/02/2008)
Rating
I have not tried the code yet but i would like to know the programming language you're using and its version..thanks!
 
Comment #72  (Posted by an unknown user on 06/23/2008)
Rating
i would like to see your codes on another perspective let say the datasource, data set, databinding are merely defined on your code and not by dragging the controls. just merely 100% coding.
 
Comment #73  (Posted by an unknown user on 01/16/2009)
Rating
THANKS FOR YOUR HELPFUL ARTICLE
 
Sponsored Links