Solved

Access 2013 - Help for functions

Posted on 2015-02-10
8
221 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
  • 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
 

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
A short article about problems I had with the new location API and permissions in Marshmallow
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

12 Experts available now in Live!

Get 1:1 Help Now