Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Random Code Generator 1

Status
Not open for further replies.

dpye

Technical User
Mar 24, 2003
31
0
0
CA
Here's my dilemma....

I'm looking for a way to build in a random code generator that I can embed into a form and have the result stored in the table the form is based on. If the code could use part of the date and/or username in the generated code that would be even better.

I'm not a programmer and this may be something really simple for a programmer to whip up. I hoping that perhaps there is pre existing sample code or an FAQ that I could use and incorporate.

I'm not opposed to coding this myself, I'm just not that experienced coding. Perhaps someone can get me started on how to code this.

Any help would be appreciated. For the time being I'm using a random code generator downloaded from the net and copying/pasting the generated code in the field.

Thanks.
 
I'm not really clear about what you want to do, but you could create a random number using the current date by putting something like the following as the Control source of a text box

=Format(Date(),"yymmdd") & Rnd(100000)

Simon Rouse
 
Thanks, I'll give that a shot.

I'll try and clarify what I want to do......

I have a form that records information pertaining to incoming calls, some of that information will be a case#. Basically, it's a tier1 agent calling tier2 agent for advice on a case.

The tier1 agent calling in will provide the tier2 agent with a case# and may request approval for something like sending a part etc. If approval is required I want to click a button that will generate a code and have that code save in a code field back in the table, this way the code stays with the record.

I would also like the code to have some meaning and not just look like I ran my hand across the keyboard. This is why I was thinking about incorporating something like the date, or my username, or part of case#.

I would assume the code would go on the "on click" event but, not being a programmer, I not sure how to generate the number.

Any help is certainly appreciated.
 
The OnClick property would be the eevent to use if you are going to atatched this to the command button.

Simply put, this is not too difficult of a task once you have identified all your value and desired results. Take a look at you form and please provide the follwing items. With these items, we can generate the code necessary to make what you want.

What you desire the output to look like. Provide an example or two as well as the control to receive the output.
Input fields. Names of controls that might be included in the random code and what type of data is stored in them.

With this bit of information, all you need to do is format the input to match you output, include an randomization you may want, then pass the output to the appropriate code.

****************************
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. (Albert Einstein)

Robert L. Johnson III
MCSA, CNA, MCP, Network+, A+
w: robert.l.johnson.iii@citigroup.com
h: wildmage@tampabay.rr.com
 
He Guys,

I first want to start out by apologizing for my late response, I was called away to meetings fo rthe past few weeks.

Anyway, I've given some thought as to how I like the code to look. I want to use the first and last initial of the current user + the last 4 digits of the case# + the 2 digit year and finally 2 random numbers. The should add up to a 10 digit authorization code.

example: dp456703xx

The dp would be my initials, the 4567 is the last 4 digits of a case#, the 03 is the year and the xx is a random number.

Background:
The form (frmResourcePhone)is based off the table called "tblResourcePhone" and here are the involved fields and the controls on the form they are bound to:

CurrentUser (form control is txtCurrentUser)
Case# (form control is txtCaseNumber)
Date (form control is txtDate)

Other fields on this form are:

AgentID (form control is AgentName)
ModelID (form control is cboModel)
IssueID1 (form control is cboIssueClass1)
IssueID2 (form control is cboIssueClass2)
Serial# (form control is txtSerialNumber)
Approval# (form control is Text69)
PartSent (form control is PartSent)
Service (form control is Service)
Notes (form control is txtCaseNotes)
ISSPNotes (form control is txtIsspNotes)

If there is more information required just let me know.

Thanks
 
Okay....not too bad.....here's what I would do.

Assumptions:
The Current User Name is First Last...like Robert Johnson
The Output (the approval #) will be placed in the Approval# control

Setup:
Place a command button on the form. Put the following code in the OnClick event of this command button

' ************** Start Code **************
Dim strMsg As String
Dim strApprovalNumber As String

If IsNull(Me![txtCurrentUser) Or IsNull(Me![txtCaseNumber]) Or IsNull(Me![txtDate]) Then
strMsg = MsgBox("You have not supplied all required data.", vbOkOnly, "Missing Data")
Exit Sub]
End If

' First Name Initial
strApprovalNumber = Left(Me![txtCurrentUser], 1)
' Last Name Initial
strApprovalNumber = stApprovalNumber & Mid(Me![txtCurrentUser], InStr(1, Me![txtCurrentUser], " ") + 1, 1)
' Last Four Of Case Number
strApprovalNumber = stApprovalNumber & CStr(Right(Me![txtCaseNumber],4))
' Last Two Of Year
strApprovalNumber = stApprovalNumber & CStr(Format(Me![txtDate], "yy"))
' Random Two Digits
Randomize
strApprovalNumber = strApprovalNumber & CStr(Format(Int((99 - 0 + 1) * Rnd + 0), "00"))

Me![Text69] = strApprovalNumber
' *************** End Code *************

That shuld do it....let me know.

****************************
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. (Albert Einstein)

Robert L. Johnson III
MCSA, CNA, MCP, Network+, A+
w: robert.l.johnson.iii@citigroup.com
h: wildmage@tampabay.rr.com
 
First of all, I want ot say thanks, for your time and patience with this.

Now there is a slight problem I'm having....

The number being generated is just something like 0365. The first 2 are the the year and the last 2 are randomly generated. It is missing the initials and case number.

Here is the code as it looks right now:

Private Sub cmdCodeGen_Click()
Dim strMsg As String
Dim strApprovalNumber As String

If IsNull(Me![txtCurrentUser]) Or IsNull(Me![txtCaseNumber]) Or IsNull(Me![txtDate]) Then
strMsg = MsgBox("You have not supplied all required data.", vbOKOnly, "Missing Data")
Exit Sub
End If

' First Name Initial
strApprovalNumber = Left(Me![txtCurrentUser], 1)
' Last Name Initial
strApprovalNumber = stApprovalNumber & Mid(Me![txtCurrentUser], InStr(1, Me![txtCurrentUser], " ") + 1, 1)
' Last Four Of Case Number
strApprovalNumber = stApprovalNumber & CStr(Right(Me![txtCaseNumber], 4))
' Last Two Of Year
strApprovalNumber = stApprovalNumber & CStr(Format(Me![txtDate], "yy"))
' Random Two Digits
Randomize
strApprovalNumber = strApprovalNumber & CStr(Format(Int((99 - 0 + 1) * Rnd + 0), "00"))

Me![Text69] = strApprovalNumber

End Sub
 
dpye,

First, check all occurances of strApprovalNumber....I noticed in your post I misspelled some of them.....the should all be strApprovalNumber.

Second, Make sure there is data in each of the fields we are pulling from.....I tried to account for any missing info, but maybe something isn't working right there...

Finally, put a stop point at the beginning of the code and walk through it line by line, checking the value for strApprovalNumber at each step.

If you want, feel free to zap me the db at my work address and I'll take a look for you. If you do, please remove any sensitive data and zip the file or it will be stripped by my exchange server...

Let me know how things go..

****************************
Only two things are infinite, the universe and human stupidity,
and I'm not sure about the former. (Albert Einstein)

Robert L. Johnson III
MCSA, CNA, MCP, Network+, A+
w: robert.l.johnson.iii@citigroup.com
h: wildmage@tampabay.rr.com
 
I love to give a programming solution. However, another way to skin this cat, is a non-programming solution. Have three fields in your table, that does not need to be viewed on the form. (You can use a text box to concatenate the three values later.)

It doesn't appear that you're a Autonumber field in your table. Why not create one, instead if it incrementing, have it Random (its a setting in the design of the table). Then have the other two fields store the user's name, and date. Simply concatenate the three values how you desire on a text box in a form or report.
$0.02
 
mstrmage1768 (Programmer

Thanks, it worked. It was just the typo, I feel stupid that I missed something simple like that.

Now, if I later decide to cut out the year and have 4 random number. I assume I would first of all rem the lines dealing with the date but I don't fully understand the randomize section so I would know what to edit. If you could just break that line down for me I think I'll be fine.

Once again, thanks for all you help
 
Hello DPye

Thought i'd lend a quick hand, just change the random generator line from this:

strApprovalNumber = strApprovalNumber & CStr(Format(Int((99 - 0 + 1) * Rnd + 0), "00"))

to this

strApprovalNumber = strApprovalNumber & CStr(Format(Int((9999 - 0 + 1) * Rnd + 0), "0000"))

quick explaination:
the 9s indicate how many numbers
the 0's indicate the format


Regards

Samulayo
 
Thanks,

That worked fine and now I have a better understanding of this line.

Now, I do have one last request which I feel guilty asking since you've been such a great help so far. anyway, my supervisor wants to ensure the code isn't easily duplicated.

She was thinking about having the 2 digit year surround the last 4 of the case. For example:

The code for case# 7301234567 as it looks now would be DP4567xxxx or DP456703xx depending on the number of random digits.

With the change it would look like DP045673xxxx or DP045673xx.

Since I'm not a programmer can you tell me what line(s) would need to be added and/or modified.

Thanks Again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top