Results 1 to 9 of 9
  1. #1
    LanguidAnomie is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Mar 2012
    Posts
    12

    Problem using macro to generate unique ID in primary key field

    Hi,



    I am creating a relational database for my A-level ICT coursework (this is the first time I've used database software) and am encountering a bit of trouble when trying to generate a unique ID in the primary key field of one of my forms using a macro.

    I wrote a macro that takes the first character of the 'Comments' field, the last three characters of the 'CustomerID' field and the number of days that have elapsed since 01/10/2011 and strings them together to make a unique ID and put it into the primary key field called 'JobID'. The problem is that the macro, which I've set to run 'after update' of the 'Comments' field, doesn't run automatically, but as soon as I click on the JobID field, the macro runs just fine. There's nothing I can see that's telling the macro to run when I click on the field.

    The macro details are as follows:

    Action
    SetValue

    Arguments
    [Forms]![FrmJob]![JobID], Left([Forms]![FrmJob]![Comments],1) & Right([Forms]![FrmJob]![CustomerID],3) & DateDiff("d",#01/10/2011#,Date())

    Any idea how I can fix this? I have other macros that generate unique IDs in other forms that work just fine.

    Thanks.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Why do those other forms work? What is their setup? Is anything different from this form? If the macro actually is referenced in the AfterUpdate event property of Comments field, it should work. Sounds like the macro is associated with Comments LostFocus or Exit event or an event of JobID (GotFocus, Click, Enter).

    Would have to analyse project if you want to provide it. I only use VBA but I can usually figure out macros.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    LanguidAnomie is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by June7 View Post
    Why do those other forms work? What is their setup? Is anything different from this form? If the macro actually is referenced in the AfterUpdate event property of Comments field, it should work. Sounds like the macro is associated with Comments LostFocus or Exit event or an event of JobID (GotFocus, Click, Enter).

    Would have to analyse project if you want to provide it. I only use VBA but I can usually figure out macros.
    I'd be happy to provide you with my project. I tried to attach it to this message but it exceeds the size limit. Is there some other way I can get it to you? PM it or something?

    Thanks.

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    Did you run Compact & Repair first? Zip it? Still too big? Can upload to fileshare site such as box.com and post link to the file.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    LanguidAnomie is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by June7 View Post
    Did you run Compact & Repair first? Zip it? Still too big? Can upload to fileshare site such as box.com and post link to the file.
    Archived the file as .zip.

    Attached Files Attached Files

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    I should have said this in my first post but I was little slow nailing down what bothered me about the issue. I would never construct unique IDs this way. For one thing, they are dependent calculations. Not only is there a requirement for a comment but the comment can never be edited, at least not the first character. And what if customer changes contact number? Editing these data through the forms will trigger the macros. Record associations will be royally wrecked.

    Why did you take this approach?

    Would be better off using the autonumber type as unique IDs.

    You should set the JobID textbox as Locked Yes and TabStop No. TabOrder is bypassing the CustomerID combobox.

    I tried the macro in another control's AfterUpdate event and still the same issue. Something about that form. Maybe rebuild it. Creat new form and copy/paste the controls. See if behavior changes.
    Last edited by June7; 03-15-2012 at 03:59 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    LanguidAnomie is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by June7 View Post
    I would never construct unique IDs this way. For one thing, they are dependent calculations. Not only is there a requirement for a comment but the comment can never be edited, at least not the first character. And what if customer changes contact number? Editing these data through the forms will trigger the macros. Record associations will be royally wrecked.

    Why did you take this approach?

    Would be better off using the autonumber type as unique IDs.

    You should set the JobID textbox as Locked Yes and TabStop No. TabOrder is bypassing the CustomerID combobox.

    I tried the macro in another control's AfterUpdate event and still the same issue. Something about that form. Maybe rebuild it. Creat new form and copy/paste the controls. See if behavior changes.
    The only reason I did it like that was because the guide I was using told me to, but you've convinced me to change it to an autonumber.

    Thanks for all the help. Really appreciate it.

    Last edited by LanguidAnomie; 03-16-2012 at 12:54 PM.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,921
    I am shocked that any reference would recommend such a schema. There could be a way to generate ID from those components and have it remain unchanged. The event would have to include conditional statement like If IsNull(ID) Then create ID. However, the id components are still dependent on other data input for their structure. If no comment or phone contact, id will not conform to the structure and if code was in AfterUpdate event, it wouldn't even get built. Still bad design. Glad you chose the good Force.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    LanguidAnomie is offline Novice
    Windows 7 32bit Access 2007
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by June7 View Post
    I am shocked that any reference would recommend such a schema. There could be a way to generate ID from those components and have it remain unchanged. The event would have to include conditional statement like If IsNull(ID) Then create ID. However, the id components are still dependent on other data input for their structure. If no comment or phone contact, id will not conform to the structure and if code was in AfterUpdate event, it wouldn't even get built. Still bad design. Glad you chose the good Force.
    I've come to a new problem now. If you'd be willing to take a look at it then I'd be eternally grateful.

    Here's the link to it:

    https://www.accessforums.net/showthr...330#post110330

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

Similar Threads

  1. Exception in unique field
    By neo651 in forum Access
    Replies: 2
    Last Post: 07-01-2011, 02:23 PM
  2. Problem with primary key on imported database
    By 1953hogan in forum Access
    Replies: 1
    Last Post: 06-23-2011, 05:02 PM
  3. Generate Multiple Queries per Field value
    By dssrun in forum Programming
    Replies: 1
    Last Post: 02-26-2011, 12:12 PM
  4. Unique ID problem
    By Remster in forum Access
    Replies: 10
    Last Post: 10-12-2010, 01:48 PM
  5. Primary Key change macro?
    By gracin in forum Access
    Replies: 0
    Last Post: 03-06-2009, 10:59 AM

Tags for this Thread

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