473,471 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Random Alphabet combination from a given string

171 New Member
Hi
Is there a way we can generate random alphabet combination from a given string? I am looking for a 3 letter combination from a given string by using vba.
Before or after generating it should validate as a unique combination which is not available in another table?
Hope someone can help me

Regards
Oct 27 '15 #1
8 2015
zmbd
5,501 Recognized Expert Moderator Expert
If you mean, is there a built in function, yes and no,
yes because you can (in newer version of Access) use an autnumber field set to the "Replication ID" data type that returns random strings such as:
{C8D98C03-FC97-4054-9F5D-ED4ABB144497}

No, in that there is no built in function that will either generate a random string by itself nor from a provided string.

Can you program one, yes, indeed I have such a function. :)

+ I based mine on the static string of:
1the2quick3brow4nfx5jum6psv7er8laz9ydg
Then length is 38
+ Standard randomize command to ensure that each round starts with a new random number seed
+ Standard method of producing the random number, one such being: CInt(Int((n * Rnd()) + 1)) so in the above would be CInt(Int((38 * Rnd()) + 1))
+ Use the Mid() function to return a single character from the string.
+ I have one parameter passed to this function which is the length.
I loop the number of times, concatenate the result from each loop with the prior

I even have one that will calculate a check digit to validate a provided "serial number;" however, that is a different thread. (-.-)

We'll be happy to take a look at your version of the code.
Oct 27 '15 #2
rajeevs
171 New Member
Hi zmbd
Thank u for the reply.
I have found a code with which I tried but not getting the desired result. I am posting it here so you can help me to modify it according to my requirement. The code actually taking the first 2 characters and then adding another one from the alphabets. I need the code to select the first 2 from the given string then select randomly from another 1 from the given string only. Hope I have explained well
Expand|Select|Wrap|Line Numbers
  1. Sub GenerateRandomAlphabetsFromGivenText()
  2.    Dim s As String * 3 'fixed length for the resulting string
  3.    Dim n As Integer
  4.    Dim ch As Integer 'the character
  5. '
  6.    For n = 1 To Len(s)
  7. '
  8.       Do
  9.          ch = Rnd() * 127
  10.       Loop While ch < 65 Or ch > 90
  11. '
  12.       s = "test vbtr"
  13.       Mid(s, n, 1) = Chr(ch)
  14.    Next
  15. '
  16.    Debug.Print UCase(s)
  17. '
  18. End Sub
Oct 28 '15 #3
zmbd
5,501 Recognized Expert Moderator Expert
A) If you are going to call this to return the value within another sub-routine or via SQL then you should make this a Function(). For testing, the debug.print is ok; however, with a function you could <ctrl><g> then type ?FuntionName(AnyRequiredParameters) and have the result returned in the immediates window.

B) Line 2: not sure that you really need to explicitly declare the fixed length. You're controlling the length with your loop and appending script - unless you are going to alter the string length later, in which case, I would place that in the parameters of the Function(zDesiredLen AS Long) and then pass that into the function for use in the loop
B2) Line 2 is in conflict with Line 12.
C) You need a holding string variable to hold your returned characters as you build the string.

D) Line 6: see (B)

E) Line 8 - 10: The Do...Loop here isn't needed. Use the hint I provided in my first post for returning a random number between 1 and a given upper limit.
E1) If you are not going to hard code the reference string, Line12, then you could pass this into the Sub/Func so that you have Function(zDesiredLen AS Long, zRefString as String) then use the len() function to set your upper limit on the random number
E2) you should include Randomize in your code, I'd suggest placing it just before your For..Next loop
F) In post #3 you stipulate that your first two characters should come from the reference string and then the third from "a-z"
F1) Alter your For..Next loop to only loop twice, building your first two characters from the reference string (or if you desire, you could alter this to loop n times based in the passed in parameter).
F2) Using the hint on Random number generation, set your bottom limit to 65 and the upper limit to 90 (note the hint provided give a result from 1 to upper limit... ) use the Chr() to convert to the String character and append to your current string

So your code could look something like
Expand|Select|Wrap|Line Numbers
  1. Function ExampleAirCode(zDsrdLn as Long, zPick1stN as Long, _ 
  2.    zTestStr as String) As String
  3.    ... Declared variables
  4. '
  5.    ... validate and pass in zDsrdLn, zPick1stN and zTestStr
  6. '
  7.    ... get the length of zTestStr
  8. '
  9.    ... For...Next loop based zPick1stN value to return the first N characters from the zTestStr. Use the Length of zTestStr as the upper limit on the rnd() and the Mid() to return from the zTestStr
  10. '
  11.    ... now use the rnd() with the lower/upper bounds for 65 thru 95, convert, and append to your built string.
  12.    ... could add a loop here to append more than one!
  13. '
  14.    ... now ExampleAirCode = builtstring.
  15. '
  16. Exit Sub
  17.    ... should have error trapping setup
  18. End Sub
That should get you started.

Post back after you have tweeked the code.
You might also want to bookmark > Before Posting (VBA or SQL) Code for the trouble shooting advice.

Just a to clarify this from post#2:
Expand|Select|Wrap|Line Numbers
  1. + zRefStr="1the2quick3brow4nfx5jum6psv7er8laz9ydg"
  2. + zUpper=len(zRefStr)
  3. + zPosition=CInt(Int((zUpper * Rnd()) + 1))
  4. + zTmp = zTmp & mid(zRef,zPosition,1)
zrefstr could be set to a passed in parameter or hardcoded as shown above; hence, the Len() to set the upper limit on the random number generator. In post#2 I showed the upper limit hardcoded to 38
Oct 28 '15 #4
rajeevs
171 New Member
Hi zmbd
Thank you for the reply. I just got your message. I will try the code and reply you with the result
Oct 28 '15 #5
rajeevs
171 New Member
Hi zmbd
Sorry for a late reply. I was trying with the same code I have posted because the code you have supplied was difficult for me to understand and make the changes to get a result what I need.
I have a form where I will enter a staff name and from that name I need to get a random combination of 3 letters (first letter should be the first letter in the staff name) and the other 2 letters can be selected randomly from the staff name. After generating the 3 letter, cross check this 3 letter against a table for any duplicates. If there is already same 3 letter then the process will continue till it can generate a unique 3 letter and show the result.
Hope I explained well. Pls help
Nov 8 '15 #6
zmbd
5,501 Recognized Expert Moderator Expert
+ The cross check against a table is fairly easy.
You can create an index against the field with unique set to true, and/or, you can use a simple query with the WHERE clause appropriately defined.

In the lab, I most often use both the indexed method as I have to ensure that all of the sample serial numbers are unique and due to regulatory requirements these are never purged. I use the query just to avoid error trapping an index violation before appending/updating the records.

+ zPosition=CInt(Int((intLargestNumberToReturn * Rnd()) + intLowestNumberToReturn))

So if you want between 0 and 100
zPosition=CInt(Int((100 * Rnd()) + 0))
(of course, the zero isn't required, it's just there for illistation)

So if you want between 65 and 95
zPosition=CInt(Int((95 * Rnd()) + 65))


+The code you posted will prove, IMHO, more troublesome than you expect; however, post your latest and we'll revise from there.
Nov 8 '15 #7
rajeevs
171 New Member
Dear zmbd
please note that I am looking for only alphabet combinations from a given string of alphabets. If "SELECTION" is the given string it should check the possibilities of starting from the first two characters "SE" and start adding the next letter "L" (So the 3 letters are "SEL") and cross check with a table if it is unique. If not then try with the next letter that is "E" so the 3 letters are now "SEE". Try until it can find a unique combination. Hope my request is clear. Please advise
Nov 9 '15 #8
zmbd
5,501 Recognized Expert Moderator Expert
Yes,
I understand what you want.
I have provided the basic outline of the required code all you need to do is fill in the blanks.

I'll happily help trouble-shoot your posted code; however, I currently do not have the time today to write the code for you.
Nov 9 '15 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: zjut | last post by:
I need to implement the method : round(String name, int index) The given string maybe the every type of float type, ( the msdn given the regax is that : integral-digits]exponential-digits]) ...
10
by: kd | last post by:
Hi All, Is there a command to check whether a given string is present in a text file, without having to read the contents of the file, a line at a time and then check for the given string? ...
8
by: varsha.gadekar | last post by:
what is the easy way to check if the given string is substring of other string only using c++?
2
by: Kelly B | last post by:
I tried to write a code which would reverse the order of words in a given string. I.e if Input String=The C Programming Language Output String=Language Programming C The Here is the code...
1
by: redgrL86 | last post by:
Hi, I am trying to figure out how to see if any of a given set of childnodes equals a given string. For example, in the XML and XSL code below, I want the text in the "xsl:when" statement to output...
10
by: mandyj | last post by:
Given String:- ST200STT:35697600114106009,20070407,08:09:00,+18.511246,+73.842756,1.430,332.87,1,0000,0 I wanna separate this string in the format below,pls help me out... 35697600114106009 =...
1
by: vaskarbasak | last post by:
Hi All, 1.How can i convert a String to Unicode characters? 2.Have there any way to get the Unicode range for a given String? Thanks! vaskar
16
by: saki | last post by:
Write a program to print all the permutations of a given string.
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.