Solved

Access 2013 - Help for functions

Posted on 2015-02-10
8
231 Views
Last Modified: 2015-02-10
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.
0
Comment
Question by:jegajothy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:FarWest
ID: 40601000
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
0
 

Author Comment

by:jegajothy
ID: 40601005
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.
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40601017
check this

Public Function MakeMeCapital(anystr As String) As String

    MakeMeCapital= UCase(anystr )

End Function
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:jegajothy
ID: 40601255
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.
0
 
LVL 12

Expert Comment

by:FarWest
ID: 40601283
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

0
 

Author Comment

by:jegajothy
ID: 40601408
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
0
 
LVL 12

Accepted Solution

by:
FarWest earned 500 total points
ID: 40601439
I just have to leave my computer now
but to get value from any function either use the function name directly or assign a variable to it
example

dim ss as string
ss = MakeMeCapital("asjjs")

or
textbox1.text = MakeMeCapital("asjjs")

value or variable inside the function parentheses ("asjjs") is input parameter (unless used by reference which is not our case now)
0
 

Author Closing Comment

by:jegajothy
ID: 40601594
thank u for the solution.
0

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

752 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