Solved Table key violation error

Gr3g0ry

Registered User.
Local time
Today, 00:06
Joined
Oct 12, 2017
Messages
163
1588218211974.png

this is my query:

DoCmd.RunSQL "INSERT INTO CUSTOMERDETAILS (CUSTOMERID, CUSTOMERNAME, CUSTOMERADDRESS, CONTACT, EMAIL, ORGANIZATIONTYPE, ORGANIZATIONID,STATUS) VALUES (" & Me.CUSTOMERID.Value & ", '" & Me.cusname.Value & "', '" & Me.cusaddress.Value & "', '" & Me.CONTACT.Value & "', '" & Me.EMAIL.Value & "', '" & Me.orgtype.Value & "', '" & Me.orgid.Value & "', " & stat & ");"

1588218333672.png

i have checked all my field names and they are correct. i have copied and pasted the field names just to be sure.
stat is an integer variable with the value of 1.

Please help.
 
Hi. Have you checked to make sure you're not creating a duplicate customer ID?
 
how about the Me.CustomerID, does it have same value on the table?
before running the query, test first if there is a Customer with same CustomerID:

If DCount("1", "CustomerDetails", "CustomerID = " & Nz(Me.CustomerID, -9999)) > 0 Then
'There is duplicate, show some message
Msgbox "Duplicate"

Else
'Run the query
DoCmd.RunSQL …
End If
 
how about the Me.CustomerID, does it have same value on the table?
before running the query, test first if there is a Customer with same CustomerID:

If DCount("1", "CustomerDetails", "CustomerID = " & Nz(Me.CustomerID, -9999)) > 0 Then
'There is duplicate, show some message
Msgbox "Duplicate"

Else
'Run the query
DoCmd.RunSQL …
End If


1588220435221.png

and i still get the same key violation error
 
The error message contains something I don't see very often, a combination of errors. It is telling you (I think) that your CustomerID field isn't the data type you think it is. If that is true then what has actually happened is that the data type doesn't convert correctly so is trying to enter an invalid value, probably zero. The other fields are of type text so it would be rare for that to fail on a type conversion. You told us what was in the status variable, but you didn't tell us what was in the CustomerID control.

Speaking of value, all of your query's .Value references can be omitted. For access controls, the rule is that if the control HAS a value, the default property is .Value so you don't need to name it. That should save you some typing.
 
The error message contains something I don't see very often, a combination of errors. It is telling you (I think) that your CustomerID field isn't the data type you think it is. If that is true then what has actually happened is that the data type doesn't convert correctly so is trying to enter an invalid value, probably zero. The other fields are of type text so it would be rare for that to fail on a type conversion. You told us what was in the status variable, but you didn't tell us what was in the CustomerID control.

Speaking of value, all of your query's .Value references can be omitted. For access controls, the rule is that if the control HAS a value, the default property is .Value so you don't need to name it. That should save you some typing.


so i took you advice with the .Value thing. however the error still persists even after i declared an integer variable and assigned CustomerID's value to it. CustomerId control is a text box in which an integer is entered.

1588229385666.png
 
maybe Cast it to Integer:

Dim cus as Integer
cus = CInt(Me.CustomerID & "")
f DCount("1", "CustomerDetails", "CustomerID = " & cus) > 0 Then
'There is duplicate, show some message
Msgbox "Duplicate"

Else
'Run the query
DoCmd.RunSQL …
End If

////Edit:
Integer — range from -32,768 to +32,767.
Long Integer — range from -2,147,483,648 to +2,147,483,647
 
Last edited:
maybe Cast it to Integer:

Dim cus as Integer
cus = CInt(Me.CustomerID & "")
f DCount("1", "CustomerDetails", "CustomerID = " & cus) > 0 Then
'There is duplicate, show some message
Msgbox "Duplicate"

Else
'Run the query
DoCmd.RunSQL …
End If

////Edit:
Integer — range from -32,768 to +32,767.
Long Integer — range from -2,147,483,648 to +2,147,483,647
1588254209872.png

did as you suggested. same error persists. i totally wanna know whats happeneing here. . thanks man
 
Next suggestion/question: Have you run a Compact&Repair on this database recently? This kind of behavior CAN indicate a damaged record. I've seen it before.
 
Next suggestion/question: Have you run a Compact&Repair on this database recently? This kind of behavior CAN indicate a damaged record. I've seen it before.
no i havent. how do i do this please ?
 
yes sir on numerous occasions, ive also made sure to enter only numbers
Hi. Can you post a sample copy of your db with test data? We may have to physically look at it to help you find out what's actually happening.
 
Next suggestion/question: Have you run a Compact&Repair on this database recently? This kind of behavior CAN indicate a damaged record. I've seen it before.

YUP. did the Compact & Repair. then closed the file, then reopened. same eroor persists. attached is the file im working with. the from is called CUSTOMER FORM
 

Attachments

YUP. did the Compact & Repair. then closed the file, then reopened. same eroor persists. attached is the file im working with. the from is called CUSTOMER FORM
Hi. Saw it. The explanation for the "key violation" error is because you have a 1-to-1 relationship between CUSTOMERDETAILS and RENT and RESERVATION tables. Which means, you cannot add a record into the CUSTOMERDETAILS table without a corresponding CUSTOMERID in the RENT and RESERVATION table. Hope that helps...
 
Hi. Saw it. The explanation for the "key violation" error is because you have a 1-to-1 relationship between CUSTOMERDETAILS and RENT and RESERVATION tables. Which means, you cannot add a record into the CUSTOMERDETAILS table without a corresponding CUSTOMERID in the RENT and RESERVATION table. Hope that helps...

WOW .... thanks a million man.
 
WOW .... thanks a million man.
You're very welcome. We're all happy to assist. Sometimes, it is easier/faster when we can see the issue first hand. Good luck with your project.
 

Users who are viewing this thread

Back
Top Bottom