comments inline.
MackBlale said:
Tina, it is absolutely necessary that the ContactID numbers be random. There
will never be more than 1000 as the job is limited. I must not be formatting
correctly because I am not getting the display with the leading zeros
like
at the table level, make sure the field's Data Type is Number, then set the
field's Format property to 0000.
If I understand correctly, I should create a table with one row of numbers
from 1-9999.
re-read my post, hon. i said 9999 *records*, NOT 9999 fields. you want *one*
number field, data type Number, and do not name the field "number". name it
something else, like "Num", for instance.
You lost me on the additional yes/no field. Where does that go
it goes next to the Num field. so you have two fields in the table:
Num (data type Number)
Assigned (data type Yes/No)
and how do I make Access use only unassigned numbers.
open a standard module in Access, you can name it modUtilities, and paste in
the following code, as
Public Function isRandomNum(ByVal X As String) As Single
' the x variable is included to force a call to this function on every
' record in the query. it is NOT used within the function itself.
Randomize
isRandomNum = Rnd
End Function
next, create the numbers table, as
tblNumbers
Num (data type Number, field size Long Integer, primary key)
Assigned (data type Yes/No)
enter 9999 records in the table, as
1
2
3
4
5
etc.
next, create a query to pull a random unused number, as
SELECT TOP 1 Num, Selected FROM tblNumbers WHERE Selected = False ORDER BY
isRandomNum([Num]);
if you're assigning the random number to a field in your data table, using
an open form bound to the data table, there are several ways to get the
number from the above query and then change the Selected field from No to
Yes (False to True) and save the Num record in tblNumbers so that number
isn't used again. the easiest way might be to just bind the above query to
another form, then open that form using code, set the Selected field of the
returned record to Yes (True), and save the record, then assign the value of
the Num field to the client number field in your data entry form. assuming
that your data entry form is called frmClients, and the client number field
is called ClientNum, and the second bound form is called frmNum, the code
would be something like
DoCmd.OpenForm "frmNum", , , , , acHidden
With Forms("frmNum")
!Selected = True
.Dirty = False
End With
Me!ClientNum = Forms("frmNum")!Num
DoCmd.Close acForm, "frmNum", acSaveNo
where you run the above code depends on when and how you want to assign the
client number. you could call it from the form's BeforeInsert event, or from
the BeforeUpdate event procedure (though if from there, you need to add code
to only assign the new random number on a record where there is no existing
value in the ClientNum field - otherwise you'll be overwriting previously
assigned client numbers), or from a command button on frmClients.
using a hidden form to get an unused random number in a multi-user
environment is questionable, since it will be slower than
opening/updating/closing a recordset entirely in VBA. but if adding new
clients is not a constant, second-to-second action, or if the database is
single-user, it should be okay.
hth
first,
third,
the