473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Random unique numbers for access records

171 100+
I have an access table with records having unique numbers in one field. I would like to duplicate the record except the unique number and while duplicating I like to give a different number.
I have found a code which can generate the random numbers but I dont know where to put it in the button click which creates the duplicate record and cross check the table to find wether the number has been already used.
I am duplicating the record through a bound form button click and the event is as below: Can anyone help me to re arrange or modify the code to achieve the result. Thank you in advance

Expand|Select|Wrap|Line Numbers
  1. Private Sub CmdDupe_Click()
  2.  
  3.        If MsgBox("Press Ok to create a duplicate record with the same details." & vbNewLine & "Cancel to exit.", vbQuestion + vbOKCancel, "Duplicate Record") = vbCancel Then
  4.     Exit Sub
  5.     Else
  6.         'Duplicate the main record: add to form's clone.
  7.         With Me.RecordsetClone
  8.             .AddNew
  9.  
  10.  
  11. intHighNumber = 99999
  12. intLowNumber = 25001
  13.  
  14. For i = 1 To 5
  15.     Randomize
  16.    intnumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
  17.  Me.uniquenumberfield = intnumber
  18. If DLookup("uniquenumberfield ", "tbl", "uniquenumberfield =" & Me.uniquenumberfield ) Then
  19. MsgBox "Number - " & Me.uniquenumberfield & " already used. Click again to generate unique number", vbCritical + vbOKOnly, "uniquenumberfield Dupe"
  20.  
  21. Me.uniquenumberfield = ""
  22. Me.uniquenumberfield.SetFocus
  23. End If
  24.  
  25. Exit Sub
  26.  intnumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
  27.  
  28.    Me.uniquenumberfield = intnumber
  29. Next
  30.  
  31.                 !otherfields1= Me.otherfields1
  32.                 !otherfields2= Me.otherfields2
  33.  
  34.                 'etc for other fields.
  35.             .Update
  36.  
  37.             .Bookmark = .LastModified
  38.  
  39.             'Display the new duplicate.
  40.  
  41.              Forms!formname.Requery
  42.  
  43.         End With
  44.     End If
  45.  
  46. Exit_Handler:
  47.     Exit Sub
  48.  
  49. Err_Handler:
  50.     MsgBox "Error " & Err.number & " - " & Err.Description, , "Error"
  51.     Resume Exit_Handler 
  52. End Sub
Mar 8 '14 #1
15 4866
GKJR
108 64KB
I didn't even look at your code yet, but do you have an auto-number primary key field? I guess the next question is can you create a duplicate record in the same table?
Mar 8 '14 #2
rajeevs
171 100+
The records are actually imported from a log file. And the log file has a number field which is unique. I need to create the same record sometimes but the table wont allow duplicate for the unique number. That is the reason i want to generate a random number first, then check in the table whether the number exists, if exists then generate another number and save the record. After saving the record, form to requery and show the last saved record (duplicate record)
Mar 8 '14 #3
GKJR
108 64KB
Can you change the option for the field to 'indexed(duplicates ok)' in design view? It might be set to 'no duplicates' already even though that might not be necessary for your application.
Mar 8 '14 #4
rajeevs
171 100+
I need the field as no duplicates. The above code generates numbers but i want to do a dlookup after generating the number and if the generated number is already in the table with another record then randomly generate and check for a unique number and append the record to table
Mar 8 '14 #5
GKJR
108 64KB
Do you know anything about using Database and Recordset Object variables in VBA? I think you may need to venture into this if you're talking about record manipulation.
Mar 8 '14 #6
rajeevs
171 100+
I think my request was to correct the above code. If you are unable to help please leave it and hope someone else can help me in this aspect
Mar 8 '14 #7
GKJR
108 64KB
My best suggestion is to declare a recordset and run some tests on the field in question. If the number already exists assign it a new value with the edit method. There are a lot of resources for generating a random number all over the internet. Once you assign a new number test it again to see if the new value already exists. If not then save, if so repeat the process.

I hope this helps...
Good luck
Mar 8 '14 #8
rajeevs
171 100+
Thank u GKJR
The above code does half of the requirement and I am not a vb expert. So I am looking for a re arrange of the code lines which generate the number, check the number if already any record has got the same, if then generate again till if can generate a unique else save the new record with the number generated.
Hope my request is clear
Mar 8 '14 #9
Rabbit
12,516 Expert Mod 8TB
1. Remove your exit sub, not even sure why that's in there.

2. Change your loop to an infinite loop and exit the loop only when you've found a non-duplicated number.

3. Assign the number to the field only after you've found a unique number.
Mar 9 '14 #10
rajeevs
171 100+
Dear Rabbit
Thank you for your reply. But your point number 2 and 3 is what I want the code to do. I am not an expert in VB. It will be a great help if you could help me to re do my above code as per your instruction
Mar 11 '14 #11
Rabbit
12,516 Expert Mod 8TB
This is not a code writing service but if you post your attempt at the solution using my suggestions, we can help guide you along.
Mar 11 '14 #12
rajeevs
171 100+
Sorry for the misunderstanding. I requested for a help to re arrange the code I already posted.
I have tried something between 1 and 10 but after reaching the last new number generated i get the error about table cannot accept the changes due to duplicate. but i want the code to split the record and give an option to enter the uniqnum manually
the modified code as below
Expand|Select|Wrap|Line Numbers
  1. with Me.RecordsetClone
  2.            .AddNew
  3. intHighNumber = 10 
  4. intLowNumber = 1 
  5. For i = 1 To 5
  6.     Randomize
  7.    intnumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
  8.  
  9. If DLookup("uniqnum", "tbl", "uniqnum=" & intnumber) Then
  10.  
  11. 'continue generating
  12. Randomize
  13. intnumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
  14. Else
  15.  
  16. Me.newnum = intnumber
  17.  'newnum is textbox to store the value
  18. End If
  19. Next
  20.                 !uniqnum= Me.newnum
  21. !otherfileds=me.otherfields
  22. .update
  23. me.bookmark=me.lastmodified
  24. end with
  25.  
  26.  
Mar 14 '14 #13
Rabbit
12,516 Expert Mod 8TB
Rearranging the code will not fix the problems in the code. You need to change the code using what I laid out in post #10.
Mar 14 '14 #14
rajeevs
171 100+
Thank you for the quick response.
What I am strugling is that I am not familiar with writing an infinite loop.
Is it something I have to try with
Do While Not before the Dlookup then randomize
after that
assign the value
Next
and start adding the fields to tbl
Mar 14 '14 #15
Rabbit
12,516 Expert Mod 8TB
Yes, you'll want to use a Do While loop instead of a For loop. You'll want to generate and check the uniqueness within the loop and exit the loop only when you've found a unique number.
Mar 14 '14 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: August1 | last post by:
A handful of articles have been posted requesting information on how to use these functions in addition to the time() function as the seed to generate unique groups (sets) of numbers - each group...
4
by: Megan | last post by:
Hi- I need some help/ advise on how to code unique numbers for the primary keys of my 2 tables. I inherited a database that covers information about Hearings and Rulings. Information about the...
1
by: bburton | last post by:
I'm using a form in Access which was used as a bug tracker. The users could update the record with an attachment. (The data type of the field that is used for attachments is the OLE Object) I...
3
by: Comboni | last post by:
Creating invoices from individual Access records. I think that my question is simple, but as a newcomer to using MS Access 2003, it is beyond me. I have read through the various ‘Invoice’ inputs...
1
by: ronnchpra | last post by:
Hello, Im still kind a new to C++ Programming? Can anyone help me write a small code that would generate 50 random numbers, say from between 1-100? This is a small part of a program I'm...
8
by: menmysql | last post by:
i am not bale to solve this problem since two weeks i am trying to access records from mysql database using jsp. inside this jsp program i wrote all my JDBC code. it is working very nicely and...
1
by: Franzi | last post by:
Hello, I have got a question concerning putting pictures in access records. I have a moviecatalogue in access and want to put individual dvd covers to each record... I figured out how to put a...
11
TTCEric
by: TTCEric | last post by:
This will be original. I promise. I cannot get the random number generator to work. I tried seeding with Date.Now.Milliseconds, it still results in the same values. What I have are arrays...
2
by: lmao29 | last post by:
hi guys, i am new to JAVA I am having trouble with array, anyone can help? here is the problem. if i input 10 numbers, how can i count the unique numbers and output them here is the example: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.