none
Me.Variables in subroutine decalartions RRS feed

  • Question

  • I tried to use a form variable, e.g. Me.test to get a value by means of a subroutine call, like e.g.

    call GiveTestAValue(Me.test)

    But this results, unexptected, always in the value 0 for Me.Test.

    It goes correctly in the following way

    Dim I

    call GiveTestAValue(I)

    Me.test=I

    Is there a reason why a form variable may not be used in the subroutine call?

    With kind regards

    Jan Lichtenbelt

    Thursday, September 28, 2017 10:05 AM

Answers

  • All of you, thanks for help.

    My problem is that I get contradictional results in the subroutine I real use with 2 variables, something like:

    call GiveTestAValue(Me.test1,Me.test2)

    And Test1 returns the correct data, while tests stays always zero. I have to do more suveys to find out what is wrong.

    I stop this problem now.

    Regards

    Jan Lichtenbelt

    Friday, September 29, 2017 12:41 PM

All replies

  • You can use a Variable if it's an actual Variable. If it is, then you don't preface it with Me. Just use the Variable name:

    GiveTestAValue(Test)

    Does GiveTestAValue return the value into that variable? Or does it do something else? If it returns a value, you may need to pass in your Me.Test Form Control reference differently - or change it to a Function, which would be the preferred method if that routine returns a value.



    -- Scott McDaniel, Microsoft Access MVP

    Thursday, September 28, 2017 10:11 AM
  • call GiveTestAValue(Me.test)

    Hi Jan,

    Did you declare the variable Test as ByVal? In that case no values are returned.

    You can try to call the routine as:     GiveTestrAValue Me!Test,   or  GiveTestAValue Test.

    Surrounding a parameter with brackets means that the function-result of that parameter is passed, and not the variable itself.

    Imb.

    Thursday, September 28, 2017 10:32 AM
  • How is the argument declared in your function's declaration?  If it is declared as anything other than an object type or Variant, then the control's value will be converted to a temporary variable of that type, and that temporary variable will be passed to the function, rather than the control itself.  In that case, changing the value of the argument inside the function will have no effect on the control.

    Dirk Goldgar, MS Access MVP
    Access tips: www.datagnostics.com/tips.html

    Thursday, September 28, 2017 2:15 PM
  • I'm not quite sure what you are attempting, but here's a simple example, using a sub-procedure, which will assign a value to a module level variable:

    Option Compare Database
    Option Explicit

    Dim Test As Integer

    Private Sub GiveTestAValue(i As Integer)

        i = Day(VBA.Date)

    End Sub

    Private Sub cmdTest_Click()

        GiveTestAValue Test
        
        Debug.Print Test
        
    End Sub

    Today, 28th September,  this returns 28 in the debug (aka 'immediate') window.  I think most people would use a function to to return a value though:

    Option Compare Database
    Option Explicit

    Dim Test As Integer

    Private Function GiveTestAValue() As Integer

        GiveTestAValue = Day(VBA.Date)
        
    End Function

    Private Sub cmdTest_Click()

        Test = GiveTestAValue()
        
        Debug.Print Test
        
    End Sub

    Ken Sheridan, Stafford, England

    Thursday, September 28, 2017 4:31 PM
  • All of you, thanks for help.

    My problem is that I get contradictional results in the subroutine I real use with 2 variables, something like:

    call GiveTestAValue(Me.test1,Me.test2)

    And Test1 returns the correct data, while tests stays always zero. I have to do more suveys to find out what is wrong.

    I stop this problem now.

    Regards

    Jan Lichtenbelt

    Friday, September 29, 2017 12:41 PM
  • call GiveTestAValue(Me.test1,Me.test2)

    Hi Jan,

    And what if you just use:

            GiveTestAValue Me!test1, Me!tyest2 ?

    Groeten, Imb.

    Friday, September 29, 2017 12:58 PM