Jump to content

Need help debugging _arrayadd function


Recommended Posts

Hi all, 

First, I want to give a huge shout-out to the community. I'm completely self-taught, and have never had to actually ask a question before because the forum is that good at answering questions and explaining things. However, I'm kind of stumped here, and I've been stuck on this problem for almost a full day.

I'm working on a script to populate drafts of deeds at work. I have the main GUI and  a function (ctrl($n) and read()) for adding fields to find and data to replace it with to an array for later use with _word_docfindreplace. All of that works. However, due to the way I have the forms set up, I need to create additional fields and info based on the data that's there. Specifically, if there's only one buyer, I need to add the field [Buyer1&2] and the data in $aArray_Base for [Buyer 1]. I also need to add a field [Buyer 2] and have a blank data set in the next column over in the array, and I need to do the same for the Seller. To this end, the function parties() sets boolean variables $2buyers and $2sellers accordingly. Then, I have buyers() and sellers() to populate the data. 

The problem that I'm running into is that each function works... when ONLY the buyer 1 name field is filled, and when ONLY the seller 1 field is filled.

So if I fill Buyer 1 Name and save it, the data is populated correctly. But when I fill Buyer 1 and Seller 1 name, only the buyer 1 data populates correctly. Worse, when I fill several fields, neither populate correctly. I have no idea why this happens. I've added messageboxes to debug throughout the entire process and can't pinpoint what's causing the issue. The entire script is below. The function(s) in question are buyers() and sellers(). Only Sellers() has messageboxes throughout.

Can someone help walk me through what might be causing this and help me find a solution? Thanks a ton in advance, and sorry for the wall of text.

-Anoig

Spoiler
#include <ButtonConstants.au3> ;AutoIT plugins
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <TabConstants.au3>
#include <WindowsConstants.au3>
#include <Array.au3>
#include <MsgBoxConstants.au3>
#include <Word.au3>
#include <ComboConstants.au3>
#Region ### START Koda GUI section ### Form=C:\Users\Randy\Downloads\koda_1.7.3.0\Forms\KNDS.kxf
$KNDS = GUICreate("KNDS DocPrep Wizard", 727, 507, 540, 193)
$MainPage = GUICtrlCreateTab(8, 40, 708, 424)
$TabSheet1 = GUICtrlCreateTabItem("Property")
$Property = GUICtrlCreateGroup("Legal Description", 32, 80, 657, 153)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$InputLegal = GUICtrlCreateEdit("", 40, 96, 641, 129, $ES_WANTRETURN, 0)
GUICtrlSetTip(-1, "Full legal description from Schedule A of Title Commitment")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Buyer = GUICtrlCreateTabItem("Buyer")
$Buyer1Group = GUICtrlCreateGroup("Buyer 1", 24, 72, 329, 185)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$inputbuyer1 = GUICtrlCreateInput("", 120, 104, 217, 22)
GUICtrlSetTip(-1, "Bob Buyer, LLC")
$Buyersingle = GUICtrlCreateRadio("Single", 32, 136, 57, 17)
$BuyerMarried = GUICtrlCreateRadio("Married", 32, 160, 65, 17)
$BuyerLLC = GUICtrlCreateRadio("LLC", 32, 184, 57, 17)
$BuyerInc = GUICtrlCreateRadio("Inc.", 32, 208, 49, 17)
$inputbuyerentitystate = GUICtrlCreateInput("", 272, 208, 73, 22)
GUICtrlSetTip(-1, "Write out state name, e.g. Texas")
$Buyer1label = GUICtrlCreateLabel("Buyer 1 Name:", 32, 104, 75, 18)
$labelbuyerentitystate = GUICtrlCreateLabel("State (only if LLC or Inc.):", 136, 208, 127, 18)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Buyer2Group = GUICtrlCreateGroup("Buyer 2", 368, 72, 321, 185)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Buyer2label = GUICtrlCreateLabel("Buyer 2 Name:", 376, 104, 75, 18)
$InputBuyer2 = GUICtrlCreateInput("", 464, 104, 209, 22)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$GroupBuyAdd = GUICtrlCreateGroup("Buyer's Address", 24, 264, 673, 185)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$labelbuyadd1 = GUICtrlCreateLabel("Address:", 32, 304, 49, 18)
$labelbuyadd2 = GUICtrlCreateLabel("City, State, Zip:", 32, 336, 81, 18)
$labelbuycounty = GUICtrlCreateLabel("County:", 32, 368, 41, 18)
$InputBuyerAdd1 = GUICtrlCreateInput("", 120, 304, 561, 22)
GUICtrlSetTip(-1, "123 Main Street")
$InputBuyerAdd2 = GUICtrlCreateInput("", 120, 336, 561, 22)
GUICtrlSetTip(-1, "City, State Zip")
$InputBuyerAdd3 = GUICtrlCreateInput("", 120, 368, 561, 22)
GUICtrlSetTip(-1, "County")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Seller = GUICtrlCreateTabItem("Seller")
$Group1 = GUICtrlCreateGroup("Seller 1", 26, 73, 329, 185)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$InputSeller1 = GUICtrlCreateInput("", 122, 105, 217, 22)
GUICtrlSetTip(-1, "Sally the Seller, LLC")
$Labelseller1 = GUICtrlCreateLabel("Seller 1 Name:", 34, 105, 73, 18)
$SellerMarried = GUICtrlCreateRadio("Married", 34, 161, 65, 17)
$SellerSingle = GUICtrlCreateRadio("Single", 34, 137, 57, 17)
$SellerLLC = GUICtrlCreateRadio("LLC", 34, 185, 57, 17)
$SellerInc = GUICtrlCreateRadio("Inc.", 34, 209, 49, 17)
$Label2 = GUICtrlCreateLabel("State (only if LLC or Inc.):", 138, 209, 127, 18)
$InputSellerEntityState = GUICtrlCreateInput("", 274, 209, 73, 22)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group2 = GUICtrlCreateGroup("Seller 2", 372, 73, 321, 185)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Label3 = GUICtrlCreateLabel("Seller 2 Name:", 380, 105, 73, 18)
$InputSeller2 = GUICtrlCreateInput("", 468, 105, 209, 22)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group3 = GUICtrlCreateGroup("Seller's Address", 26, 265, 673, 185)
GUICtrlSetFont(-1, 8, 400, 0, "Arial")
$Label4 = GUICtrlCreateLabel("Address:", 34, 305, 49, 18)
$Label5 = GUICtrlCreateLabel("City, State Zip:", 34, 337, 81, 18)
$Label6 = GUICtrlCreateLabel("County:", 34, 369, 41, 18)
$InputSellerAdd1 = GUICtrlCreateInput("", 122, 305, 561, 22)
GUICtrlSetTip(-1, "123 Main Street")
$InputSellerAdd2 = GUICtrlCreateInput("", 122, 337, 561, 22)
GUICtrlSetTip(-1, "City, State Zip")
$InputSellerAdd3 = GUICtrlCreateInput("", 122, 369, 561, 22)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Lender = GUICtrlCreateTabItem("Lender")
GUICtrlSetState(-1,$GUI_SHOW)
$Group5 = GUICtrlCreateGroup("Lender Info", 19, 73, 393, 161)
$Label7 = GUICtrlCreateLabel("Address:", 27, 129, 45, 17)
$Label8 = GUICtrlCreateLabel("City, State, Zip:", 27, 161, 81, 18)
$Label9 = GUICtrlCreateLabel("County:", 27, 193, 40, 17)
$InputLenderAdd1 = GUICtrlCreateInput("", 115, 129, 281, 21)
GUICtrlSetTip(-1, "123 Main Street")
$InputLenderAdd2 = GUICtrlCreateInput("", 115, 161, 281, 21)
GUICtrlSetTip(-1, "City, State, Zip")
$InputLenderAdd3 = GUICtrlCreateInput("", 115, 193, 281, 21)
GUICtrlSetTip(-1, "e.g. Tarrant County")
$Label1 = GUICtrlCreateLabel("Lender Name:", 27, 97, 71, 17)
$InputLender = GUICtrlCreateInput("", 115, 97, 281, 21)
GUICtrlSetTip(-1, "We Lend Things, Inc.")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group4 = GUICtrlCreateGroup("Loan Info:", 16, 248, 681, 201)
$Label10 = GUICtrlCreateLabel("Principal:", 27, 272, 47, 17)
$Label11 = GUICtrlCreateLabel("Write Out:", 27, 305, 52, 17)
$Label14 = GUICtrlCreateLabel("", 27, 337, 4, 4)
$InputPrincipal = GUICtrlCreateInput("", 88, 272, 153, 21)
GUICtrlSetTip(-1, "$xx,xxx.xx")
$InputWriteOut = GUICtrlCreateEdit("", 88, 304, 593, 129, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_WANTRETURN))
GUICtrlSetTip(-1, "THIS MANY AND NO/100 DOLLARS (NO LINE BREAKS)")
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Group6 = GUICtrlCreateGroup("Trustee Info", 418, 73, 281, 161)
$Label18 = GUICtrlCreateLabel("Trustee Name:", 426, 97, 74, 17)
$InputTrustee = GUICtrlCreateInput("", 506, 97, 185, 21)
GUICtrlSetTip(-1, "I.M. Trustworthy, Jr.")
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateTabItem("")
$GFLabel = GUICtrlCreateLabel("GF No:", 8, 8, 38, 17)
$CommonLabel = GUICtrlCreateLabel("Property Address:", 128, 8, 87, 17)
$CloseDateLabel = GUICtrlCreateLabel("Closing Date:", 544, 8, 67, 17)
$InputGF = GUICtrlCreateInput("", 48, 8, 73, 21)
GUICtrlSetTip(-1, "123456-RN-X")
$InputCommon = GUICtrlCreateInput("", 216, 8, 321, 21)
GUICtrlSetTip(-1, "Common address from front of file")
$InputInvoiceNo = GUICtrlCreateInput("", 72, 472, 129, 21)
$HelpButton = GUICtrlCreateButton("Help", 632, 32, 81, 25)
$LabelInvoiceNo = GUICtrlCreateLabel("Invoice No.", 8, 472, 59, 17)
$InputClosingDate = GUICtrlCreateInput("", 616, 8, 97, 21)
GUICtrlSetTip(-1, "Close date from contract")
$SaveButton = GUICtrlCreateButton("Save", 640, 472, 73, 25)
GUICtrlSetTip(-1, "Click this when you're ready to prep docs")
$InputType = GUICtrlCreateCombo("", 304, 472, 129, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "GWD|WDVL|WDMR|WDVLMR|SWD|SWDVL|SWDMR|SWDVLMR|Note & DoT|Wraparound Package")
$Label12 = GUICtrlCreateLabel("Doc Type:", 248, 472, 54, 17)
$Label13 = GUICtrlCreateLabel("Doc Cost:", 440, 472, 51, 17)
$InputCost = GUICtrlCreateInput("", 496, 472, 73, 21)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###
Global $GF, $type, $fail[0][2], $addfail[1][2], $n = "", $invoice = 4, $sleep = 50, $aArray_invoice[12][2] = [["Field","Data"]], $buyerentitystate, $sellerentitystate, $buyerstatus, $sellerstatus, $buyer2, $seller2, $Cid, $arrayadd[1][2], $Fields[] = ["[Legal]","[Common]","[GF]","[CloseDate]","[Buyer 1]","[Buyer 2]","[Buyeradd1]","[Buyeradd2]","[Buyeradd3]","[Seller 1]","[Seller 2]","[Selleradd1]","[Selleradd2]","[Selleradd3]","[Lender]","[Lenderadd1]","[Lenderadd2]","[Lenderadd3]","[Principal]","[Write Out]","[Trustee]","[Invoice No.]","[Type]"]
hotkeyset("`", "prep")

While 1 ; What to do when buttons are pressed/forms are filled
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
         Case $HelpButton
            msgbox(-1, "Help", "Under construction!")
         Case $SaveButton
            Read()
            Parties()
            Buyers()
            Sellers()
            _arraydisplay($aArray_Base, "Saved Data")
    EndSwitch
 WEnd

 Func Read() ; Function for getting all data from input boxes
    global $aArray_Base[0][2] ; Start with clean array each time function is run
    For $n = 1 to 23 ; Make sure upper boundary is highest number in ctrl()!!
       If Guictrlread(Ctrl($n)) = NOT ("" OR 0)  Then ; Skip blank data
            $arrayadd[0][0] = $Fields[$n-1]         ; Define temporary array with data from $fields and Ctrl($n)
            $arrayadd[0][1] = Guictrlread(Ctrl($n)) ;
         If _arraysearch($aArray_Base, $arrayadd[0][0]) = NOT (-1) Then ; If [Field] is already in main array, replace data from input box, leave [Field] same
            $aArray_Base[_arraysearch($aArray_Base, $arrayadd[0][0])][1] = GuiCtrlRead(Ctrl($n))
         ElseIf @error = (6 OR 3) Then ; If Data not found or main array is empty, append new index to main array
         _arrayadd($aArray_Base, $arrayadd)
         EndIf
      EndIf
   Next
 EndFunc

 Func Ctrl($n) ; Function for passing control handles to for loops
    Select
    Case $n = 1
       $cid = $inputlegal
    Case $n = 2
       $cid = $inputcommon
    Case $n = 3
       $cid = $inputGF
    Case $n = 4
       $cid = $inputClosingDate
    Case $n = 5
       $cid = $inputBuyer1
    Case $n = 6
       $cid = $inputBuyer2
    Case $n = 7
       $cid = $inputBuyeradd1
    Case $n = 8
       $cid = $inputBuyeradd2
    Case $n = 9
       $cid = $inputBuyeradd3
    Case $n = 10
       $cid = $inputSeller1
    Case $n = 11
       $cid = $inputSeller2
    Case $n = 12
       $cid = $inputSelleradd1
    Case $n = 13
       $cid = $inputSelleradd2
    Case $n = 14
       $cid = $inputSelleradd3
    Case $n = 15
       $cid = $inputLender
    Case $n = 16
       $cid = $inputLenderadd1
    Case $n = 17
       $cid = $inputLenderadd2
    Case $n = 18
       $cid = $inputLenderadd3
    Case $n = 19
       $cid = $inputPrincipal
    Case $n = 20
       $cid = $inputWriteOut
    Case $n = 21
       $cid = $inputTrustee
    Case $n = 22
       $cid = $inputInvoiceNo
    Case $n = 23
       $cid = $inputType
    EndSelect
    Return $Cid
 EndFunc

Func Parties() ; Determine how many buyers and sellers
   local $row = _arraysearch($aArray_Base, "[Seller 2]")
  If  $row = -1 Then ; If no seller 2, seller 2 false
     global $2sellers = False
  ElseIf $aArray_Base[$row][1] = "" Then ; If seller 2 label, but not data, seller 2 false
     global $2sellers = False
  Else ; If seller 2 label and data, seller 2 true
     Global $2sellers = True
  EndIf
   local $row = _arraysearch($aArray_Base, "[Buyer 2]")
  If  $row = -1 Then
     global $2buyers = False
  ElseIf $aArray_Base[$row][1] = "" Then
     global $2buyers = False
  Else
     global $2buyers = True
  EndIf
  msgbox(-1, "", "2sellers " & $2sellers & "2buyers " & $2buyers)
EndFunc

Func Buyers()
If $2buyers = False Then ; [Buyer1&2] = [Buyer 1]
   local  $row = _arraysearch($aArray_Base, "[Buyer 1]")
   If $row = NOT -1 Then ; account for blank [Buyer 1]
   $arrayadd[0][0] = "[Buyer1&2]"
   $arrayadd[0][1] = $aArray_Base[$row][1]
   _arrayadd($aArray_Base, $arrayadd)

   $arrayadd[0][0] = "[Buyer 2]" ; Delete Buyer 2 if found
   $arrayadd[0][1] = ""
   _arrayadd($aArray_Base, $arrayadd)
   EndIf
ElseIf $2buyers = True Then ; [Buyer1&2] = [Buyer 1] and [Buyer 2]
   Local $1row = _arraysearch($aArray_Base, "[Buyer 1]")
   Local $2row = _arraysearch($aArray_Base, "[Buyer 2]")
    $arrayadd[0][0] = "[Buyer1&2]"
    $arrayadd[0][1] = "" & $aArray_Base[$1row][1] & " and " & $aArray_Base[$2row][1]
   _arrayadd($aArray_Base, $arrayadd)
EndIf
EndFunc

Func Sellers()
If $2sellers = False Then
   local $row = _arraysearch($aArray_Base, "[Seller 1]")
   msgbox(-1, "Sellers()","$row = " & $row)
   If $row = NOT -1 Then
      msgbox(-1, "Sellers()", "$row is not -1")
   $arrayadd[0][0] = "[Seller1&2]"
   msgbox(-1, "Sellers()", "$arrayadd[0][0] = " & $arrayadd[0][0])
   $arrayadd[0][1] = $aArray_Base[$row][1]
   msgbox(-1, "", "$arrayadd[0][1] = " & $arrayadd[0][1])
   _arrayadd($aArray_Base, $arrayadd)

   $arrayadd[0][0] = "[Seller 2]"
   $arrayadd[0][1] = ""
   _arrayadd($aArray_Base, $arrayadd)
   EndIf
ElseIf $2sellers = True Then
   Local $1row = _arraysearch($aArray_Base, "[Seller 1]")
   Local $2row = _arraysearch($aArray_Base, "[Seller 2]")
    $arrayadd[0][0] = "[Seller1&2]"
    $arrayadd[0][1] = "" & $aArray_Base[$1row][1] & " and " & $aArray_Base[$2row][1]
   _arrayadd($aArray_Base, $arrayadd)
   EndIf
EndFunc

Func Signatures() ; Create labels and data to fill buyer/seller fields with data entered already
EndFunc

Func Acknowledge() ; Format and fill acknowledgements based on number and type of buyers and sellers
EndFunc

Func prep()
  local $oWord = _Word_Create() ; Create Application Object
   If @error Then
      Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", "Error creating application object. Script terminated.")
   EndIf
  local $filepath = "C:\Users\Randy\Dropbox\Doc Prep\_KNDS Doc Prep Wizard (windows only)\GWD - Seller(s) People - Experiment.docx" ; Select Template to open
  local $oDoc = _word_docopen($oWord, $filepath) ; Open template
   If @error Then
      Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", "Error opening template. Script terminated.")
   EndIf
   For $i = 1 to Ubound($aArray_Base) ; Find and replace all [Fields] from main array with data
      _Word_DocFindReplace($oDoc, "" & $aArray_base[$i-1][0], "" & $aArray_Base[$i-1][1])
         If @error Then
            $addfail[0][0] = $aArray_Base[$i-1][0]
            $addfail[0][1] = $aArray_Base[$i-1][1]
            _arrayadd($fail, $addfail)
   EndIf
Next
   _ArrayDisplay($fail, "Items not replaced")
MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", "Completed Find and replace. Iterations: "&$i)
   _word_docsaveas($oDoc, @HomeDrive & @HomePath & "\Dropbox\Doc Prep\_KNDS Doc Prep Wizard (windows only)\___Completed\" & guictrlread($inputgf) & " - " & guictrlread($inputtype)) ; Save working document with GF number
      If @error Then
      Exit MsgBox($MB_SYSTEMMODAL, "Word UDF: _Word_DocFindReplace Example", "Error saving deed document. Script terminated." & @CRLF &  "Make sure Dropbox is installed on the PC." & @CRLF & @CRLF & "Also make sure that the following filepath exists:" & @CRLF & @CRLF & @HomeDrive & @HomePath & "\Dropbox\Doc Prep\_KNDS Doc Prep Wizard (windows only)\___Completed\")
   EndIf
   Shellexecute("" & @homedrive & @homepath & "\Dropbox\Doc Prep\_KNDS Doc Prep Wizard (windows only)\___Completed\")
   MsgBox($MB_SYSTEMMODAL, "KNDS Doc Prep Wizard", "Documents successfully prepared." & @CRLF & "Please inspect the document and the invoice for any errors, then drag and drop them to the appropriate location to save dropbox space.")
EndFunc

 

 

Link to comment
Share on other sites

Hi all,

I figured it out. Sorry for the inconvenience. I was doing this:

Func Buyers()
If $2buyers = False Then ; [Buyer1&2] = [Buyer 1]
   local  $row = _arraysearch($aArray_Base, "[Buyer 1]")
   If $row = NOT -1 Then ; account for blank [Buyer 1] ; <================= ERROR IS HERE
   $arrayadd[0][0] = "[Buyer1&2]"
   $arrayadd[0][1] = $aArray_Base[$row][1]
   _arrayadd($aArray_Base, $arrayadd)

   $arrayadd[0][0] = "[Buyer 2]" ; Delete Buyer 2 if found
   $arrayadd[0][1] = ""
   _arrayadd($aArray_Base, $arrayadd)
   EndIf
ElseIf $2buyers = True Then ; [Buyer1&2] = [Buyer 1] and [Buyer 2]
   Local $1row = _arraysearch($aArray_Base, "[Buyer 1]")
   Local $2row = _arraysearch($aArray_Base, "[Buyer 2]")
    $arrayadd[0][0] = "[Buyer1&2]"
    $arrayadd[0][1] = "" & $aArray_Base[$1row][1] & " and " & $aArray_Base[$2row][1]
   _arrayadd($aArray_Base, $arrayadd)
EndIf
EndFunc

Using $row = NOT -1 was causing problems, although I'm not sure why. Using $row <> -1 fixed everything. Thanks anyway!

-Anoig

Edited by anoig
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...