Log inUsernamePassword
Log me on automatically each visit    
Register
Register
Log in to check your private messages
Log in to check your private messages
Visual Basic Forum for Visual Basic Programmers VB Forum Index » Database & Reporting

Post new topic   Reply to topic
Making sure a field is kept unique
View previous topic :: View next topic  
Author Message
Mitesh2004
Freshman


Joined: 26 Jan 2004
Posts: 43

PostPosted: Feb 17th, 2004 07:54 AM    Post subject: Making sure a field is kept unique Reply with quote

Hi,

I am using ADO to connect to my database. I have a form that allows the users to enter in details of events into a database. I want to make it so that the same EventName is not duplicated in any of the records.

I am guessing that i will have to do a search in my database to make sure that none of the other events have the same EventName. Am i right? If so, how would I do this sort of search in the database?

Your help will be greatly appreciated, Cheers!
Back to top
View user's profile Send private message Send e-mail
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Feb 17th, 2004 07:58 AM    Post subject: Reply with quote

Which database are you using ? If you're using MS Access then just do the following:

1. Open the database in MS Access.
2. Open the table in Design View.
3. Select the Field you want to be unique..
4. After selecting the field check the Indexed property and set it to Yes (No Duplicates).
5. Save and Close!

This will solve your problem.

Thanks!
_________________
Code Snippets, Tutorials, Utilities, Controls

Low cost Web Hosting
Hosting starts at as low as $4 per year!


Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Mitesh2004
Freshman


Joined: 26 Jan 2004
Posts: 43

PostPosted: Feb 17th, 2004 08:07 AM    Post subject: Reply with quote

Thanks for that Avis,

I have just tried that and it works from the database point of view. However, when a record of the same EventName as an existing record is attempted to be added, my VB program crashes with an error.

How do i prevent this from happening? Is there any code that i can write for when the 'Add' button is clicked?
Back to top
View user's profile Send private message Send e-mail
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Feb 17th, 2004 08:26 AM    Post subject: Reply with quote

Hi!

Use this just replace the error number you get with the error number in this one:

Code:
On Error GoTo NoError:

'Put all your
'source code here

NoError:
If Err.Number = 123456 Then MsgBox "Event name already exist!"


Hope this helps!

Thanks!
_________________
Code Snippets, Tutorials, Utilities, Controls

Low cost Web Hosting
Hosting starts at as low as $4 per year!


Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Mitesh2004
Freshman


Joined: 26 Jan 2004
Posts: 43

PostPosted: Feb 17th, 2004 08:29 AM    Post subject: Reply with quote

Thanks for this Avis,

But i have been told that it is bad coding practice to use 'GoTo'. Am i correct? or do you think it is ok to use GoTo in this case?
Back to top
View user's profile Send private message Send e-mail
Avis
Junior Poster


Joined: 07 Oct 2003
Posts: 510
Location: India

PostPosted: Feb 17th, 2004 08:34 AM    Post subject: Reply with quote

Its okay to use Goto in this case as there's no other alternative. On the other hand you can also use On Error Resume Next ...but i don't know then how to trap the errors. This will only keep on ignoring the errors and you won't get any message.

But On Error Goto you can trap errors and control the behaviour and other stuff as we're doing in here. Using too many Goto's in a program is a bad practice but one Goto does'nt hurts! :p
_________________
Code Snippets, Tutorials, Utilities, Controls

Low cost Web Hosting
Hosting starts at as low as $4 per year!


Always follow posting guidelines
Put your VB code in [vb ] your code [ /vb] tags!
Back to top
View user's profile Send private message Send e-mail Visit poster's website Yahoo Messenger MSN Messenger ICQ Number
Display posts from previous:   
Post new topic   Reply to topic    Visual Basic Forum for Visual Basic Programmers VB Forum Index » Database & Reporting All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Visual Basic Forum runs phpBB | Forum Template © iOptional
VB Resources | SSL | Visual Basic