Modules & VBA :: Generated Random Number Always The Same
Jun 2, 2015
I have a log in screen that my users must use before they can use my db. if they enter the wrong username/password they get the usual message boxes. I also thought it would be a nice personal touch if once they've logged in they get a little greeting, so i made a message box that says "Hello " followed by their first name
This did the trick for a while, however theyre sick of seeing "Hello" and want other greetings, i figured this wouldnt be too complicated and took me only about 10 minutes or so to implement.
Basically i created a table (tblGreetings), has two fields, an autonumber field (ID) and a short text field
I've added the following code into the login sequence:
Dim greetings As Integer
Dim greet As String
Dim randomNum
greetings = DCount("ID", "tblGreetings")
randomNum= Int((greetings - 1 + 1) * Rnd + 1)
greet = DLookup("Greeting", "tblGreetings", "[ID]=" & randomNum)
which in theory should generate a random number between 1 and "greetings" (which is an integer representing the number of greetings ive stored in my table), currently greetings=9
then it should pick a greeting from tblGreetings using the random number as an ID
This more or less works, except whenever the DB is opened and a user logs in, the "random" number is the same every time, it always seems to be 7 (which corresponds to the greeting "G'day").
Why this random number is always the same? it changes if i run the code a few times in the same session, but once the database is closed and reopened it still gives off the number 7, every time
View Replies
ADVERTISEMENT
May 28, 2015
I've been building a CRM in Access that allows for creating profiles for our accounts. When we add a new account, I'd like an Account ID to automatically generate and look like this: CRMXXXXX where the X's represent a random number. I originally wanted to set the field as autonumber to just count up, but unfortunately I need to append this value to another sales database we have; you can't store the text "CRM" with an autonumber in a table.
Right now, all I've done is place an equation in the default value of a textbox I have on my 'Add Account' form. I then made the control source of the text box the Account ID field. My equation is as follows:
="CRM" & CStr(Int(Rnd(Now())*10000))
This appears to work, as it generates a value in the correct format. However, I've realized that each time I open the CRM, the random numbers start over again and I run into issues of trying to create a new record with the same ID as a previously created record. I assume I need to incorporate some sort of timestamp to it in order to change it.
View 5 Replies
View Related
Aug 21, 2013
I have a RN generator that looks at 9 different lanes and puts in a number based off of a certain criteria but I was wondering if in the frmLanes the actual "Go" button was an incoming number how could I get my current random number generator to look at that number and determine if the current number is within 5 of the "go" number?
Any way to have a beginning number that I would enter into a text box and have the number generator kick off based off of that number. I have included a copy of my db for viewing.
View 7 Replies
View Related
May 10, 2005
Quote by Pat Hartman
>>>BTW, one somewhat undesirable thing happens when you go with replication. Access changes ALL your autonumbers to random instead of sequential. That means that from that time on, all new autonumbers will be assigned randomly to minimize the problem of conflicts between the design master and replica sets. A random autonumber can range from a very small negative number to a very large positive number. These autonumbers can no longer be used as your customer numbers or order numbers if these numbers are ever printed or given to the customer to be used for reference. A customer probably will be confused by an order number = -2. If this is going to cause you a problem, you'll need to switch to custom generated numbers. Post back for details if you need them.<<<<
I'd be interested in getting some details. Data will be entering that table through a data entry form and through appending from a second copy of the DB. I want to make the PK a random autonumber but then when the second DB is added to the Master DB have the table in the Master DB assign each record a "prettier" more usable id number. I know this is stupid but I'm not sure how to relate code to a table (there's no before update event for a table as far as I'm aware).
Thanks for any help you can give.
Stephanie
View 1 Replies
View Related
Mar 17, 2014
I am working on a form and need it to be able to generate a random number to use as an invoice number. The only parameters i need it to meet is that it is at least 5 digits in length. I was hoping i could use a button to make the number generate in a separate box.
View 5 Replies
View Related
Jul 16, 2013
I am trying to construct a system in access where I can have a user type in a part number, revision of a part, job number, serial number quantity, and generate a serial number based on the quantity that they need.
I am getting hung up on even how to construct the tables effectively to accomplish this, let alone the forms, queries, etc.
The numbers have to be in the following form. XXXX-XXXX
For an example:
I have part ABC and I need 5 serial numbers.
I need to be able to create a batch of serial numbers automagically that reads like:
9001-0001
9001-0002
9001-0003
9001-0004
9001-0005
Then for the next part XYZ I need 3 serial numbers. It would be constructed like this:
9002-0001
9002-0002
9002-0003
With the goal of eventually being able to search a part number and revision, or a job number and return all serial numbers assigned to that.
Is this possible in MS Access?
View 4 Replies
View Related
Jul 8, 2006
Quick question, how do I have access generate a random number between 1 and 8? I want it to generate either a 1,2,3,4,5,6,7, or 8. (no zero, no nine)
Expr1: Fix(Rnd(8-1)*10*Rnd()) is what I have been using, but this always generates the number 2? I do want the decimals to be rounded to the nearest integer, thus I have Fix.
If someone could please help me, it would be greatly appreciated! Thanks in advance.
View 3 Replies
View Related
Feb 11, 2006
How can I make random auto numbers 8 digits in length (including leading zeros) and always positive?
Thank you
View 2 Replies
View Related
Sep 25, 2006
Hello,
I have this query:
SELECT UNIT.Code, Rnd() AS Expr1
FROM UNIT;
The problem is that in the Expr1 field all the numbers are the same. I would like to have random numbers that are different for each record (generate new random number for each record)
Thanks for your help
Cheers
View 2 Replies
View Related
Feb 13, 2006
Ok, I apologize in advance for my lack of knowledge but I need Help!!
I have a database form that has a O_Num field that populates the Order table. Now the client wants the order number to be a randomly generated number. this is the primary key and I cant use autonum because there is another field in the table that is autonum.
I have tried to use the following in the OnClick field, doesnt work
=Int((1000 * Rnd()) + 1)
View 1 Replies
View Related
Mar 5, 2005
Hello i've got to make a project for school and thought I would make a rental shop like Blockbuster. But I need to create a number that will be assigned to each customer when they become a member. So I need to generate a random number that uses a certain amount of digits (I was thinking about 8) and that won't be used again can you help.
rn2j
View 2 Replies
View Related
Jan 30, 2005
Hi All,
I'm designing this database, in which, I have to give each record a unique random (or sequance) number of 5 digits, giving that the first digit must be retrived from another field.
Please Help, I need this ASAP!
Any help will be very much appreciated!
Thanks,
CS.
View 1 Replies
View Related
Oct 22, 2013
Anyway, here is what I am looking to do:
1.) Generate a random number between 1 and 100 and assign the result an identifier, like a person's name.
2.) Generate a second random number between 1 and 100 and assign the result a different identifier, like another person's name.
3.) Compare the two randomly generated numbers.
4.) Have the identifier of the LARGER random number displayed on the screen.
I would like to have this linked into a button click.I am hoping there is an easy way to accomplish this task.
View 3 Replies
View Related
Aug 3, 2012
On an Access database, I would like an ID number to be generated randomly.
This ID number needs to be within the range of 11000 - 99999.
How is this possible?
View 13 Replies
View Related
Mar 9, 2006
My company does daily, weekly, and monthly drawings using mountains of entry tickets and a barrel that has seen better days. So needless to say this system has to go. I hope to be able to do it digitally.
I would like to do a random number generator to pick the winners based off of a ticket number range. The ticket numbers issued to the customer would correspond to the customer account number. To complicate things I would really love to have the random numbers displayed on the screen so people feel they are watching the randomization pick in progress with the winner name popping up.
I have searched Help for Rnd function, as well as googled. I haven had any luck finding what I need. Is what I am looking to do possible in access? I had something similar in Excel a few years ago but lost it.
View 7 Replies
View Related
Mar 24, 2005
hello all
I am trying to set column values to a random number between 3 and 5
PMRatingTokenID: textH1: doubleYearID: textCode:UPDATE PMRating SET PMRating.H1 = (5+3-3)*Rnd()+3
This shows type mismatch error . What is the problem here..
please do help...Thank you
View 1 Replies
View Related
Jun 11, 2013
I would like to create a table containing email templates for use around various applications and interfaces. I would also like the user to be able to create the text for these templates, but as well as the dynamic text. So a user might create a record as such:
Email_Template_ID: 1234
Email_Name: "Payment notification"
Email_Subject: "Project ID: {Project_ID} - Payment made"
Email_Body:
"Dear {Title} {Surname}
We have recently made a payment of {Amount}. Please check your bank account in the next two days. This relates to Project ID: {Project_ID}, {Project Name}.
Best wishes
Payment Team"
So a user, with sufficient knowledge of certain field names, would be able to drop in the dynamic content as they deem appropriate. The VBA would then replace anything in curly brackets with the genuine field name.
I could possibly start with Replace(Email_Body, "{", "' & [") and repeat it for the end curly bracket, but I'm sure VBA would just render this as Dear ' & [Title] & ' etc. without interpreting it as Dear Mr...
I hope I've explained myself OK. To look at it another way, in Excel there is a function called INDIRECT() which allows Excel to interpret a cell reference as the actual content of the cell...
View 1 Replies
View Related
Sep 28, 2006
Hi.
I have major report due within hours.
I need to add a field to existing table that will be a 9 digit number, non duoplicating or - to take an existing field of 9 digits and allow it's values to change to random or incremental, non-duplicate numbers of 9 digits. I already have an autonumber field I must keep.
Russ
View 2 Replies
View Related
Jul 31, 2013
I have a form which allows the user to add new records to a table. After the user had entered all the information into the form, they click a command button to add the record. In addition to adding the new record, my command button runs an query which is supposed to generate a random number between 1 & 1,000,000,000 and update the record ID field with that number.
Here is the formula I have been using in the "update To" now of my query: Int((1000000000-1+1)*Rnd()+1)
My problem is that I keep getting duplicates. You would think that the chances of getting a duplicate number would be pretty small with this large of a range, but I get a duplicate almost every time.
I have tried indexing (No duplicates) the field in the table, but that did not work. When my query generated a duplicate number, the record was just not added to the table.
I also tried a two step approach:
1-Make a table of all in use record ID numbers from my table (tblIdNo)
2-Update new record with a random number that is not in tblIdNo
This was a no-go too
How to build an update query that will update each new record added to the table with a random number between 1 & 1,000,000,000 without any duplicates? This seems like it should be so simple, and I am starting to get really frustrated.
I would prefer to accomplish this through a query/queries (if possible) rather than with 100 lines of code. This database is not for me, it's for another group, and the individuals in this group are totally freaked out by code.
View 3 Replies
View Related
Jan 2, 2015
I'm trying to create a query that generates random numbers for each record, sorts them by that field, then selects the top record. This should randomize the record being selected.
I can use the Rnd([ID]) function which does appear to generate a random number. Problem is that each time I exit the program and come back in, it always selects the same record. When I remove the Top = 1, to show all the records, every row does have a different random number but it does not appear to be sorting by this field.
If I run the query, here is the number I get: 0.98609316349029
Exit the program, restart, and run the query again: 0.98609316349029
If I refresh the query, the second and third time does appear to be random but the first result is always the same. how to generate truly random numbers?
View 7 Replies
View Related
Nov 24, 2014
I have an Access 2013 database which will generate a pdf report and attach it to an email using the code:
Code:
DoCmd.SendObject acSendReport, "Report Name", acFormatPDF, Nz(To email address, a), , , "Email Subject", "Email body", True
I would also like to automatically attach on the server to this email. Such a file stored such as servernamesharefilename.pdf. Is it possible to do this?
View 14 Replies
View Related
May 27, 2014
Is it possible to run a SQL command to update a field within a table with random numbers?
More specifically - random long integers linking back to an ID (autonumber) field in another table?
Background to this is, I have multiple static data tables related to each other by long integer identifiers (autonumbers)
The structure is fine but I haven't been provided with the actual data yet - but for development purposes, I need to work on other functionality which requires that this data be present.
So I want to fill my table with dummy data such that I can go off and work on the remaining functionality, but then just go back and clear it all out once I get the actual data.
I have one 'main' static table, which links back to other tables, which I have already populated with dummy static (i.e. company names, locations etc) Now I want to go into my main table and populate those fields in each record with a random ID. I don't mind doing this field-by-field (there's only a handful) but I've a lot of records in there (~1000) so I'd rather not do this record-by-record.
View 6 Replies
View Related
Sep 6, 2013
I am using a API to generate a random / unique directory. This an API form a 32-bits environment.
I know that you can still use it when declaring it with PtrSafe
This is the code I am using:
Option Compare Database
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Declare Function GetTempFileName Lib "kernel32" Alias "GetTempFileNameA" (ByVal lpszPath As String, ByVal lpPrefixString As String, ByVal wUnique As Long, ByVal lpTempFileName As String) As Long
[Code] .....
View 1 Replies
View Related
Jun 26, 2013
I'm working on a database for my holiday pictures. The pictures are organized in folders per trip/date/location.
When viewing the data of a particular trip, I want the form to display a random image from the respective folder (path stored in database).
I already found a function to count the files in the folder:
Code:
Function GetFileCount(folderspec As String) As Integer
' Returns a count of files in folderspec, or -1 if folder does not exist
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FolderExists(folderspec) Then
GetFileCount = fso.GetFolder(folderspec).Files.Count
Else
GetFileCount = -1
End If
End Function
to plug into a "randomizer":
Code:
lngFileCount = GetFileCount(strFOLDER)
Randomize
intRND = 1 + Int(lngFileCount * Rnd())
MsgBox intRND
The thing I can't find is how to reference this nth file (or even if there is a way to directly reference this file).
Is there, or do I have to use some kind of looping?
View 3 Replies
View Related
Nov 21, 2014
I have a form with windows media player embedded in it. What I want it to do is play a random song when I open it. So I put a field called Randomizer in the form and added this code.
Me.randomizer.Value = Int((60 - 1 + 1) * Rnd + 1)
Now How do I put the URL for the song into vba format? I tried just using the URL property field for the player (D:[randomizer].mp3) but no luck.
View 3 Replies
View Related
Jun 15, 2014
The code works perfectly except that a random "0" appears in the body of the email. How to correct the code to remove this annoying little "0"?
Private Sub Command62_Click()
Dim strSubject As String
strSubject = Me.LoadNo
DoCmd.SendObject _
[Code] .....
View 1 Replies
View Related