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 - Private Sub CmdDupe_Click()
-
-
If MsgBox("Press Ok to create a duplicate record with the same details." & vbNewLine & "Cancel to exit.", vbQuestion + vbOKCancel, "Duplicate Record") = vbCancel Then
-
Exit Sub
-
Else
-
'Duplicate the main record: add to form's clone.
-
With Me.RecordsetClone
-
.AddNew
-
-
-
intHighNumber = 99999
-
intLowNumber = 25001
-
-
For i = 1 To 5
-
Randomize
-
intnumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
-
Me.uniquenumberfield = intnumber
-
If DLookup("uniquenumberfield ", "tbl", "uniquenumberfield =" & Me.uniquenumberfield ) Then
-
MsgBox "Number - " & Me.uniquenumberfield & " already used. Click again to generate unique number", vbCritical + vbOKOnly, "uniquenumberfield Dupe"
-
-
Me.uniquenumberfield = ""
-
Me.uniquenumberfield.SetFocus
-
End If
-
-
Exit Sub
-
intnumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
-
-
Me.uniquenumberfield = intnumber
-
Next
-
-
!otherfields1= Me.otherfields1
-
!otherfields2= Me.otherfields2
-
-
'etc for other fields.
-
.Update
-
-
.Bookmark = .LastModified
-
-
'Display the new duplicate.
-
-
Forms!formname.Requery
-
-
End With
-
End If
-
-
Exit_Handler:
-
Exit Sub
-
-
Err_Handler:
-
MsgBox "Error " & Err.number & " - " & Err.Description, , "Error"
-
Resume Exit_Handler
-
End Sub
15 4866
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?
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)
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.
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
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.
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
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
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
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.
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
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.
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 -
with Me.RecordsetClone
-
.AddNew
-
intHighNumber = 10
-
intLowNumber = 1
-
For i = 1 To 5
-
Randomize
-
intnumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
-
-
If DLookup("uniqnum", "tbl", "uniqnum=" & intnumber) Then
-
-
'continue generating
-
Randomize
-
intnumber = Int((intHighNumber - intLowNumber + 1) * Rnd + intLowNumber)
-
Else
-
-
Me.newnum = intnumber
-
'newnum is textbox to store the value
-
End If
-
Next
-
!uniqnum= Me.newnum
-
!otherfileds=me.otherfields
-
.update
-
me.bookmark=me.lastmodified
-
end with
-
-
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.
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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:
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
| |