Results 1 to 8 of 8
  1. #1
    paulc is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Apr 2016
    Posts
    4

    add random numbers to an existing table

    Hi everyone,



    I'm trying to import a large text file with hundreds of thousands of rows, add a column with unique random numbers, and re export the text file for use in another program. I've tried this in Excel and R and they both butcher the original format. I've figured out how to add random numbers to a new table, and then cut and past some of my rows into that new table, but its no good for big tables. Is there a way to add a column to an existing table and populate it with random numbers? I'm pretty new to Access so if there are other ways to achieve the same goal, I'd welcome any and all suggestions.

    Cheers,
    Paul

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

  3. #3
    paulc is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Apr 2016
    Posts
    4
    Thanks Orange,

    That looks like very helpful code. I have two follow up questions,

    1) which part of the code is just to create and populate the random numbers ?
    2) where does one enter the code into Access to get it to execute?

    Cheers,
    Paul

  4. #4
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    The procedure called
    Sub PayBandStuff()

    populates the xtrafield with a Random number.

    I would suggest you import the data into table in Access. Then, add a field to the table to hold the
    random number, say RandField. Then adapt the procedure PayBandStuff to populate RandField.

    Show us your table layout and we can help you.

    You would run the procedure when ready.

  5. #5
    paulc is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Apr 2016
    Posts
    4
    Thanks for your patience with me Orange,

    Here is what the table looks like

    Click image for larger version. 

Name:	table screenshot.jpg 
Views:	8 
Size:	252.7 KB 
ID:	24441

    I'm comfortable modifying the code, but I'm just not sure where to enter the code and run it? Thanks for your help, I've not used Access at this level before

    Cheers,
    Paul

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    It appears that your table fields
    field1,2 and 7 are text datatype
    3,4,5,6 are number datatype.

    You could copy your table, add a field8 with number data type, then select FieldSize Double. Let's say your newTable is called NewtableXX, and the added field is RandField number datatype

    this routine should insert a Random number in RandField. (random numbers between 0 and 500 with about 10 decimal places)

    Code:
    '---------------------------------------------------------------------------------------
    ' Procedure : P_PaulC
    ' Author    : Jack
    ' Date      : 20/02/2014
    ' Purpose   : To put a random number in a field (Randfield) in a table NewTableXX
    '
    '---------------------------------------------------------------------------------------
    '
    Sub P_PaulC()
          'populate Randfield with random number
              Dim sql As String
              Dim rs As DAO.Recordset
              Dim i As Integer
    10       On Error GoTo P_PaulC_Error
    
    20        Randomize 'must use this to getnew random numbers
    30        Set rs = CurrentDb.OpenRecordset("NewTableXX")
    40        Do While Not rs.EOF
    50            rs.Edit
    60            rs!Randfield = Rnd() * 500 'just a number
    70            rs.Update
    80            rs.MoveNext
    90        Loop
    
    100      On Error GoTo 0
    110      Exit Sub
    
    P_PaulC_Error:
    
    120       MsgBox "Error  "  & Err.Number & " in line "  & Erl & "  (" & Err.Description & ") in procedure P_PaulC "
    End Sub

  7. #7
    paulc is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Apr 2016
    Posts
    4
    Thank you Orange! That worked perfectly, you have saved me so much time, I sincerely appreciate it.

    Now I need to figure out how to export it so the format matches my other software.

    Thanks again.
    Paul

  8. #8
    orange's Avatar
    orange is offline Moderator
    Windows 8 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870

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

Similar Threads

  1. Random Numbers
    By MTSPEER in forum Queries
    Replies: 8
    Last Post: 12-15-2014, 07:08 PM
  2. Access generating random numbers
    By dama in forum Forms
    Replies: 1
    Last Post: 02-19-2012, 06:36 PM
  3. Random Text and Numbers
    By Mclaren in forum Programming
    Replies: 5
    Last Post: 11-05-2010, 04:34 AM
  4. Random numbers
    By Gator777 in forum Access
    Replies: 3
    Last Post: 08-12-2009, 10:04 AM
  5. generating random numbers on form
    By anitra in forum Forms
    Replies: 1
    Last Post: 02-14-2006, 10:08 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