Generate Random No (1 Viewer)

kzatakia

New member
Local time
Today, 10:34
Joined
Apr 16, 2016
Messages
7
Hi,
I am working on customer care application and need to generate 3 code for asking review about service, I try to below code and generate it but every time I get same result, might be some where I doing wrong....please help me to rectify or you have any other way then plz plz guide me on it.

warmly welcome to valuable suggestion and correction

(Note: I am not a professional, but just keep focusing on my application work well by help of genius like you, so please bare with me if code found childes...:p


Currently Using code make easy explainable :


'Generate 4 Digit Random No between 1001 to 9000


Private Sub Command4_Click()


Dim GenerateCodeCheck As Boolean
GenerateCodeCheck = DLookup("generatecode", "ConfigT", "ConfigID = 1")

If GenerateCodeCheck = True Then ' check with config is it true or false

Dim sI, sMyNum, ab
For sI = 1 To 2
sMyNum = Int((9999 * Rnd(Now())) + 1)
If sMyNum > 1000 Then
scode.Value = sMyNum ' scode is used for satisfactory service
'GoTo ab
End If
Next
'ab
Dim aI, aMyNum
For aI = 1 To 10
aMyNum = Int((9999 * Rnd(Now())) + 1)
If aMyNum > 1000 Then
acode.Value = aMyNum ' acode is used for avrage service
End If
Next
Dim pI, pMyNum
For pI = 1 To 10
pMyNum = Int((9999 * Rnd(Now())) + 1)
If pMyNum > 1000 Then
pcode.Value = pMyNum 'pcode is used for Poor service
End If
Next
iscodegenerate = True
Else
iscodegenerate = False
MsgBox "Code is not generated : check your config setting"
End If
End Sub
 
Last edited:

JHB

Have been here a while
Local time
Today, 18:34
Joined
Jun 17, 2012
Messages
7,732
I don't know what you exactly do, (then your code isn't logic), but you need a Randomize.
Google it to get the explanation about it.
 

kzatakia

New member
Local time
Today, 10:34
Joined
Apr 16, 2016
Messages
7
Hi, JHB, Thanks for Prompt,

I am using Randomize, but face issue

1) each time generate and store value in table same as every records

2) I want only 4 digit between 1000 to 9999
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 01:34
Joined
May 7, 2009
Messages
19,169
this is a general purpose random number generator function
to call it:

variable=RandomNumber(1000) or
variable=RandomNumber(1000, 9999)

Code:
Option Compare Database
Option Explicit

Public Function RandomNumber(Optional Lowest As Long = 1, Optional Highest As Long = 9999)
' Generates a random whole number within a given range
   Randomize (Timer)
   RandomNumber = Int(Rnd * (Highest + 1 - Lowest)) + Lowest
End Function
 

kzatakia

New member
Local time
Today, 10:34
Joined
Apr 16, 2016
Messages
7
Thanks Minty,
as reference of link I write this, and get result also, but I need 4 Digit No.
so, I use for...next statement
could you or anyone check that is correct ?

Private Sub Command4_Click()
Dim LRandomNumber As Integer
Randomize
LRandomNumber = Int((9999 - 1001 + 1) * Rnd + 200)
scode = LRandomNumber
'Dim sI, sMyNum
' For sI = 1 To 5
' Randomize
' LRandomNumber = Int((9999 - 1001 + 1) * Rnd + 200)
' If LRandomNumber > 1000 Then
' scode = LRandomNumber
' End If
' Next
End Sub
 

kzatakia

New member
Local time
Today, 10:34
Joined
Apr 16, 2016
Messages
7
Thanks arnelgp

Solved with vary simple and easiest way...:)

Thanks all of you too
 

Users who are viewing this thread

Top Bottom