|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Best way to have a subroutine modify more than one variablethat affect individual record in the database. One of the functions involves a bunch of calculations that results in new values to several fields in that database (straight average, rolling average, weighted average, etc.). I would like to put all of these calculations into a subroutine, but I am not sure of the best way to code it. If only one value was getting updated, I would call the program call the subroutine, pass it whatever values it needs, and have it return the new value, which the calling program would then store in the database. In this situation, several fields need to be updated. I know I can pass the database record set itself and let the subroutine do the updates, but I prefer to keep the new database values in variables and write them out all at once at the end in one place. So, I could pass the field variables ByRef and then any changes the subroutine makes would affect the actual variables. This seems "dirty" to me. Is there a better way? Thanks -- For email, use Usenet-20031220@spamex.com "LurfysMa" <invalid@invalid.invalid> wrote in message I like to pass classes around. A class can keep the sub from directly news:orrnk194frfa7odm17k2s8v35ntd878ahq@4ax.com... >I have a program that manages a database. It responds to user actions > that affect individual record in the database. One of the functions > involves a bunch of calculations that results in new values to several > fields in that database (straight average, rolling average, weighted > average, etc.). > > I would like to put all of these calculations into a subroutine, but I > am not sure of the best way to code it. modifying the data if required. Like anything else, there are dozens of options. You can pass arrays, set up a function to return arrays, use ParamArray to send a variable number of arguments, etc, etc. Passing a class is pretty straight forward.... You can even code it as a function that returns a class. That class can be PublicNotCreatable if you want to control who's creating who. -- Ken Halter - MS-MVP-VB - http://www.vbsight.com DLL Hell problems? Try ComGuard - http://www.vbsight.com/ComGuard.htm Please keep all discussions in the groups.. 2 easy ways:
1 - ByRef: Public Function GetResults(ByRef p1 As Double, ByRef p2 As Double) As Double 2 - Using a UDF: Public Type MyResultsT p1 As Double p2 As Double p3 As Double End Type Public Function GetResults() As MyResultsT Dim ret As MyResultsT ret.p1 = 1 ret.p2 = 2 ret.p3 = 3 GetResults = ret End Function How to call: Private Sub Form_Load() Dim ret As MyResultsT ret = GetResults2() Debug.Print ret.p1, ret.p2, ret.p3 End Sub The UDF method will also work in the same way if you converted it to a class. This depends on your need. Show quoteHide quote "LurfysMa" <invalid@invalid.invalid> wrote in message news:orrnk194frfa7odm17k2s8v35ntd878ahq@4ax.com... >I have a program that manages a database. It responds to user actions > that affect individual record in the database. One of the functions > involves a bunch of calculations that results in new values to several > fields in that database (straight average, rolling average, weighted > average, etc.). > > I would like to put all of these calculations into a subroutine, but I > am not sure of the best way to code it. > > If only one value was getting updated, I would call the program call > the subroutine, pass it whatever values it needs, and have it return > the new value, which the calling program would then store in the > database. > > In this situation, several fields need to be updated. I know I can > pass the database record set itself and let the subroutine do the > updates, but I prefer to keep the new database values in variables and > write them out all at once at the end in one place. > > So, I could pass the field variables ByRef and then any changes the > subroutine makes would affect the actual variables. This seems "dirty" > to me. Is there a better way? > > Thanks > > > -- > For email, use Usenet-20031220@spamex.com |
|||||||||||||||||||||||