MS Access: Get value from string












0















I am trying to get this VBA code to run after a user inputs data into a form. The script will calculate the charge which equals before-after



I am defining the name of the textbox who values i want to use (Ex:"before_1,after_1,charge_1) as a string and then use those values in the CalCharge Sub.



Any ideas why this code does not work? I believe this method works in C#, but not in VBA.



Private Sub before_1_AfterUpdate()
before = "before_1"
after = "after_1"
charge = "charge_1"
Call CalCharge(before, after, charge)
End Sub

Private Sub before_2_AfterUpdate()
before = "before_2"
after = "after_2"
charge = "charge_2"
Call CalCharge(before, after, charge)
End Sub



Sub CalCharge(before, after, charge)
charge.Value = before.Value - after.Value
Exit Sub
End Sub









share|improve this question



























    0















    I am trying to get this VBA code to run after a user inputs data into a form. The script will calculate the charge which equals before-after



    I am defining the name of the textbox who values i want to use (Ex:"before_1,after_1,charge_1) as a string and then use those values in the CalCharge Sub.



    Any ideas why this code does not work? I believe this method works in C#, but not in VBA.



    Private Sub before_1_AfterUpdate()
    before = "before_1"
    after = "after_1"
    charge = "charge_1"
    Call CalCharge(before, after, charge)
    End Sub

    Private Sub before_2_AfterUpdate()
    before = "before_2"
    after = "after_2"
    charge = "charge_2"
    Call CalCharge(before, after, charge)
    End Sub



    Sub CalCharge(before, after, charge)
    charge.Value = before.Value - after.Value
    Exit Sub
    End Sub









    share|improve this question

























      0












      0








      0








      I am trying to get this VBA code to run after a user inputs data into a form. The script will calculate the charge which equals before-after



      I am defining the name of the textbox who values i want to use (Ex:"before_1,after_1,charge_1) as a string and then use those values in the CalCharge Sub.



      Any ideas why this code does not work? I believe this method works in C#, but not in VBA.



      Private Sub before_1_AfterUpdate()
      before = "before_1"
      after = "after_1"
      charge = "charge_1"
      Call CalCharge(before, after, charge)
      End Sub

      Private Sub before_2_AfterUpdate()
      before = "before_2"
      after = "after_2"
      charge = "charge_2"
      Call CalCharge(before, after, charge)
      End Sub



      Sub CalCharge(before, after, charge)
      charge.Value = before.Value - after.Value
      Exit Sub
      End Sub









      share|improve this question














      I am trying to get this VBA code to run after a user inputs data into a form. The script will calculate the charge which equals before-after



      I am defining the name of the textbox who values i want to use (Ex:"before_1,after_1,charge_1) as a string and then use those values in the CalCharge Sub.



      Any ideas why this code does not work? I believe this method works in C#, but not in VBA.



      Private Sub before_1_AfterUpdate()
      before = "before_1"
      after = "after_1"
      charge = "charge_1"
      Call CalCharge(before, after, charge)
      End Sub

      Private Sub before_2_AfterUpdate()
      before = "before_2"
      after = "after_2"
      charge = "charge_2"
      Call CalCharge(before, after, charge)
      End Sub



      Sub CalCharge(before, after, charge)
      charge.Value = before.Value - after.Value
      Exit Sub
      End Sub






      excel vba ms-access






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 13 '18 at 17:42









      JRob23123JRob23123

      223




      223
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Regardless of the fact that you are using undimensioned variables, I would say that the problem is that you are not referring to the controls in CalCharge.



          Try to use this:



          Private Sub before_1_AfterUpdate()
          Call CalCharge(1)
          End Sub

          Private Sub before_2_AfterUpdate()
          Call CalCharge(2)
          End Sub

          Private Sub CalCharge(ByVal index As Long)
          Me("charge_" & index).Value = Me("before_" & index).Value - Me("after_" & index).Value
          End Sub


          I restructured it a bit:
          CalCharge awaits a numeric parameter now, defining the 'set' of controls to be used by an index.



          Your event procedures before_1_AfterUpdate() and before_2_AfterUpdate() now just call this new CalCharge procedure by providing the index.



          CalCharge now references the controls by building the control names first.



          Me is a reference to current object instance, your form, where the current code is in.






          share|improve this answer


























          • Thank you, I just added Me in front of everything and it worked. I guess it like "Self" in C#.

            – JRob23123
            Nov 13 '18 at 19:04











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286718%2fms-access-get-value-from-string%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Regardless of the fact that you are using undimensioned variables, I would say that the problem is that you are not referring to the controls in CalCharge.



          Try to use this:



          Private Sub before_1_AfterUpdate()
          Call CalCharge(1)
          End Sub

          Private Sub before_2_AfterUpdate()
          Call CalCharge(2)
          End Sub

          Private Sub CalCharge(ByVal index As Long)
          Me("charge_" & index).Value = Me("before_" & index).Value - Me("after_" & index).Value
          End Sub


          I restructured it a bit:
          CalCharge awaits a numeric parameter now, defining the 'set' of controls to be used by an index.



          Your event procedures before_1_AfterUpdate() and before_2_AfterUpdate() now just call this new CalCharge procedure by providing the index.



          CalCharge now references the controls by building the control names first.



          Me is a reference to current object instance, your form, where the current code is in.






          share|improve this answer


























          • Thank you, I just added Me in front of everything and it worked. I guess it like "Self" in C#.

            – JRob23123
            Nov 13 '18 at 19:04
















          0














          Regardless of the fact that you are using undimensioned variables, I would say that the problem is that you are not referring to the controls in CalCharge.



          Try to use this:



          Private Sub before_1_AfterUpdate()
          Call CalCharge(1)
          End Sub

          Private Sub before_2_AfterUpdate()
          Call CalCharge(2)
          End Sub

          Private Sub CalCharge(ByVal index As Long)
          Me("charge_" & index).Value = Me("before_" & index).Value - Me("after_" & index).Value
          End Sub


          I restructured it a bit:
          CalCharge awaits a numeric parameter now, defining the 'set' of controls to be used by an index.



          Your event procedures before_1_AfterUpdate() and before_2_AfterUpdate() now just call this new CalCharge procedure by providing the index.



          CalCharge now references the controls by building the control names first.



          Me is a reference to current object instance, your form, where the current code is in.






          share|improve this answer


























          • Thank you, I just added Me in front of everything and it worked. I guess it like "Self" in C#.

            – JRob23123
            Nov 13 '18 at 19:04














          0












          0








          0







          Regardless of the fact that you are using undimensioned variables, I would say that the problem is that you are not referring to the controls in CalCharge.



          Try to use this:



          Private Sub before_1_AfterUpdate()
          Call CalCharge(1)
          End Sub

          Private Sub before_2_AfterUpdate()
          Call CalCharge(2)
          End Sub

          Private Sub CalCharge(ByVal index As Long)
          Me("charge_" & index).Value = Me("before_" & index).Value - Me("after_" & index).Value
          End Sub


          I restructured it a bit:
          CalCharge awaits a numeric parameter now, defining the 'set' of controls to be used by an index.



          Your event procedures before_1_AfterUpdate() and before_2_AfterUpdate() now just call this new CalCharge procedure by providing the index.



          CalCharge now references the controls by building the control names first.



          Me is a reference to current object instance, your form, where the current code is in.






          share|improve this answer















          Regardless of the fact that you are using undimensioned variables, I would say that the problem is that you are not referring to the controls in CalCharge.



          Try to use this:



          Private Sub before_1_AfterUpdate()
          Call CalCharge(1)
          End Sub

          Private Sub before_2_AfterUpdate()
          Call CalCharge(2)
          End Sub

          Private Sub CalCharge(ByVal index As Long)
          Me("charge_" & index).Value = Me("before_" & index).Value - Me("after_" & index).Value
          End Sub


          I restructured it a bit:
          CalCharge awaits a numeric parameter now, defining the 'set' of controls to be used by an index.



          Your event procedures before_1_AfterUpdate() and before_2_AfterUpdate() now just call this new CalCharge procedure by providing the index.



          CalCharge now references the controls by building the control names first.



          Me is a reference to current object instance, your form, where the current code is in.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 18:33

























          answered Nov 13 '18 at 18:21









          Unhandled ExceptionUnhandled Exception

          975159




          975159













          • Thank you, I just added Me in front of everything and it worked. I guess it like "Self" in C#.

            – JRob23123
            Nov 13 '18 at 19:04



















          • Thank you, I just added Me in front of everything and it worked. I guess it like "Self" in C#.

            – JRob23123
            Nov 13 '18 at 19:04

















          Thank you, I just added Me in front of everything and it worked. I guess it like "Self" in C#.

          – JRob23123
          Nov 13 '18 at 19:04





          Thank you, I just added Me in front of everything and it worked. I guess it like "Self" in C#.

          – JRob23123
          Nov 13 '18 at 19:04


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286718%2fms-access-get-value-from-string%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Xamarin.form Move up view when keyboard appear

          JBPM : POST request for execute process go wrong