User Function with input variable as

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • peter1234
    New Member
    • Nov 2009
    • 11

    User Function with input variable as

    In the example below I have chosen to define the input variable in a text box called Text_year. The function is run from a for with a Toggle Button. The text box in the same form. My purpose in to invite user to input a year in the text box to obtain the number of days in that year.

    The error message given reads:
    Control can't be edited ; it's bound to the expression 'DaysInYear([Text_year])

    The function:
    Code:
    Public Function DaysInYear (ByVal Year As Integer) As Integer
        
        Dim LeapDays As Integer
        LeapDays =0
        If Year Mod 4 = 0 Then LeapDays = 1
        DaysInYear = 365 + LeapDays
    End Function
    Any help will be greatly appreciated.
    Last edited by NeoPa; Nov 25 '09, 02:06 PM. Reason: Please use the [CODE] tags provided.
  • ajalwaysus
    Recognized Expert Contributor
    • Jul 2009
    • 266

    #2
    Are you allowing them to input the year in one field and showing the number of days in another, or are you trying to change the value in the same text box that they just entered the year in?

    This may be your problem, if that is the case. I don't think you can overwrite a value while that value is being used to calculate another value.

    I could be way off, but let us know.

    -AJ

    Comment

    • peter1234
      New Member
      • Nov 2009
      • 11

      #3
      Many thanks to ajalwaysus.
      I have a GetDaysInYear function linked to a second text box to extract the number of days in the year. The error message remains:
      The error message given reads:
      Control can't be edited ; it's bound to the expression 'DaysInYear([Text_year])

      Comment

      • ajalwaysus
        Recognized Expert Contributor
        • Jul 2009
        • 266

        #4
        Where have you set DaysInYear([Text_year]) in your text box. Did you put it under Control Source?

        Also, whether or not this is how you do it now, try to make sure it appears as =DaysInYear([Text_year]) in the control source.

        -AJ

        Comment

        • NeoPa
          Recognized Expert Moderator MVP
          • Oct 2006
          • 32633

          #5
          Peter, this sounds like you're not paying attention to what you're saying.

          Is there a GetDaysInYear() function? Or is it simply DaysInYear() as referred to elsewhere.

          Is the operator trying to enter data into the TextBox which has the formula in it (As both AJ and I seem to think)? or are they really entering into one which is unbound as you indicate?

          I have to say that the error message seems to contradict what you are saying. Please check your details and reply carefully.

          Comment

          • peter1234
            New Member
            • Nov 2009
            • 11

            #6
            Thank you for the responses. I have made some changes with no results.
            Code:
            Dim TotalDays as Integer
            
            Public Function DaysInYear (ByVal Year As Integer) As Integer 
              
                Dim LeapDays As Integer 
                LeapDays =0 
                If Year Mod 4 = 0 Then LeapDays = 1 
                TotalDays = 365 + LeapDays 
            End Function 
            
            Public Function GetDaysInYear () 
                  GetDaysInYear=    TotalDays
            End Function
            The control source for the button: DaysInYear([text_year])

            The text field [text_year] is left blank. I want to be able to enter values once the form is opened.

            In the control source of the second text box: GetReportTotal( )
            Last edited by Frinavale; Nov 25 '09, 07:15 PM. Reason: Please post code in [code] ... [/code] tags. Changed quote tags into code tags.

            Comment

            • missinglinq
              Recognized Expert Specialist
              • Nov 2006
              • 3533

              #7
              Despite NeoPa's post you continue to apparently really have no idea what it is you're trying to say! You have, once gain, thrown in a heretofore unmentioned function, GetReportTotal( ), with no explanation except that it is the Control Source for the "second" textbox. One would suppose, from your previous posts, that the second textbox was the one you wanted to populate with the number of days in the year entered in Text_Year control. If so, how can it have another function as its Control Source?

              You are simply going to have to give a better explanation of your scenario here, if you want us to help!

              Linq ;0)>

              Comment

              • ajalwaysus
                Recognized Expert Contributor
                • Jul 2009
                • 266

                #8
                Ok, like Linq said, we still don't know what you are doing. But I can tell you what to do to get the results you are looking for, what you need to do:
                1. Assume you have a text box named txtYear
                2. Create another text box and go to its properties and put "=DaysInYear([txtYear])" in the control source field. (no double quotes!)
                3. Keep this code with the tweak I put in:
                Code:
                Public Function DaysInYear (ByVal Year As Integer) As Integer
                
                Dim LeapDays As Integer
                LeapDays =0
                If Year Mod 4 = 0 Then LeapDays = 1
                DaysInYear = 365 + LeapDays
                End Function
                4. No need for the GetDaysInYear function or any buttons.

                What should happen now is that immediately after the user enters a year, the other text box will automatically display the number of days for you.

                Try this,
                -AJ

                Comment

                Working...