Vb6 Upload File to Update Database in Access

Making The Information Enlightened Program Data Enlightened!

Code View (…cont.)

Every bit I said earlier, I prefer to control it all myself in my own code.

In this program, there are ii forms; the principal form which you saw before, and ane specifically set up to allow the user to add a new contact (friend) to their database.

Yous'll want to reference the code while reading the residual of information technology and it's on my website in ii web pages:

Form1.vb

AddNewContact.vb

You might want to open those and just minimize it as I have no (or little) comments in the code itself. I'll explain what I've done in the text of this prose.

In the class-level declarations nigh the acme of Form1, you'll see that I've set up upwards a new class that I'm calling "ContactInfo". I'll use that later when I get into calculation a new contact. For now just suffice that it's the easiest way (in my stance) to movement a related agglomeration of information equally one single object.

After the information is "filled" by the tabular array adapter, in the class's load event, I also bank check whether or non at that place's any data in the table:

Code Snippet one

That sub just looks to see what the row count is and if nothing is in that location, it disables both the DataGridView itself and the " Remove Selected Contact " push button; otherwise information technology enables them. You lot might wonder if information technology'southward not dangerous to assume that at that place's a row selected (ergo, what if they click the " Remove Selected Contact " push and at that place'due south nothing selected!)

Information technology'due south not a problem for one very simple reason: Y'all cannot Non select a row in a DataGridView; a row is ever selected , menses, the end, no manner around information technology. Therefore, information technology's non dangerous at all to presume there's ever a row selected.

Whether there are or aren't whatever rows in the datatable, the " Add New Contact " button is always enabled. Let's get to that part now.

When the user clicks that push button, the code in the button'due south click result will create a new instance of the AddNewContact.vb class. That'due south done hither:

Lawmaking Snippet two

We'll become back to everything subsequently the line "anc.ShowDialog" momentarily.

Look now at the lawmaking for AddNewContact.vb and notice at the top, in the course-level declarations, the showtime one is "userCancel" as a Boolean type variable, set to default to logical True. Let me explicate what this is for and why information technology's defaulted to Truthful:

We're showing this form modally (".ShowDialog") which means that this form volition evidence up on tiptop of the main form and the main form cannot get focus again until this dialog is closed. So how can they close the dialog?

I forgot to prepare the form's .AcceptButton and .CancelButton (yous should practise that though) but even with that, if the user simply clicks the " 10 " to close the course, when it returns back to Form1 y'all're in a quandary as to whether they clicked the OK button or not.

This is how I prevent information technology. Using that Boolean variable, the ONLY way that information technology gets set up to imitation is when I set up it to imitation in that form's " OK " button, so when it returns, if it'due south not false so I know the user closed the dialog either with the "Cancel" button or the " 10 ", and honestly it doesn't thing which it was; it wasn't the " OK " one!

The form itself looks similar this (I forgot to take a screenshot only of how the form is laid out, only this volition evidence you well enough):

The data itself has to be validated in a number of means. If you lot think when I created the database tabular array itself, I did Non set the three main fields (columns) to non allow nulls; nonetheless subsequently in lawmaking if you have a null value it will plague you when you endeavor to update (save) the data, so I don't allow it and I do that in lawmaking.

This was one reason for the class that was created and the defaults used; if "no data is there" from the user, it volition instead utilize what information technology'due south defaulted to in the class definition.

For this programme I'm saying that the new contact's first name and last name have to "exist something other than bare" and also that the telephone number (a string – more most that afterward) is completely optional but if information technology's there, information technology can ONLY be 10 digits with no literals.

All of that is checked in the code that you see.

On this second form, I'g also using an Error Provider to display a message to the user whenever there's errant data entered. At that place's one in your toolbox and information technology'southward no more difficult to add it than only elevate information technology to your form. It'll show up in the components tray at the lesser of your Design View when you do.

Look at this bit of lawmaking:

Code Snippet three

A few points of interest in that snippet:

First, this is the Validating Effect for both the Showtime Proper name textbox and the Last Proper noun textbox. Information technology'south like to the ".Leave" event in that it fires once the control has had focus and then loses information technology, but that's where the similarity ends. The event arguments for the .Validating consequence are wholly different and we'll use that difference in the code that yous're looking at thusly:

If an error is spotted, we will prevent them from leaving that command (the textbox) until they fix information technology! They can blank it out and go out the control but since I have the program set up to only enable the " OK " push button if at that place'south information in both the outset and final names, it still won't permit them add the new name.

The other bespeak of interest at that place is something which may puzzle y'all at first, and that's the LINQ query:

Dim qry = From Friends In Form1.ContactsDataSet.Friends _

Where Friends.FirstName.ToLower.Replace( " "c , "" ) & _

                     Friends.LastName.ToLower.Replace( " "c , "" ) = wholeName

The purpose of it is to forbid the user from adding the same name twice. Since I am also requiring both a first and a last name though, I have to do a bit of work to ensure this AND to know it wasn't simply a divergence in possibly adding a few spaces between or i being Proper Example and the other being Lowercase, etc..

The query above checks that. It strips out all spaces of both showtime name and last name, concatenates them in code, changes both what it'southward looking for and what it's await at to lower case So asks the question: "Are you here?".

If information technology comes back showing a count greater than zero, and then I don't allow it to be added:

In one case they have entered valid data and they click the " OK " button, they code returns to Form1 again after the "anc.ShowDialog". Expect over again at Code Snippet 3 and you can come across what next happens.

At this betoken, the code in Form1 at present asks the 2d form "Did the user click the OK push?" and it does that past looking at the variable "userCancel" in that second form.

"Wait, wait, expect … uhh, hold on, how can information technology see that variable in another form??"

Ah, glad you asked!

In the second grade, find that the 2 variables (i being "userCancel" and the other existence "ContactToBeAdded which is an instance of the new class from Form1) are both declared every bit Public. The new class – ContactInfo – in Form1 is public and that'due south how the second form tin "run across" it also.

Now back to where nosotros left off. Once it returns to Form1 and verifies that user did, in fact, click the " OK " push button, it so creates a new datarow in the datatable ("Friends"), and using the example of the form returned ("ContactToBeAdded"), information technology so fills in the information and adds that new row.

The adjacent pace is to tell the BindingSource that we're done editing (.EndEdit) and it and so looks to run across IF there's a pending change. This is way that [normally] helps yous avert that ill-blighted "Concurrency Violation" exception.

The reason why there aren't whatsoever nulls – and cannot exist – is because of how the data is returned. Look at this at present from the second course:

Code Snippet 4

That'due south the " OK " push's click event on the second grade. Run across how it'southward washed now? When they click the " OK " button on that grade – and not until then – the new instance of the grade that was created in that 2nd grade is so populated with the data that the user entered.

The reason I'm not worried if there'due south no phone number is considering of the default set in the field of the grade and THIS style – no nulls! Nosotros can take empty text (actually in mine you lot tin't because it defaults to "—"), and that'due south fine, but not Zip!

Speeding things up a chip here, when they add a new name, it's automatically right and so and there updated (saved). You can change the behavior if you want to, but that'southward how I take it set upwards in this.

Permit'southward at present have a expect at the program with two names added:

If they select a proper name and click to delete the name, they're presented with a confirmation dialog:

If they click the " Yeah " on that dialog, the program will remove the name from the table and one time again, will automatically salvage (update) the database. Again, yous tin can modify this behavior if you want to.

All that is shown here:

Code Snippet 5

Do you encounter how I'm doing it? If you're not sure, then inquire only I'll move along to the last role of it at present.

Wait at the region in the code of Form1 called "Check/Save Changes". Information technology's already gear up up and waiting on you (or me if need be) to modify the code to allow the user to change the data – only not in the DataGridView.

In your lawmaking, you'll need to "send information technology to the sub" called "CheckDB_NeedsSaving" at the appropriate place. Prior to doing so, be sure to tell the BindingSource to .EndEdit, then send it to the sub to show the user that it needs to be saved and the " Save Changes " button will be enabled for them to exercise so.

Endmost Remarks

This is merely a barely functional plan – it needs a lot of embellishing just my focus here was to become you started in the right direction.

If y'all want, I'll nada upward the unabridged projection folder and upload it for you to use.

I hope it's been helpful. :)


Delight telephone call me Frank :)

jenkinsbehen1945.blogspot.com

Source: https://social.msdn.microsoft.com/Forums/vstudio/en-US/d53b58a5-85b8-4976-8c5c-db7f505597a9/vb-code-for-updating-a-access-database?forum=vbgeneral

0 Response to "Vb6 Upload File to Update Database in Access"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel