Solved

Access 2013 - Visual basic code

Posted on 2015-02-05
10
255 Views
Last Modified: 2015-02-09
My OS is win 7 prof 64 bit and I use  Office 365, Access 2013.  I would like to know what is the syntax to rearrange a string of text :
Eg : a string like : 1234TqRi*(Ka
The string will consist of CAPS, small case, numbers and any key from the keyboard.
To be rearranged at random using vb code.
Thank u.
0
Comment
Question by:jegajothy
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Expert Comment

by:FarWest
Comment Utility
you can put each letter in array and make loop for len of the string you want
that takes random number of the array index usin RND function
0
 
LVL 57

Expert Comment

by:Jim Dettman (Microsoft MVP/ EE MVE)
Comment Utility
There's nothing built-in to do that.   FarWest's idea is a good one and can be accomplished with all the usual string functions:  Left(), Mid(), Right(), etc and more than likely, you'll use Mid() the most.

Jim.
0
 

Author Comment

by:jegajothy
Comment Utility
In response to FarWest, thank u for your suggestion, but if u can spare the time to give me a sample code.  Thank u.
0
 
LVL 18

Expert Comment

by:SimonAdept
Comment Utility
This doesn't use FarWest's array idea (I was writing it before the first response) and is probably not as efficient, but it works...

Function RandomiseString(inputString) As String
mystr = inputString
myStrLen = Len(inputString)
resultstring = ""
Do Until Len(resultstring) = myStrLen
    random_number = Int(Len(mystr) * Rnd) + 1
    'Debug.Print random_number
    resultstring = resultstring & Mid(mystr, random_number, 1)
    mystr = Left(mystr, random_number - 1) & Right(mystr, Len(mystr) - random_number)
Loop
Debug.Print resultstring
End Function

Sub TestIt()
Debug.Print RandomiseString("SwitchThisPitch")
End Sub

Open in new window

0
 
LVL 12

Expert Comment

by:FarWest
Comment Utility
here is the code
Private Sub Command5_Click()
Dim ar() As String
Dim rstr As String
Dim ii As Integer
Dim rn As Integer
ar = Split("a,b,c,d,N,R,U,r,$,/,T", ",")
Randomize
For ii = 1 To 10
rn = Int(11 * Rnd())  '10 is array lenght
rstr = rstr + ar(rn)

Next
Debug.Print rstr


End Sub

Open in new window

0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 18

Accepted Solution

by:
SimonAdept earned 500 total points
Comment Utility
Sorry, had to tweak mine. I hadn't set the return value for the function:
Function RandomiseString(inputString) As String
mystr = inputString
myStrLen = Len(inputString)
resultstring = ""
Do Until Len(resultstring) = myStrLen
    random_number = Int(Len(mystr) * Rnd) + 1
    resultstring = resultstring & Mid(mystr, random_number, 1)
    mystr = Left(mystr, random_number - 1) & Right(mystr, Len(mystr) - random_number)
Loop
RandomiseString = resultstring
End Function

Sub TestIt()
Debug.Print (RandomiseString("1234TqRi*(Ka"))
End Sub

Open in new window


You can feed strings of any length to the function to return them re-ordered randomly.
0
 

Author Comment

by:jegajothy
Comment Utility
thank u everyone for your inputs.  SimonAdept's solution fits into my design, thank u very much.
0
 

Author Closing Comment

by:jegajothy
Comment Utility
SimonAdept's solution fits into my design, thank u.
0
 
LVL 12

Expert Comment

by:FarWest
Comment Utility
Final hint, don't forget to call Randomize to link the RND function call with system clock, so probability to have the same output twice will be close to 0
0
 

Author Comment

by:jegajothy
Comment Utility
In response to FarWest, my soluion has been a crude and dirty one, and hope it gives me the result that I want.  i think I should put up the final code to see what the experts think of it.  Thank u again.
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now