473,407 Members | 2,359 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,407 software developers and data experts.

Primary Key conflict in form entry

6
Hi --

I have a main form attached to a table (Invoice header info). When the form loads I use acNewRec to take us to the end of a table, and I get a clean, empty form.

The user, who knows their invoice number, enters it into the first textbox on the form. If there's no match on a separate table (a cross referencing process takes place before this) they get a message that the number wasn't found and they get taken back to rest quietly in the textbox. This is what I want to have happen--they can't go any further.

However, if there is a match with that cross reference table, I want the form to either pull up (this is key) a partially completed invoice header that they they forgot when they went out to lunch, or, if no partially completed invoice header exists, I want them to truck onward and enter it.

Instead, I get a 3022 error saying my action would create duplicates in the primary key and the record cannot be saved. This (I'm guessing) is because of the AcNewRec I invoked in the first place.

This is the query that causes the 3022 error:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT [qEnterHeader].[NewInvoice] FROM qEnterHeader WHERE (((tHeader.NewInvoice)=" & [Forms]![fdataentry]![NewInv] & ")) ;"
  2. Me.RecordSource = strSQL
  3.  
Can someone help sort this out? I would appreciate it.

Thank you.
Mar 27 '07 #1
6 2029
Rosie
6
Dear someone,

Please take pity on me.

Thank you,
Rosie
Mar 28 '07 #2
Rabbit
12,516 Expert Mod 8TB
Hi --

I have a main form attached to a table (Invoice header info). When the form loads I use acNewRec to take us to the end of a table, and I get a clean, empty form.

The user, who knows their invoice number, enters it into the first textbox on the form. If there's no match on a separate table (a cross referencing process takes place before this) they get a message that the number wasn't found and they get taken back to rest quietly in the textbox. This is what I want to have happen--they can't go any further.

However, if there is a match with that cross reference table, I want the form to either pull up (this is key) a partially completed invoice header that they they forgot when they went out to lunch, or, if no partially completed invoice header exists, I want them to truck onward and enter it.

Instead, I get a 3022 error saying my action would create duplicates in the primary key and the record cannot be saved. This (I'm guessing) is because of the AcNewRec I invoked in the first place.

This is the query that causes the 3022 error:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT [qEnterHeader].[NewInvoice] FROM qEnterHeader WHERE (((tHeader.NewInvoice)=" & [Forms]![fdataentry]![NewInv] & ")) ;"
  2. Me.RecordSource = strSQL
  3.  
Can someone help sort this out? I would appreciate it.

Thank you.
I don't understand what you need that code for.
Mar 28 '07 #3
Rosie
6
To attach a recordsource to the form. I've tried a variety of things before and after posting -- that particular scenario was the last I'd tried before posting. Most of the things I've tried are apparently the result of some misguided fantasy I'm having about the relationship between the recordsource and the form, but I don't know what I don't know. If you know what I mean.

This is where I am now: I stopped using anything at the form load event. I have invoices in a database with details table, a header table, and an xref table.

If a user comes to the main form (tied to a query that selects all the records in the header table), and enters an invoice number not on the xref table, they are not allowed to proceed. That works fine.

If the user comes to the main form, and enters an invoice number on the xref table, they can again proceed. That works fine.

If the user comes to the main form and enters an invoice number that is already on the header table, I want the form to display that invoice for editing.

Really, all I want is to know the procedure to pull up a unique recordset for editing.

I'm sorry if I'm incoherent -- I've spent too much time on this now and I can tell I'm not dealing with it rationally. I'm just flailing around. Here's my most recent catastrophe:

Expand|Select|Wrap|Line Numbers
  1.  
  2. 'IS THERE A HEADER?
  3. 'here's where we determine whether the invoice they've entered is already on the table. 
  4. strSQL = "SELECT theader.newinv " & _
  5. "FROM theader " & _
  6. "WHERE (((theader.NewInv)=" & [Forms]![fdataentry]![NewInv] & "));"
  7. rs.Open strSQL, Conn, adOpenKeyset, adLockOptimistic, adCmdTableDirect
  8. If rs.BOF And rs.EOF Then
  9.     MsgBox "No header exists"
  10. Else
  11. 'if a header does exist, I want to bring that record up to be displayed for editing
  12. 'on the form. Instead I get an error because it clearly thinks I'm trying to create 
  13. 'a new record. I'm not. I'm trying to get to the existing record. 
  14.     MsgBox "a header exists; let's do the following"
  15.     Me.RecordSource = strSQL
  16. End If
  17.  
Mar 28 '07 #4
Rabbit
12,516 Expert Mod 8TB
I assume invoice number is the primary key?

The problem is that you're using the bound control to determine if there's an existing record. Whatever you type into that control gets saved, so when you type an existing invoice number, it will throw up an error because it is trying to save an existing key.

Try doing it from the Before Update event of the control. If it matches an existing primary key, set cancel to true to stop the update and then go to the existing record.
Mar 28 '07 #5
Rosie
6
Okay. I've tried this (and thank you) and at least it doesn't cough an error back in my face, so it's definitely an improvement. I don't actually know how to go to the record, though. I don't know the syntax to use to perform that function -- how to propel us to that record on the form. Can you tell me what the statement should look like?

For what it's worth, this is all of the code attached now to the textbox. It contains fossils of my previous failures.

Expand|Select|Wrap|Line Numbers
  1.  
  2. Private Sub NewInv_BeforeUpdate(Cancel As Integer)
  3. 'FIRST, MAKE SURE THE INVOICE EXISTS:
  4. Dim strSQL As String
  5. Me.lstOldInvoices.Requery
  6. strSQL = "SELECT tInvXref.OldInvoice " & _
  7. "FROM tInvXref " & _
  8. "WHERE (((tInvXref.NewInvoice)=" & [Forms]![fdataentry]![NewInv] & "));"
  9.  
  10. Dim Conn As ADODB.Connection
  11. Dim rs As New ADODB.Recordset
  12. Set Conn = CurrentProject.Connection
  13. rs.Open strSQL, Conn, adOpenKeyset, adLockOptimistic, adCmdTableDirect
  14.  
  15. If rs.BOF And rs.EOF Then
  16.     MsgBox NewInv.Text & " not found on Xref table." & _
  17.     vbCr & "You need to allow the system to generate a new invoice first.", vbOKOnly, "Invoice Number not found."
  18.     DoCmd.SetWarnings False
  19.     DoCmd.DoMenuItem acFormBar, acEditMenu, 8, , acMenuVer70
  20.     DoCmd.DoMenuItem acFormBar, acEditMenu, 6, , acMenuVer70
  21.     DoCmd.SetWarnings True
  22.     Exit Sub
  23. End If
  24. rs.Close
  25.  
  26. 'IS THERE A HEADER?
  27. strSQL = "SELECT theader.newinv " & _
  28. "FROM theader " & _
  29. "WHERE (((theader.NewInv)=" & [Forms]![fdataentry]![NewInv] & "));"
  30. rs.Open strSQL, Conn, adOpenKeyset, adLockOptimistic, adCmdTableDirect
  31. If rs.BOF And rs.EOF Then
  32.     MsgBox "No header exists"
  33. Else
  34.     MsgBox "a header exists; let's do the following"
  35.     Cancel = True
  36.     'Me.RecordSource = strSQL
  37. End If
  38.  
  39. rs.Close
  40. Set rs = Nothing
  41. Set Conn = Nothing
  42. End
  43.  
  44.  
  45. Err_handler:
  46.     Select Case Err.Number
  47.         Case "3022"
  48.             DoCmd.SetWarnings False
  49.                 MsgBox "A header record already exists for this invoice."
  50.                 Unload Me
  51.                 DoCmd.SetWarnings False
  52.                 Exit Sub
  53.         Case Else
  54.             MsgBox "Error in AllowDataEntry module. Report: " & Err.Number & " " & Err.Description & "."
  55.     End Select
  56.  
  57.  
Mar 28 '07 #6
Rabbit
12,516 Expert Mod 8TB
You can use DoCmd.FindRecord. You can get all the syntax and info you need from the microsoft visual basic editor help files.
Mar 28 '07 #7

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

Similar topics

14
by: Abhi | last post by:
FYI: This message is for the benefit of MS Access Community. I found that this prblem has been encounterd by many but there is hardly any place where a complete solution is posted. So I thought...
2
by: ken | last post by:
When you have a text primary key thus no duplicates and through a form you enter a duplicate value into the primary key...is there anyway through events to tell the user that they entered a...
6
by: nasirmajor | last post by:
please any urgent response. Dear alls; (database relevent quetion) i want to display the next availible primery key {e.g 1,2,3 (autonumbered) next 4} on the user entry form in "label"...
12
by: Aidan | last post by:
I have a form to create a new record for a training course. The form is based on one table that has 4 keys set to primary key. The first combo box on the form allows selection of the course POP...
4
by: hlebforprimeminister | last post by:
Hi I am having trouble with some forms i am creating for a database. I have a data entry form at the moment that enters information into one table and a subform within the form that enters...
14
lotus18
by: lotus18 | last post by:
Hello all I have these records on my Day Table for my complete database table please click here 1. M 2. T 3. W 4. TH 5. F 6. S
8
by: stuchdk | last post by:
background: I have created a series of data entry forms for use of data collection. Each has a table as a record source with the same primary key "survey_id", which is a text field. this is...
2
jmoudy77
by: jmoudy77 | last post by:
Hi, I've got a database with front-ends at multiple locations that pull from a primary back-end over a satellite network. Each time a front-end closes it triggers a back-up utility that creates a...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.