Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving Result Of A Function 1

Status
Not open for further replies.

OrWolf

MIS
Mar 19, 2001
291
I want to call the same function repeatedly from different forms, however I can't figure out how to retrieve the resulting data. Any ideas?
 
Need more information. What does the funtion do? ljprodev@yahoo.com
Professional Development
MS Access Applications
 
The function needs to be placed in a module. You then call it by assigning something to its value, for example:

myvariable = functionname(parameter1, parameter2) Mike Rohde
rohdem@marshallengines.com
 
Thanks Rodhem, I thought I was on the right path, but there must be something wrong with my code. I always get nothing back as the result. Below shows my sub and function. Can you see the problem?

Sum in 'Test' form
Sub TestField_AfterUpdate()
Dim InputVal As String
InputVal = FindSpecialChar(Me.TestField)
MsgBox "The new value is: " & InputVal
End Sub

Function in 'GeneralMod' module
Public Function FindSpecialChar(InputVal As String)
Dim BadCharPos As Integer, NewValue As String, SpecialChar As String
Dim ExitBtn As Variant
SpecialChar = " "
Step = 0
Do Until SpecialChar = ""
Step = Step + 1
Select Case Step
Case 1
SpecialChar = "#"
Case Else
SpecialChar = ""
End Select
If SpecialChar = "" Then
GoTo ExitFunc
End If

Do
BadCharPos = InStr(1, InputVal, SpecialChar)
If BadCharPos > 0 Then
Mid(InputVal, BadCharPos) = " "
End If
Loop Until BadCharPos = 0
Loop
ExitFunc:
End Function
 
Somewhere in your function, you have to assign a value to 'FindSpecialChar'. This is the value that is returned when the function is finished running. Mike Rohde
rohdem@marshallengines.com
 
I added 'FindSpecialChar = InputVal' after the final loop of the function. Shouldn't 'InputVal' in the sub then equal the result? I tried running function with messages throughout and have the correct value until the message in the sub returns null.

Any ideas?????
 
Is your 'FindSpecialChar = InputVal' before or after your ExitFunc: label?

When you call the Goto ExitFunc, you might be skipping over the assignment statement.

I would use the debugger to test your code and watch your variables. Place a breakpoint on the first line of the function (click the grey bar to the left of the line, the line should turn red). Now when the code executes, it will stop on this line. Now resize the code window so it is on the top half of the screen. Under the 'Debug' menu, click 'Add Watch'. Type in 'InputVal' Repeat this for other variables such as FindSpecialChar. Size the 'watch' window so it is at the bottom of the screen. Now click on the code window and press the F8 key. It will move to the next line of code (highlighted in yellow). Keep pressing F8 and watch how the values in your variables change as you step through the code. You should be able to determine what the problem is.

Good Luck
Mike Rohde
rohdem@marshallengines.com
 
I finally got it to work with the following code in the sub. I needed to call the function then just reference the value set in the function. Thanks for the help.

Sub TestField_AfterUpdate()
Dim InputVal As String
InputVal = Me.TestField
Call FindSpecialChar(InputVal)
Msgbox InputVal
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top