Results 1 to 14 of 14
  1. #1
    harrytgs is offline Advanced Beginner
    Windows 7 32bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38

    Cool Make ID field a random number

    Hi,



    On an Access database, I would like an ID number to be generated randomly.

    This ID number needs to be within the range of 11000 - 99999.

    How is this possible?

    Many thanks

  2. #2
    Compufreak is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2012
    Posts
    70

  3. #3
    harrytgs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38
    Thanks - will look at now

  4. #4
    harrytgs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38
    How exactly would this work though? This database's sole duty will (at the beginning) be to generate random numbers and store them (perhaps when a box is ticked).

    These are unique numbers for our stock and we have gone up to 11000 so can't go under that, and each number needs to be unique ...

  5. #5
    harrytgs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38
    Anyone able to help ??

  6. #6
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,917
    What part of the suggested link did you not understand? It looks like a good solution for you.

  7. #7
    harrytgs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38
    I will look into this later - I am assuming I will just have to create a macro and then add the function on Access?

  8. #8
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    It's easy enough to generate a random integer in the range you specified:

    int(rnd*(99999-11000) + 11000) (or int(rnd*(88999) + 11000) ) will do it for you. Be sure to use type long; if 11000 is not an allowable value, you will need to check for the (rare) occasion that you get it:

    Dim MyRandomInt as long
    MyRandomInt = int(rnd*(99999-11000) + 11000)
    if MyRandomInt = 11000 then MyRandomInt = 11001

    That done, you need to check that MyRandomInt is not a duplicate; DLookup is probably the easiest way, and you could put the whole thing in a loop, in a function, like this:

    Public Function NextInt() as Long
    Dim MyRandomInt as long
    do
    MyRandomInt = int(rnd*(99999-11000) + 11000)
    if MyRandomInt = 11000 then MyRandomInt = 11001
    until DCount("*","MyTable", "FieldName = " & MyRandomInt ) = 0
    NextInt = MyRandomInt
    End Function

    Replace MyTable and FieldName with the names of your table and field containing the random numbers.

    HTH

    John
    Last edited by John_G; 08-07-2012 at 07:21 PM. Reason: fix typeo

  9. #9
    harrytgs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38
    Hi John, I will try this out when I get to the office at 10 today (it's 6 now)

    That function sounds perfect for my requirement - although I'm not sure how the function would integrate with my field - would I just have to add =myrandonint in the field's expression?

  10. #10
    harrytgs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38
    Hi John,
    I am adding it on the Visual Basic editor but it doesn't like the line that begins with until...
    It just says "Expected: line number or label or statement or end of statement".

    Any idea why?

    Cheers

  11. #11
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Hi -

    Oops - sorry! The until line should read

    Loop until DCount("*","MyTable", "FieldName = " & MyRandomInt ) = 0

    When do you need to generate the random number, i.e. what are doing at the time? My guess would be when you create a new record?

    John

  12. #12
    harrytgs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38
    Hi John,

    Yes - it should.

    I think the best way is for a form to appear with two fields - one for the new ID and one tick (Yes/No) box, which would generate a new one ...

    Is this a good idea?

    Cheers

  13. #13
    harrytgs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38
    I've set the code to the below as a new module (ID Generator):

    Public Function NextInt() As Long
    Dim MyRandomInt As Long
    Do
    MyRandomInt = Int(Rnd * (99999 - 11000) + 11000)
    If MyRandomInt = 11000 Then MyRandomInt = 11001
    Loop Until DCount("*", "Numbers", "ID = " & MyRandomInt) = 0
    NextInt = MyRandomInt
    End Function


    How is this now added on my database - will be playing around and will repost if I work it out!

    Many thanks

  14. #14
    harrytgs is offline Advanced Beginner
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2011
    Posts
    38
    Got it! I made it the default value and it works perfectly

    Many thanks

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

Similar Threads

  1. Replies: 8
    Last Post: 03-19-2012, 08:50 PM
  2. No-So-Random Number
    By oleBucky in forum Programming
    Replies: 8
    Last Post: 11-08-2011, 11:02 AM
  3. Query Random Number Column Manipulation
    By gmontano in forum Queries
    Replies: 4
    Last Post: 10-05-2010, 01:36 PM
  4. Sorta Random Serial Number Generator
    By Cuselco in forum Programming
    Replies: 3
    Last Post: 08-27-2010, 12:05 PM
  5. Random number generators with no repeats
    By CJ508 in forum Access
    Replies: 5
    Last Post: 10-29-2009, 07:30 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