Unanswered: RND() Genertaing same number on each field during update
I'm using the following code to generate a random ID code for a list of users. It used to work properly, now when I run it it uses the same random number for each field (which results in duplicates) rather than a new number each time.
autonumber... kidding, actually.
several months ago, i realized that autocreation of primary keys are painstakingly unnecessary... I'd usually end up extracting a coded key from information supplied by the user, or something like that...
i always liked the idea of keys holding sensible info relative to the record itself
You need to call Randomize just before the Rnd call. Is you code in a module? Or in a query? If it is in a query then I would suggest creating a function in a module that takes the name and zip as parameters and then creates the code for you.
It's currently in a query and for some reason used to work just fine. Might you all be able to provide some sample code for what you are talking about? I can usually tweak VB code to my needs, but I'm not good at starting from scratch. Thanks!
Here is some code for what I was talking about. Put it in a module in your database.
Public Function GetCode(varName As Variant, _
varZip As Variant) As String
Dim strName As String
Dim strZip As String
'Check to make sure the name and zip are not null, if so put in default values
strName = Left(Nz(varName, "NONAME"), 3)
strZip = Left(Nz(varZip, "NOZIP"), 5)
GetCode = strName & (Round((Rnd() * (9999 - 1000) + 1000), 0)) & strZip
Then in your query add a field similar to:
For each record, GetCode is called and the Name and Zip will be passed to the function and a code will be created.
Also, when you add the code to a module, you can test the code by typing
?GetCode("Test","60126") in the Immediate Window (View/Immediate Window). When you press enter the GetCode function should be called and a code should be returned.