Solved

Access 2013 - variables

Posted on 2015-02-10
11
155 Views
Last Modified: 2016-02-11
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.
0
Comment
Question by:jegajothy
  • 6
  • 4
11 Comments
 
LVL 18

Expert Comment

by:Simon
ID: 40601841
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.
0
 

Author Comment

by:jegajothy
ID: 40602084
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.
0
 
LVL 74

Expert Comment

by:Jeffrey Coachman
ID: 40602187
Just curious...

Whats the need to create a variable, and then randomize it...
What not just create a random string to begin with...?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 18

Expert Comment

by:Simon
ID: 40602538
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?
0
 

Author Comment

by:jegajothy
ID: 40603159
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.
0
 
LVL 18

Expert Comment

by:Simon
ID: 40603372
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.
0
 

Author Comment

by:jegajothy
ID: 40603432
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.
0
 

Author Comment

by:jegajothy
ID: 40603450
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.
0
 
LVL 18

Accepted Solution

by:
Simon earned 500 total points
ID: 40603699
In your main form, if you want a calculated value in a textbox, make its datasource
=randomiseString(nameofControlThatHasTheInitialStringValueIn)

Or, if using VBA and you want the value to go into a textbox called txtFinalPassword
a) take the initial value to randomise from another control on the form.
me.txtFinalPassword = randomiseString(me.nameofControlThatHasTheInitialStringValueIn)
B) if you have the string already in a variable named 'pwd'
me.txtFinalPassword = randomiseString(pwd)
0
 

Author Comment

by:jegajothy
ID: 40604752
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.
0
 

Author Closing Comment

by:jegajothy
ID: 40604753
thank u.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Use Windows Task Scheduler to print a Word document weekly so your printer ink won't dry out.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

679 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