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!

Call a function within a function

Status
Not open for further replies.

roystreet

Programmer
Oct 12, 2000
146
US
Hello,
I have a function with an if statement. If a certain value is present, then I want this function to call another one in the same module. Is this possible & how?

Thanks,
~roystreet
 

How do you call the first one?
Why would it be any different?


Randy
 
Hello,
The first function is triggered by a call on a form. Once that function is called, then the function on in the mod will check another value located on the form & then direct it to the correct function in the mod.

Example:
On form, user inputs value "help" in field txtUserInput

if txtUserInput = "help" then
call mdo1.help(me)

end if
------------------In mod1-------------------
public function help(frm as form)

if frm.txtUserInput = "button" then
call mod1.buttonhelp

elseif frm.txtUserInput = "switch" then
call mod1.switch

end if

 
Whoops I meant mod1 to appear this way:
------------------In mod1-------------------
public function help(frm as form)

if frm.txtUserInput2 = "button" then
call mod1.buttonhelp

elseif frm.txtUserInput2 = "switch" then
call mod1.switch

end if

-I change it to frm.txtUserInput2
 
I get an error when I try this...

compile error:
Argument not optional


 
switch is a function in Access/VBA and should not be used as the name of your user-defined-function.

We can't see any of the other code so it's difficult to help further.

Duane
Hook'D on Access
MS Access MVP
 
Sorry about that...I was just using the word switch as an example in this case, not the actual situation. You may replace that word with any other useful word.

Thank you for that information though.


------------------In mod1-------------------
public function help(frm as form)

if frm.txtUserInput2 = "button" then
call mod1.buttonhelp

elseif frm.txtUserInput2 = "name" then
call mod1.namehelp

else:
frm.txtDisplay = "Command not found"

end if

--------------------------------
public function buttonhelp()
frm.txtDisplay = "This will display help on using buttons"
end function

public function namehelp()
frm.txtDisplay = "This will display help on using the right names"

end function
 
I don't believe your buttonhelp and namehelp functions know what "frm" is.

I don't care much for the hard-coding of the control names. I would send the form and control names/objects into each function that needs it.

I also don't think you need to use the "mod1." to call another function.

Duane
Hook'D on Access
MS Access MVP
 
As I have been testing these things, I haven't yet built the final code for this. I was trying to simplify it for you, but this isn't being effective I guess. "Mod1" isn't the name. My apologies.

dhookom = Please elaborate on what you are meaning by not hard coding control names, but would send names into each function that needs it. I may be using the wrong approach here.

What am I trying to do here? - Simple - User inputs text like "help-*" (This means they know at least part of what they are looking for), so send them to the function that will further lookup what they need. The helpgeneral code would give them further options on how to specify which help topics are available.
I will focus on when they use anything that beings with "help-" In the comSys module, it then looks at the statement more closely & will send it to the next appropriate function. I didn't want to place all of the code in the form or all in one function in the module.

Below, txtInput is where they will input there request. txtDisplay is simply where it will display the information concerning their request.

On the form:
Code:
Private Sub cmd_SubmitCmd_GotFocus()

    
'------------------------------Help System


If txtInput Like "help-*" Then
    'They at least started a specific help
    txtValueHold = "help"
    Call comSys.help(Me)
    Call LMNav.comSysCommdandEnd(Me)


ElseIf txtInput Like "HelpMe*" Then
    'Then they didnt specify type of help they wanted
    txtValueHold = "help"
    Call comSys.helpgeneral(Me)

Else:
    Call LMNav.comSysCommdandEnd(Me)
    txtDisplay = CurrentUser() & "  - That command isn't recognized" 
    txtValueHold = Null
    txtValueHold2 = Null
    

End If

Obviously it calls 2 modules here, but I will only address 1 as the 1st one works fine.

[bold]The comsys module[/bold]

Code:
Public Function help(frm As Form)

if frm.txtInput = "help-strength" then
	call helpstrength

elseif frm.txtInput = "help-purpose" then 
	call helppurpose

elseif frm.txtInput = "Help-export" then
	call helpexport

end if

public function helpstrength(frm as form)
	frm.txtDisplay = "This is help for strength command"

end function


public function helptpurpose(frm as form)
	frm.txtDisplay = "This is help concerning your purpose"

end function


public function helpexport(frm as form)
	frm.txtDisplay = "This is help on exporting"

end function

Hope this helps.
 
Please elaborate on what you are meaning by not hard coding control names, but would send names into each function that needs it. I may be using the wrong approach here.

Not a wrong approach just will make your code a little more flexible. You are already doing it with the frm, you could just pass in the textbox. Here is an example
Code:
Public Sub changeProperties(ctrl As Access.TextBox)
  'pass in an textbox object
  'A simple procedure that can change properties of any textbox in the database
   With ctrl
     .ForeColor = vbRed
     .BackColor = vbBlue
     .FontBold = True
     .FontItalic = True
  End With
End Sub

Public Sub testCP()
  changeProperties (Me.txtBox1)
  changeProperties (Me.txtBox2)
  changeProperties Forms("someOtherForm").txtbox3
End Sub

But if you pass a parameter into a function, you can pass it to another function and never even use it in the first function. Just pass it right out. Here is a dumb example to do (A+B) * C
Code:
public function myMath(A as double,B as Double, C as double)as double
  dim aPlusB as double
  'Never use it in the function, but Pass A and B back out
  aPlusB = getAplusB(A,B)
  myMath = aPlusB * C
end function

public function getAplusB(A as double, B as double)
  getAPlusB = A + B
end function

So your only problem is here
call helppurpose
needs to be
call helppurpose(frm)

because you have to pass the form back to the next function
public function helptpurpose(frm as form)
BTW you have a spelling error
 
This may exaggerate the concept even more.
Code:
Public Function func1(ByVal A As Double, ByVal B As Double, ByVal C As Double) As Double
  'do not do anything just pass it out
  func1 = func2(A, B, C)
End Function

Public Function func2(ByVal A As Double, ByVal B As Double, ByVal C As Double) As Double
  func2 = func3(A, B)
  func2 = func3(func2, C)
End Function

Public Function func3(ByVal X As Double, ByVal Y As Double) As Double
  func3 = X + Y
End Function

Public Sub testfunc1()
  Debug.Print func1(3, 4, 5)
End Sub
And the answer is? ....
 
If frm.txtInput = "help-strength" Then
Call helpstrength[!](frm)[/!]
ElseIf frm.txtInput = "help-purpose" Then
Call helppurpose[!](frm)[/!]
ElseIf frm.txtInput = "Help-export" Then
Call helpexport[!](frm)[/!]
End If

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
And to really complicate things the function can call itself over and over. Example

Code:
Public Function recursivePattern(strPattern As String, TotalIncrement As Integer, Optional ByVal currentIncrement As Integer = 0, Optional ByVal currentPattern = "") As String
  Do While currentIncrement < TotalIncrement
    currentIncrement = currentIncrement + 1
    currentPattern = currentPattern & strPattern
    Call recursivePattern(strPattern, TotalIncrement, currentIncrement, currentPattern)
 Loop
 recursivePattern = currentPattern
End Function

Public Sub testPattern()
  Debug.Print "Make Pattern: " & recursivePattern("ABC", 5)
End Sub
 
I have done something like this before but placed all the help in tables. One of the Help tables had a primary key of the combination of a FORM and CONTROL name. I had a generic function that opened a help form (bound to my tables) sending in the active form and control.

This solution removes all of the hard-coding of values and puts your data in tables where you can easily modify it without changing code.

Duane
Hook'D on Access
MS Access MVP
 
Hello everyone,
Thank you so much for the assistance & the detailed information - All of it helps. You gave me extra to chew on & broaden my knowledge (Here comes the experiments)

~roystreet
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top