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

asked on

Access 2013 - Help for functions

My OS is win 7 prof 64 bit and i use Office 365 and Access 2013.
in Access 2013, I have a Form, which has the following code under a command button :

Private Sub Command29_Click()
      Dim torandom As String
      torandom = "1234TqRi*(Ka"
      MsgBox "this is what the string to randomize looks like before being randomized : " & torandom
      MsgBox (RandomiseString("1234TqRi*(Ka"))
      Debug.Print (RandomiseString("1234TqRi*(Ka"))
End Sub

The Function, RandomiseString is created in the Module, and it is as follows :

Public Function RandomiseString(inputString) As String
'-start

Dim mystr As String
Dim myStrLen As Integer
Dim random_number As Integer


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

glbconverted = resultstring
return glbconverted
MsgBox "This is INSIDE the function of the glbconverted" & glbconverted
'---END
End Function

i would like to return the result from the function to the calling Procedure, but i do not know how to do it.  And also where do I declare the variables in the Function.  I tried in the Module, but the procedure does not see it.  So looks like i am not doing things right.

Please let me know where I should  create the variable that will have its scope in the entire access program, and what is the syntax.

how does the function, return the result to a variable, that will be visible outside this function?

As i was experimenting, thus, please correct my code as necessary.  Thank u for your advice.
Avatar of FarWest
FarWest

commenting this line
'RandomiseString = resultstring
makes the function without return value
you should assign value to function name like above to return a value to caller
Avatar of Jegajothy vythilingam

ASKER

In response to FarWest.  Can u please give me the syntax to call the Function and to return the processed value to a variable, which I can use in the calling module.  thank u.
check this

Public Function MakeMeCapital(anystr As String) As String

    MakeMeCapital= UCase(anystr )

End Function
in response to FarWest, sorry for being so basic, but can u pse show me the syntax for assigning a variable to call this function from the body of the Form, and then I could use the result somewhere else within the same form.
Thank u.
you mean like this, you just define your variable out of any sub of function
like this
Dim nn As String

Public Sub test1()
nn = MakeMeCapital("asjjs")
test2
End Sub

Public Function MakeMeCapital(anystr As String) As String

    MakeMeCapital = UCase(anystr)

End Function

Public Sub test2()
MsgBox (nn)
End Sub

Open in new window

In response to Far West, i hope u do not mind if u can please take a look at the code and see where I am going wrong.  the command button is called : Generate.  and the code is attached.  Hope u can please comment on the code where the errors are or best practices could be used.  Thank u
ERROR-IN-CODE.txt
ASKER CERTIFIED SOLUTION
Avatar of FarWest
FarWest

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
thank u for the solution.