Page 2 of 2 FirstFirst 12
Results 16 to 18 of 18
  1. #16
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2016
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,742
    @Fostertrident,

    Did you review Vlad's suggestion in post #6? Thoughts?

  2. #17
    Fostertrident is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    32
    Thanks for this, I didn't realise you could generate random autonumbers either. Thanks for your advice and I hope the lunch was still warm.

  3. #18
    Fostertrident is offline Advanced Beginner
    Windows 10 Access 2016
    Join Date
    Jan 2018
    Posts
    32
    Quote Originally Posted by accesstos View Post
    Even if, the choice of the separate table of random IDs, sounds like the best option, I think that it complicates the procedures. Doesn't hurts so much the creation of some new records or just one when you need it in the main table using code.

    So, my suggestion is the code below:
    Code:
    Option Compare Database
    Option Explicit
    
    Const lngcMaxID As Long = 999999
    Const lngcMinID As Long = 100000
    
    Function RandomNum(lngMax As Long, Optional lngMin As Long) As Long
        'Returns a random number between lngMin and lngMax
        Randomize
        RandomNum = Int(Rnd() * (lngMax - lngMin + 1)) + lngMin
    End Function
    
    Function SaveNewRecord() As Long
        'Creates a new record in Table1 with a random six-digit ID
        Dim lngID As Long
        Dim db As DAO.Database
    
        Set db = CurrentDb
        Do
            lngID = RandomNum(lngcMaxID, lngcMinID)
            db.Execute "INSERT INTO Table1 (ID) VALUES (" & lngID & ")"
        Loop Until db.RecordsAffected
        Set db = Nothing
        SaveNewRecord = lngID
    End Function
    
    Sub AddRecords(Optional ByVal intCount As Integer = 100)
        Dim i As Integer
    
        For i = 1 To intCount
            SaveNewRecord
        Next i
    End Sub
    There is an implementation of suggested code in attachment.
    Thanks for this I will have a look and study this tonight.

Page 2 of 2 FirstFirst 12
Please reply to this thread with any new information or opinions.

Similar Threads

  1. How to set maximum 8 digit number in default?
    By Jeremy Sng in forum Access
    Replies: 8
    Last Post: 03-14-2017, 09:02 AM
  2. Replies: 20
    Last Post: 10-13-2015, 09:05 AM
  3. auto generate a 5 digit number?
    By Andre73 in forum Access
    Replies: 2
    Last Post: 02-18-2015, 07:45 AM
  4. Replies: 1
    Last Post: 01-11-2012, 03:07 PM
  5. Access 2007: Can't Display 17 Digit Number
    By HedgeHog in forum Access
    Replies: 9
    Last Post: 08-14-2011, 01:46 PM

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