Trying to get this macro to work...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Axll4716
    New Member
    • Jan 2014
    • 3

    Trying to get this macro to work...

    Here what I'm trying to make work, I'm using a Word document with several form fields.

    I created three fields, a checkbox called "checkbox", and two number fields called "initial" and "final". The user will enter a number in "initial". If they check checkbox", then "final" should be 9% of "initial", otherwise it should equal to 0.

    Now, what i have seen, because of the checkbox, there seem to no way to use a equation that would make this work within the formfield, at least none that i have tried so far. I read on a forum elsewhere that a macro could be ran to get the desired effect but my knowledge in VB isn't great.

    After a few attempts i got something to work on recognizing the box being checked or not, but i can't get the value in "final" to show the 9% that i want. All i keep getting is for it to 6.30 regardless of what i put in my "initial" field.

    Here's what i have so far

    Code:
    Dim Tax As Double
    Tax = 0.09
    If ActiveDocument.FormFields("CHECKBOX").CheckBox.Value = True Then
    ActiveDocument.FormFields("final").Result = ActiveDocument.FormFields("initial") * Tax
    Else
    ActiveDocument.FormFields("final").Result = 0
    End If
    End Sub
    It's probably something simple to crack but i don't know where to go next
    Last edited by Rabbit; Jan 9 '14, 09:25 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • abrokinla
    New Member
    • Jan 2014
    • 1

    #2
    From what I understand, you are trying to write a code for a program to find 9% of your "initial". First of all why not use a command button an d the if statement.
    Code:
    Private sub command1_click()
    Dim myans as integer 
    If checkbox.value= true then
    'Your answer
    Myans= 9% * initial
    Print myans
    End if
    End sub
    Last edited by Rabbit; Jan 10 '14, 03:27 AM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

    Comment

    • Axll4716
      New Member
      • Jan 2014
      • 3

      #3
      I've tried the code string suggested but it give me a compiling error and refuses to work :/

      Shouldn't my "final" and "checkbox" form field name be referenced somewhere in that code for the program to pick up where it need to put the answer?

      Not to forget that code is linked up to a word document, there may be restrictive limits that VB wouldn't have it was operating on it own to create that program

      Comment

      • Axll4716
        New Member
        • Jan 2014
        • 3

        #4
        Well after playing around with my code and doing more research i finally got it to work as wanted it to, here's the final product, maybe it can be of use to someone else in the future

        Code:
        Dim Tax1 As Single
        Dim Tax2 As Single
        Tax1 = 0.09
        Tax2 = 0.08
        If ActiveDocument.FormFields("QSTCHECK").CheckBox.Value = True Then
        ActiveDocument.FormFields("RECTAX").Result = Tax1 * ActiveDocument.FormFields("RECURRENTSDB").Result
        ActiveDocument.FormFields("FINTAX").Result = Tax1 * ActiveDocument.FormFields("FINSDB").Result
        ActiveDocument.FormFields("SALETAX").Result = Tax1 * ActiveDocument.FormFields("ADJSDBTOTAL").Result
        ElseIf ActiveDocument.FormFields("OSTCHECK").CheckBox.Value = True Then
        ActiveDocument.FormFields("RECTAX").Result = Tax2 * ActiveDocument.FormFields("RECURRENTSDB").Result
        ActiveDocument.FormFields("FINTAX").Result = Tax2 * ActiveDocument.FormFields("FINSDB").Result
        ActiveDocument.FormFields("SALETAX").Result = Tax2 * ActiveDocument.FormFields("ADJSDBTOTAL").Result
        Else
        ActiveDocument.FormFields("RECTAX").Result = 0
        ActiveDocument.FormFields("FINTAX").Result = 0
        ActiveDocument.FormFields("SALETAX").Result = 0
        End If
        End Sub
        Where QSTCHECK and OSTCHECK are my checkboxes,
        RECURRENTSDB, FINSDB and ADJSDBTOTAL are my data input field or "initial" as i had called them earlier
        RECTAX, FINTAX and SALETAX are my "final" boxes where the results will populate when the corresponding checkbox is checked.

        Comment

        Working...