Results 1 to 12 of 12
  1. #1
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100

    Key Violations

    I have two tables to append to. The first table (RebootScheduleLogicalServer) consists of 2 fields (Schema shown below). The second table has several fields(Schema below). I must insert a row into the first table and then a row into the second table using the RebootScheduleID from the first table. When I run the append query (shown below) i get a key violation error. I am also showing another append query that I know works to indicate the syntax is correct.
    Click image for larger version. 

Name:	table1.PNG 
Views:	26 
Size:	24.2 KB 
ID:	38295Click image for larger version. 

Name:	table2.PNG 
Views:	26 
Size:	30.8 KB 
ID:	38297Click image for larger version. 

Name:	quetry1.PNG 
Views:	24 
Size:	13.2 KB 
ID:	38298

    Click image for larger version. 

Name:	Query2.PNG 
Views:	24 
Size:	13.6 KB 
ID:	38299


    The LogicalServerId for the RebootScheduleLogicalServer is provided by the rowsource where I enter the Server Name.

    How can I determine which of the "Values" in the first query is causing the key violation and can you indicate how to correct the problem?



    I have tried the functions StringfromGUID and GUIDFromString as follows:

    Code:
        ''' Save the new reboot ID to the RebootScheduleLogicalServer table first
        vLogID = StringfromGUID(Me.cboSvrNm.Column(0))
        
        DoCmd.RunSQL "INSERT INTO RebootScheduleLogicalServer ([RebootScheduleID], [LogicalServerID]) " & _
                 "VALUES ('NewID()', '" & vLogID & "')"
    AND:

    Code:
        ''' Save the new reboot ID to the RebootScheduleLogicalServer table first
        vLogID = Me.cboSvrNm.Column(0)
        
        DoCmd.RunSQL "INSERT INTO RebootScheduleLogicalServer ([RebootScheduleID], [LogicalServerID]) " & _
                 "VALUES ('NewID()', '" & GUIDFromString(vLogID) & "')"
    Neither function works, both result in key violations.
    Attached Thumbnails Attached Thumbnails table1_data.PNG  
    Last edited by shylock; 05-05-2019 at 01:38 PM. Reason: Ad GUID Functions

  2. #2
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    you are using replication ID's as your number type - they are used for synchronisation and should not be used as primary keys (at least within a JET/ACE backend). Instead, use long integer. If you need synchronisation, include a replicationID as a separate column (and set the primary key new value to random), you will also need a number of date fields as well to determine the latest version and identify clashes.

  3. #3
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Quote Originally Posted by Ajax View Post
    you are using replication ID's as your number type - they are used for synchronisation and should not be used as primary keys (at least within a JET/ACE backend).
    I've seen a few examples GUID's being used as server ID's in this forum lately so I figured they ought to be OK for some types of data. Not sure why you say they shouldn't be used as PKs? I cannot think of a case where I would use them as PK values because they use up a lot of resources, but they are guaranteed to be unique so what would be the issue? In fact, in a replicated db, they are primary keys.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #4
    shylock is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    The tables are SQLServer tables. I am not permitted to alter the table schemas. I am using Access as the front end. What I don't understand is why one append query (the second query) works and the first query throws the key violation.

  5. #5
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    All I can suggest is that you examine the values of variables before executing code that attempts to run a query. That could be just mousing over the variables, or outputting the sql to the immediate window (debug.print) or using a watch window. The message is indicating that you're attempting to violate the constraints around the field values needing to be unique OR needing to be provided (e.g. you are trying to append Null). It also looks like you've got a composite pk field - maybe you don't realize you already have whatever value(s) you're attempting to enter. As for one query vs the other, I don't see how you can compare those 2 queries that you're calling 1 and 2 that are in the images.

    In the end, if you're saying you can run query 1 but not query 2 after that, AND you can run query 2 first but if you do, you cannot run query 1, then it has to be that either query is attempting to duplicate values in fields where it isn't permitted OR you are trying to append/update values that are not permitted (such as Null or empty string) - that's what the message is trying to tell you. Verify whatever it is that you're attempting to write to the table before it attempts to execute.

  6. #6
    shylock is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    Thanks, Micron. I am pretty sure of the values that I am trying to insert. I moused over and used debug,print, both, and they showed the expected values. The second query should have read as follows (See NewID() in red:

    Code:
    DoCmd.RunSQL "INSERT INTO RebootSchedule ([RebootScheduleID], [RebootWeek], [RebootTime], [Mon], [Tue], [Wed], [Thu], [Fri], [Sat], [Sun], [IsTemp], " & _
                 "[RequireSpecialInstructions], [RequireEveningReboot]) " & _
                 "VALUES (' NewID()  ', '" & sWk & "', '" & TimeValue(Me.txtTime) & "', " & Me.chkMon & ", " & Me.chkTue & ", " & Me.chkWed & ", " & Me.chkThu & ", " & Me.chkFri & ", " & _
                 "" & Me.chkSat & ", " & Me.chkSun & ", " & Me.chkTemp & ", " & Me.ChkSpecial & ", " & Me.chkPM & ")"
    This query works and give me a new RebootScheduleID. My concern is that, logically the new RebootScheduleID should be created in the RebootScheduleLocalServer table first as it is the link between the LogicalServer table and the RebootSchedule table as in the following query:

    Code:
    SELECT LogicalServer.Name, RebootSchedule.RebootWeek, TimeValue(RebootSchedule.RebootTime) AS BootTime, RebootSchedule.Mon, RebootSchedule.Tue, RebootSchedule.Wed, RebootSchedule.Thu, RebootSchedule.Fri, RebootSchedule.Sat, RebootSchedule.Sun, RebootSchedule.IsTemp, RebootSchedule.RequireSpecialInstructions, RebootSchedule.RequireEveningReboot, IsNull([LogicalServer].[DecommissionDate]) AS Expr1, RebootScheduleLogicalServer.RebootScheduleID
    FROM (LogicalServer INNER JOIN RebootScheduleLogicalServer ON LogicalServer.LogicalServerID = RebootScheduleLogicalServer.LogicalServerID) INNER JOIN RebootSchedule ON RebootScheduleLogicalServer.RebootScheduleID = RebootSchedule.RebootScheduleID
    WHERE (((IsNull([LogicalServer].[DecommissionDate]))<>False))
    ORDER BY LogicalServer.Name;
    What do you think?

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    thought I had responded, but can't see it so here it is again.
    In fact, in a replicated db, they are primary keys.
    no - they ar not. I used to support a number of replicated apps (user out in field, syncs on return to office) and experimented with using replication ID's as PK's but hit a variety of issues on joins. Using them how MS used to use them when they provided the replication facility works well - i.e as per my previous post

    @shylock, if your BE is sql server and using replication ID as PK, then suggest use a passthough query or stored procedure so the processing takes place in the BE

  8. #8
    shylock is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    Ajax:

    Sorry, what is BE?

  9. #9
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    BE=Back End - where your tables are stored
    FE=Front End - where you have forms, reports, queries, code - effectively the application

  10. #10
    shylock is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    Thank you Ajax. I once worked for a company that published a 25 page report of acronyms. Many of them were the identical but had different meanings. Very confusing.
    Ever since, I have tried to avoid acronyms.

    I will try a pass through and/or stored procedure.

  11. #11
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,372
    Quote Originally Posted by Ajax View Post
    no - they ar not. I used to support a number of replicated apps (user out in field, syncs on return to office) and experimented with using replication ID's as PK's but hit a variety of issues on joins. Using them how MS used to use them when they provided the replication facility works well - i.e as per my previous post
    I don't profess to be an expert on the subject as I've never worked on a replication db until I was asked to do so recently and I had to read up on it. It's possible that they are experiencing mis-matching of ID's (if that's what you're saying) as they've asked me to look at that now.

    Here's a couple of images (had to blur for confidentiality). If you can educate me on why we seem to have that difference of opinion, I'd appreciate learning more about it. What I wrote was based on what I see in this db:

    You should be able to tell that darkened field is pk, autonumber, replication id
    Click image for larger version. 

Name:	RepTbl1b.jpg 
Views:	11 
Size:	90.9 KB 
ID:	38304

    That same field (selected row) is fk in more than one table and is type replication id. It is also joined in relationships.
    Click image for larger version. 

Name:	RepTbl2b.jpg 
Views:	11 
Size:	89.6 KB 
ID:	38305
    If I need to move this to a new thread please let me know as I don't want to hijack this one (assuming I haven't already).

  12. #12
    shylock is offline Competent Performer
    Windows 7 64bit Access 2016
    Join Date
    Sep 2018
    Location
    Dayton, Ohio
    Posts
    100
    Micron:

    Please do not move this discussion to a new thread. I find this very educational and apropos to the project I am currently working on. Just about all of the tables I have to use have replication ID's (GUID's) as PK's and FK's. It does make things interesting, to say the least.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Key violations or some crap
    By Sarge, USMC in forum Access
    Replies: 5
    Last Post: 02-06-2019, 04:12 PM
  2. Append Key Violations
    By Chad Access in forum Access
    Replies: 4
    Last Post: 04-27-2017, 03:32 PM
  3. key violations
    By Jen0dorf in forum Access
    Replies: 5
    Last Post: 08-10-2016, 12:20 PM
  4. Key Violations
    By Daryl2106 in forum Access
    Replies: 5
    Last Post: 10-02-2012, 02:34 PM
  5. Database help, key violations
    By dhicks19 in forum Queries
    Replies: 6
    Last Post: 05-10-2012, 05:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums