Link to home
Start Free TrialLog in
Avatar of Jegajothy vythilingam
Jegajothy vythilingamFlag for United States of America

asked on

Access 2013 - variables

My OS is win 7 prof 64 bit and i use Access 2013.  i have a small form that has code.
i have a variable say, pwd which is dim as a string.  
If pwd is assigned as, after some routines,  ; "123456"
How do I code to assign the "123456' to a function.  
like : randomize (123456) because when I insert the variable it does not work.  
Thank u.
Avatar of Simon
Simon
Flag of United Kingdom of Great Britain and Northern Ireland image

Hi,

My solution to this previous question of yours had an example of this.

In the TestIt sub that I included there to demonstrate the function, I pass it a string value:
RandomiseString("1234TqRi*(Ka")

If you already have a string variable you can pass that in place of the string
DIM x as string
DIM myResult as string

x="12345"
myResult=RandomiseString(x) 'pass the variable to the function and store the return value in the variable 'myResult'
msgbox myResult 'Display the randomised version of the original string

Open in new window


Looking at the way you've written your example above, you may have used the NUMBER 12345 instead of the STRING "12345" as the parameter for the function. However, that still works and returns a randomised STRING representation of the input number.
Avatar of Jegajothy vythilingam

ASKER

in response to SimonAdept, thank u for your response.  The string that I am passing to the Function, consists of Numbers + Ucase + Lcase + special characters.  The number of each of these, will be dependent on the user's input, i.e. if the User wanted 2 numbers, and 2 Ucase, and 2 Lcase and 2 special characters, thus making the string 8 characters long.  
My problem is I cannot seem to pass a variable into the Function, like :
Test = randomizestring (pwdtoarrange)
pwdtoarrange is declared as a string, and say it has been passed : "789few&", during the above assigning process.  
Not sure whether it is the way version 2013 behaves or not. thank u.
Just curious...

Whats the need to create a variable, and then randomize it...
What not just create a random string to begin with...?
Can you tell us more about the symptoms of it not working?
Are you getting an errror message? If so, please post it.
Have you tried stepping thru the code using the toolbar button or setting a breakpoint and then using the F8 key?
Have you tried right-clicking the variable and setting a watch on it so you can see its value after each line is executed?
in response to SimonAdept, From the form, i passed the Function into a variable, like strresults = randomizestring (pwd) and pwd's value is a string of " ABxi96@! ".   but when i examined the variable, the result was not passed.  When I stpped into the program, The program executed each line correctly and i did not observe any errors.  
But if i were to put a msgbox inside the Function to see the results, it displays correctly the returned result.
Thus, I am at a lost.
The function is located at the Module.

I was thinking of declaring a global variable inside the Function, that will be visible outside the Function, if u think this will work, can u please let me know what is the syntax to declare this variable, and also where it should be declared, i.e. at the Module below Option Explicit where the Function is located or at the Form level.

I have tried relocating the Function at the Form level, but the variable still is not changed after the Function had been executed.

Thank u for your advise.
Ahh, did you use the code from the first version I posted on your previous question rather than the version you marked as the solution? In the first version I had omitted the line that provided the return value. The correct version of the function is:
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

Open in new window


Line 10 in the listing above is the key one. It assigns the internal result to the function name, which is the return value of the function.
in response to SimonAdept, thank u for your response.  But how do i access the contents of the result from the Main form, is it like this;
 strresult = ??
rather than make u laugh of my being very basics, hope u could please give me the syntax.  Thank u.
in response to Jeffrey, the app is a password creation tool, where the user will specify how many letters, numbers and special characters that the result should be.  In order to make it doubly more difficult for anyone to guess, thus i thought I would rearrange by randomizing the result string.   thank u for your suggestion.
ASKER CERTIFIED SOLUTION
Avatar of Simon
Simon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
In response to SimonAdept, thank u for your suggestions.  i did create a new field as suggesed, but somehow I did not have any success in passing the return value of the function to the me.txtFinalpassword.  i brought the function from the Module to the Form level, but that still not work.  But when I inserted the syntax inside the Function, the field got populated with the changed value.  
although it is a bit unorthodox, but the result worked.  thus i have he original value before passing into the Function and the changed value after processing by the function.  Thank u for all your suggestions, and with best wishes.
thank u.