Solved

Access 2013 - Help for functions

Posted on 2015-02-10
8
224 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

929 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

18 Experts available now in Live!

Get 1:1 Help Now