Results 1 to 14 of 14
  1. #1
    alexavier is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Dec 2013
    Posts
    7

    Combine two field values in a separate field to create a Primary Key

    Hi,

    I know this question has been asked before, but I'm really confused.

    I've followed instruction from both https://www.accessforums.net/access/...eld-14038.html and https://www.accessforums.net/access/...2-a-10960.html tried using the me.PID=me.P_FullName&me.P_State in After Update in my form for both P_FullName and P_State since either of those being updated would affect PID.

    But when I run the form in form view it says "Microsoft Access cannot find the object 'me.' " - I'm utterly lost now..

    Please help.



    I'm using MS Office Access 2013

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Where do you have this code?

  3. #3
    alexavier is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Dec 2013
    Posts
    7
    Sorry, I don't think I understand your question. I use the buttons on access really and try my best to not tinker with the code unless I really have to (I use google then..) I'm quite new to Access actually, started the day before yesterday!

  4. #4
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Do you have this in the AfterUpdate event of a Form? If not, where is it?

  5. #5
    alexavier is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Dec 2013
    Posts
    7
    Oh yes - it is in the AfterUpdate event of P_FullName and P_State - which are both fields in the form.

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Quote Originally Posted by alexavier View Post

    ...it is in the AfterUpdate event of P_FullName and P_State...
    But is it in the Code Module for the Form? It should be and look like this

    Code:
    Private Sub P_FullName_AfterUpdate()
     Me.PID= Me.P_FullName & Me.P_State 
    End Sub
    
    Private Sub P_State_AfterUpdate()
     Me.PID= Me.P_FullName & Me.P_State
    End Sub

    Linq ;0)>

  7. #7
    alexavier is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Dec 2013
    Posts
    7
    It's in the 'Property Sheet' of the field.. How do i check if it is in the code module?

  8. #8
    alexavier is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Dec 2013
    Posts
    7
    Thank you very much for replying!

  9. #9
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    While viewing the property sheet for the form, select either control and press the "..." button. What happens?

  10. #10
    alexavier is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Dec 2013
    Posts
    7
    Oh my god! I just got it happening - so I had to go to the code module and paste the code which missinglinq posted nice! Thank you so very much!

  11. #11
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    Glad Linq and I could help. Merry Christmas.

  12. #12
    alexavier is offline Novice
    Windows 7 32bit Access 2013
    Join Date
    Dec 2013
    Posts
    7
    I should also tell you - we are doing this as part of a massive research into the condition of prisons in India - so this really goes a long way! Thank you very much!

  13. #13
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    As Allan said, we're glad we could help!

    Given the reported treatment of domestic servants, in India, I cannot imagine how prisoners must be treated!

    Now that you've got this working, I should warn you that using a Primary Key that is composed of a full name plus a state name could be fraught with danger! PKs must, by definition, be unique. If, by 'state,' you are speaking of states, as in 'Assam,' 'Bihar,' etc., I would think that a given 'state' would not be unique to a single Record, but would be a valid Value for multiple Records. And if P_FullName is a person's full name, you should know that no combination of first, middle and last names is ever truly unique to one person, no matter how strange, odd or curious that name is! Therefore the combination of P_Fullname & P_State is not guaranteed to be unique.

    If the above assumptions about what P_State and P_Fullname represent are correct, you really need to add a third Field to this calculation, such as the Date and Time the Record is created. One way would be to use the Now() function, added to P_Fullname and P_State, to create an almost assuredly unique PK.

    Code:
    Private Sub P_FullName_AfterUpdate()
     Me.PID= Me.P_FullName & Me.P_State & Now() 
    End Sub
    
    Private Sub P_State_AfterUpdate()
     Me.PID= Me.P_FullName & Me.P_State & Now()
    End Sub

    Good luck with your project!

    Linq ;0)>

  14. #14
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    @Alexavier:
    We should also point out a couple of things. 1) AutoNumbers are still the best PrimaryKey (PK) for a table. 2) You can create a unique index on those fields if you want without making them the PK.

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

Similar Threads

  1. Create a paragraph using Field Values
    By imran688 in forum Access
    Replies: 12
    Last Post: 10-31-2012, 12:04 PM
  2. Replies: 20
    Last Post: 09-12-2012, 06:52 PM
  3. Replies: 1
    Last Post: 05-09-2012, 02:22 PM
  4. Replies: 6
    Last Post: 06-05-2011, 09:30 AM
  5. Replies: 1
    Last Post: 09-29-2010, 08:01 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