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!

Basic question about passing parameters

Status
Not open for further replies.

agc1976

Programmer
May 2, 2001
36
PR
I have 3 Subs... let's call them Parent, Child1, Child2. I want the Child2 sub to be able to read the values that Child1 assigned without declaring the variables in the general section.

Child2 prints wrong values. What am I doing wrong?

It looks something like this:

Private Sub Parent()
Dim P1 as String
Dim P2 as String

Child1 (P1, P2)
Child2 (P1, P2)
End Sub

Private Sub Child1(P1 As String, P2 As String)
P1 = "1"
P2 = "2"
End Sub

Private Sub Child2(P1 As String, P2 As String)
Print P1, P2
End Sub
 
You must pass the Child1 values to the Child2 sub somehow. One way is to call the Child2 sub from Child1 passing the values that way:

Private Sub Child1(P1 As String, P2 As String)
P1 = "1"
P2 = "2"

Call Child2(P1, P2)

End Sub

 
Thanks but I can't call it from Child1. I know there's a way to share the values between subs but I can't find out how.
 
The only way I know of is through globally defined variables. What are you trying to do? What is the purpose of the variables?
 
Child1 is a sub that imports a text file into a table.

Child2 processes the table and generates another table from the records that Child1 extracted.

I can do it with global variables, in fact, it's already working like that. It's just that I don't like to have global variables if they're not strictly necessary. Guess I'm just trying to follow good programming practices ;)

If you look at Access help and search for "ByRef" you'll see there's a way to do what I'm trying to do. But the help file doesn't help much explaining it.
 
I still don't understand. What prevents you from calling the child2 subroutine from child1 when it completes transfer of the file? Also, whether you pass a parameter ByVal or ByRef you are still passing the parameter. The primary difference between the two is the physical storage of the value(this is probably what you're alluding to). Since ByRef basically passes the memory address of the parameter it is possible to change it's value and have it reflected back to the calling procedure. But in any case, you are passing the value(either as a value or as a reference to it's location in memory) to the function or subroutine being called.
 
>>I still don't understand. What prevents you from calling the child2 subrouting from child1 when it completes transfer of the file?<<

I want to be able to execute child1 without executing child2 (possible future requirements.

>>Since ByRef basically passes the memory address of the parameter it is possible to change it's value and have it reflected back to the calling procedure.<<

That's EXACTLY what I'm trying to do, be able to read the values of the variables in the Parent procedure... but it doesn't work...
 
Note what I said. A change can be reflected back to the CALLING procedure. So, if you're calling child2 from parent and pass the value by reference any changes made to the value by child2 may show up in parent. It doesn't necessarily work going forward. Even if you're planning on calling the procedures independently the value must be passed to them somehow. This can be done directly(either ByVal or ByRef) or indirectly via a global variable. You just can't expect to call one child procedure and expect it to be able to use a variable from another procedure. I hope I'm understanding you and that you are able to understand me.
 
Well, I ran my code step by step as I showed you in my original post and noted that the parent procedure never saw the changes that child1 made to the variables. I tried adding &quot;ByRef&quot; to the variables in the child procedure but had no success either.

Well, I'm out... I appreciate your help... I'll keep trying to see what I find out. Thanks : )
 
As the code is written in the orignal post, your variables are completely independent of each other. In order to have values transferred you must pass them when the subroutine is called. Since the parent is the calling routine, whatever values existed in P1 and P2 at the time the routine is called is the value that gets passed. Keep in mind, child1 does not necessarily have to finish before child2 is called and executed. In fact, they will probably occur almost simultaneously. Access is event driven, with the exception of dialog boxes and modal forms code is executed pretty much sequentially without regard to what occurs after execution. Unless coded to do so, Access will not wait before attempting to execute the call to child2 after child1.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top