Posts by Glyn Roberts

    Re: Difference between DAO, ADO, and ODBC


    Quote from gweasley;143774

    Hi Barry,


    Thanks for the info! Yes, I understand the concept behind it and how it works on the back end, but I suppose what I need to see is an actual example of why you would use one versus the other. Gollem tried to explain it but I think I need a more specific and detailed VBA example on how and when you would use one against the other, because I really am having a difficult time understanding this :?


    Thanks!


    What I do is use ByVal as my default method because it means that I can change (either deliberately or accidentally) the contents of a variable passed to a function or subroutine WITHOUT there being a possibility that I can change the original value. Normally when you pass a variable to a routine you do NOT want that routine to be able to change (ie corrupt) the original variable.
    If you want to be able to change the original value then use a function and set the calling variable to the results of that function eg


    Function RoundWages (ByVal Amount as currency, ByVal Method as String) as Currency
    .....
    End Function


    Sub Use_it
    Dim Wages as Currency
    Wages=1567.78
    Wages = RoundWages(Wages,"Weekly")
    End Sub


    Doing it this way, it is obvious that the variable Wages, which is a parameter to the function, is also being changed by calling the function


    Sometimes you require a function which returns MORE than the one variable so then and ONLY then I use the ByRef call and I usually do it using a subroutine and not a function call and HIGHLIGHT the fact in my code to remind me that it's an exception.


    Sub RoundWages (ByRef Amount as currency, ByVal Method as String, ByRef WeeklyWages as Currency, ByRef MonthlyWages as Currency, ByRef YearlyWages as Currency)
    ' Note that RoundWages changes the values used to call it in order to return multiple values
    .....
    End Function


    Sub Use_it
    Dim Wages as Currency, WeeklyWages as Currency, MonthlyWages as Currency, YearlyWages as Currency
    Wages=1567.78
    RoundWages Wages,"Weekly", WeeklyWages, MonthlyWages, YearlyWages ' RoundWages Routine returns multiple values
    End Sub


    In my case I don't care whether one method is faster or uses less memory - I only care about my sanity one purpose of object orientation is to encapsulate the code inside a routine so that it is not affected by, nor can it affect, anything outside itself EXCEPT where it is specifically set up to occur.