473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Primary Key

77
Hi Friends,

I have a table called Beneficiary where [Solar Lantern No] is the primary key. The user enter the [Solar Lantern No] number. Since the user enters the [SLNO], there is always a possibility that a duplicate value is entered. After entering the data, when the user saves the record, it should check whether the particular SLNo is already entered or not. if yes, then it should display a message box, else record saved.

By default there is a system generated message box.

looking fwd to hear from one of you.

Thanks
SAjit
Mar 26 '08 #1
7 1178
mshmyob
904 Expert 512MB
I think I am missing something here. If it is the PK then duplicates are not alowed and you will therfore not be able to save it when you try and a Access message will popup anyways. You do not have to do any checking for duplicate.

You may want (and I would) just create an ON ERROR event to trap the error and display your own message.

cheers,

Hi Friends,

I have a table called Beneficiary where [Solar Lantern No] is the primary key. The user enter the [Solar Lantern No] number. Since the user enters the [SLNO], there is always a possibility that a duplicate value is entered. After entering the data, when the user saves the record, it should check whether the particular SLNo is already entered or not. if yes, then it should display a message box, else record saved.

By default there is a system generated message box.

looking fwd to hear from one of you.

Thanks
SAjit
Mar 26 '08 #2
sajitk
77
Thanks mshmyob,

Would be great if you could help me in writing this On ERROR Event..

Thanks
Sajit

I think I am missing something here. If it is the PK then duplicates are not alowed and you will therfore not be able to save it when you try and a Access message will popup anyways. You do not have to do any checking for duplicate.

You may want (and I would) just create an ON ERROR event to trap the error and display your own message.

cheers,
Mar 26 '08 #3
mshmyob
904 Expert 512MB
ON ERROR events are pretty straight forward.

In your form in the On Error event you might put some code that looks like this:

Expand|Select|Wrap|Line Numbers
  1. 'If an error occurs because of missing data in a required field
  2. 'display our own custom error message
  3.  
  4.     ' Display error information.
  5.         errCase = DataErr
  6.  
  7.         Select Case errCase
  8.         Case 3314 ' missing required field value
  9.             LResponse = MsgBox("SSN required", vbOKOnly, errCase)
  10.             Response = acDataErrContinue
  11.         Case 0
  12.             'do nothing all is well
  13.         Case 3401
  14.             LResponse = MsgBox("SSN required", vbOKOnly, errCase)
  15.             Response = acDataErrContinue
  16. Case 3058 ' index key blank
  17.             LResponse = MsgBox("SSN required", vbOKOnly, errCase)
  18.             Response = acDataErrContinue
  19.         Case 3022 ' duplicate index key
  20.             LResponse = MsgBox("SSN already exists.", vbOKOnly, errCase)
  21.             Response = acDataErrContinue
  22.         Case Else
  23.             MsgBox "Error number " & Err.Number & ": " & Err.Description
  24.             Response = acdatadisplay
  25.         End Select
  26.  
Any erros you haven't trapped in any code will 'jump' down to here. For instance in your case if a person puts in an existing SSN into the PK the CASE 3022 will be triggered (error # 3022 is duplicate PK) and it will indicate that in a nice error message.

Notice error #3314 and 3401 will display the same message because some error numbers get triggered depending on how your design is.

Obviously you can build on this.

This is only one way. You could also create private onerror trapping. This way works quite nice for errors you don't think of ahead of time.

cheers,

Thanks mshmyob,

Would be great if you could help me in writing this On ERROR Event..

Thanks
Sajit
Mar 26 '08 #4
sajitk
77
Expand|Select|Wrap|Line Numbers
  1. thanks mshmyob, 
  2.  
  3. this works. 
  4.  
  5. I was just thinking if there is a way, the moment the user enters the SSN or solar Lantern no and moves for the next entry, the system should check whether that particular SSN or Solar Lantern no is there in the table or not. if yes, it should prompt a msgbox and the focus should shift back to the SSN textbox. 
  6.  
  7. would be great if you can help me in coding this also. 
  8. Thanks
  9. Sajit 
ON ERROR events are pretty straight forward.

In your form in the On Error event you might put some code that looks like this:

Expand|Select|Wrap|Line Numbers
  1. 'If an error occurs because of missing data in a required field
  2. 'display our own custom error message
  3.  
  4.     ' Display error information.
  5.         errCase = DataErr
  6.  
  7.         Select Case errCase
  8.         Case 3314 ' missing required field value
  9.             LResponse = MsgBox("SSN required", vbOKOnly, errCase)
  10.             Response = acDataErrContinue
  11.         Case 0
  12.             'do nothing all is well
  13.         Case 3401
  14.             LResponse = MsgBox("SSN required", vbOKOnly, errCase)
  15.             Response = acDataErrContinue
  16. Case 3058 ' index key blank
  17.             LResponse = MsgBox("SSN required", vbOKOnly, errCase)
  18.             Response = acDataErrContinue
  19.         Case 3022 ' duplicate index key
  20.             LResponse = MsgBox("SSN already exists.", vbOKOnly, errCase)
  21.             Response = acDataErrContinue
  22.         Case Else
  23.             MsgBox "Error number " & Err.Number & ": " & Err.Description
  24.             Response = acdatadisplay
  25.         End Select
  26.  
Any erros you haven't trapped in any code will 'jump' down to here. For instance in your case if a person puts in an existing SSN into the PK the CASE 3022 will be triggered (error # 3022 is duplicate PK) and it will indicate that in a nice error message.

Notice error #3314 and 3401 will display the same message because some error numbers get triggered depending on how your design is.

Obviously you can build on this.

This is only one way. You could also create private onerror trapping. This way works quite nice for errors you don't think of ahead of time.

cheers,
Mar 27 '08 #5
mshmyob
904 Expert 512MB
Yep you can do that also.

Put the following code in the 'After Update' event of the text box you want to check.

Expand|Select|Wrap|Line Numbers
  1. ' this will receive a TRUE or FALSE depending if the entry is found
  2. Dim vCheckEntry As Variant
  3.  
  4. ' use DLOOKUP method to look for your SSN    
  5. vCheckEntry = DLookup("[SSN_ID]", "tblSSN", "[SSN_ID] = '" & Me.txtSSN & "'")
  6.         If vCheckEntry = True Then
  7.             MsgBox ("SSN already exists.  Please change it.")
  8.             Me.txtSSN.SetFocus
  9.         Else
  10.             ' everyhting OK - do nothing
  11.         End If
  12.  
By the way don't put your message text in '[code]' brackets. It is hard to read.

cheers,

Expand|Select|Wrap|Line Numbers
  1. thanks mshmyob, 
  2.  
  3. this works. 
  4.  
  5. I was just thinking if there is a way, the moment the user enters the SSN or solar Lantern no and moves for the next entry, the system should check whether that particular SSN or Solar Lantern no is there in the table or not. if yes, it should prompt a msgbox and the focus should shift back to the SSN textbox. 
  6.  
  7. would be great if you can help me in coding this also. 
  8. Thanks
  9. Sajit 
Mar 27 '08 #6
sajitk
77
Thank you...

it works... the only change which i need to do was to change the variable type from Variant to Boolean.

Thanks once again

Sajit

Yep you can do that also.

Put the following code in the 'After Update' event of the text box you want to check.

Expand|Select|Wrap|Line Numbers
  1. ' this will receive a TRUE or FALSE depending if the entry is found
  2. Dim vCheckEntry As Variant
  3.  
  4. ' use DLOOKUP method to look for your SSN    
  5. vCheckEntry = DLookup("[SSN_ID]", "tblSSN", "[SSN_ID] = '" & Me.txtSSN & "'")
  6.         If vCheckEntry = True Then
  7.             MsgBox ("SSN already exists.  Please change it.")
  8.             Me.txtSSN.SetFocus
  9.         Else
  10.             ' everyhting OK - do nothing
  11.         End If
  12.  
By the way don't put your message text in '[code]' brackets. It is hard to read.

cheers,
Mar 28 '08 #7
sajitk
77
Which part of the world are you from.....??? Myself from India....

Sajit

Thank you...

it works... the only change which i need to do was to change the variable type from Variant to Boolean.

Thanks once again

Sajit
Mar 28 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: John Simmons | last post by:
How is it that even though I have the column "username" in my database set as a Primary key, using my PHP script to add new users to the database works without any errors even when signing up using...
5
by: Ghulam Farid | last post by:
Hi i have a table with primary key defined on col1 and col2. now i want to have col3 also included in primary key. when i alter the table it gives me error for duplicate rows. there is an option...
4
by: Mavis Tilden | last post by:
Hi all, So I've been reading the newsgroups, and reading a few books trying to learn SQL and SQL Server 2000. The books tell me I need a Primary Key, and that every table should have one. I know...
9
by: 101 | last post by:
Taking a course on SQL. They are saying you can get better performance by having multiple files for a group. They then graphically show an example of "Primary" with multiple data files. I have...
4
by: serge | last post by:
I ran into a table that is used a lot. Well less than 100,000 records. Maybe not a lot of records but i believe this table is used often. The table has 26 fields, 9 indexes but no Primary Key at...
5
by: shenanwei | last post by:
I have a primary server and backup server located in different physical sites. The primary server is live and ship logs to backup site every 5 minutes. The primary server is being full online...
4
by: misscrf | last post by:
Ok I have 2 issues. 1) I have a main candidate form with 2 subforms on a tab control: http://www.geocities.com/misscrf/images/contactcontinouscheckbox.jpg I have been encouraged to add these...
18
by: Thomas A. Anderson | last post by:
I am a bit confused in creating a composite primary key. I have three table with two of the tables containing primary keys. I have two of the tables (each with a primary key) having one to many...
8
by: Challenge | last post by:
Hi, I got error, SQL1768N Unable to start HADR. Reason code = "7", when I tried to start hadr primary database. Here are the hadr configuration of my primary db: HADR database role ...
4
by: Peter | last post by:
I am interested in informed feedback on the use of Constraints, Primary Keys and Unique. The following SQL statement creates a Bands tables for a database of bookings Bands into Venues, where the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.