Page 1 of 1

Order of operations

Posted: Mon May 19, 2014 7:30 pm
by DavJans
So since I'm still a nob, I still do things wrong. On my buttons, I found that the "on openCard" script sneaks in between two lines of code :( Looks like I'm rewriting 50 buttons

I have a card with about 50 buttons on it,

Code: Select all

on mouseUp
   go to card Yard2
   put "2C" into fld "Zone" on card Yard2
end mouseUp
On card Yard2 there is a Mysql quary

Code: Select all

on openCard
   put Fld "Zone" into tzone
   if tzone <> "" then
      send Q1 to card "Yard2"
      else
      send Q2 to card "Yard2"
      end if
end openCard

on Q1
    ## Connect to the database
    put "mysql" into theDBType
    put "10.0.1.7:3307" into theDBHost
    put "mfc" into theDBName
    put "davej" into theDBUser
    put "******" into theDBPassword
    put fld "Zone" into tzone
    put revOpenDatabase( theDBType, theDBHost, theDBName, theDBUser, theDBPassword ) into theConnectionID
    
    if theConnectionID is an integer then
       ## Query the database for data
       put revQueryDatabase( theConnectionID, "SELECT * FROM yard WHERE loc='" & tzone & "'") into theCursor

        if theCursor is an integer then
            ConvertSQLCursorToArray theCursor, theDataGridArray
            put the result into theError
            
            if theError is empty then
                ## The cursor was successfully converted to an array.
                ## Assign it to the data grid. The 'firstname' and 'lastname' columns
                ## from the database cursor will appear in the matching columns
                ## in the data grid.
                set the dgData of group "DataGrid 1" to theDataGridArray
            end if
            
            ## Close the database cursor 
            revCloseCursor theCursor
        end if
        
        ## Close the database connection
        revCloseDatabase theConnectionID
    else
        answer "Error connecting to the database:" && theConnectionID & "."
    end if
end Q1

on Q2
    ## Connect to the database
    put "mysql" into theDBType
    put "10.0.1.7:3307" into theDBHost
    put "mfc" into theDBName
    put "davej" into theDBUser
    put "*******" into theDBPassword
    put fld "Zone" into tzone
    put revOpenDatabase( theDBType, theDBHost, theDBName, theDBUser, theDBPassword ) into theConnectionID
    
    if theConnectionID is an integer then
       ## Query the database for data
       put revQueryDatabase( theConnectionID, "SELECT * FROM yard WHERE loc IS NULL ") into theCursor

        if theCursor is an integer then
            ConvertSQLCursorToArray theCursor, theDataGridArray
            put the result into theError
            
            if theError is empty then
                ## The cursor was successfully converted to an array.
                ## Assign it to the data grid. The 'firstname' and 'lastname' columns
                ## from the database cursor will appear in the matching columns
                ## in the data grid.
                set the dgData of group "DataGrid 1" to theDataGridArray
            end if
            
            ## Close the database cursor 
            revCloseCursor theCursor
        end if
        
        ## Close the database connection
        revCloseDatabase theConnectionID
    else
        answer "Error connecting to the database:" && theConnectionID & "."
    end if
end Q2
command ConvertSQLCursorToArray pCursor, @pOutArrayA
    local i
    local theFields
    local theError
    
    ## Get the names of all the columns in the database cursor
    put revDatabaseColumnNames(pCursor) into theFields
    if theFields begins with "revdberr," then
        put item 2 to -1 of theFields into theError
    end if
    
    if theError is empty then
        put 0 into i
        ## Loop through all rows in cursor
        repeat until revQueryIsAtEnd(pCursor)
            add 1 to i
            
            ## Move all fields in row into next dimension of the array
            repeat for each item theField in theFields
                put revDatabaseColumnNamed(pCursor, theField) into pOutArrayA[i][ theField ]
            end repeat
            
            revMoveToNextRecord pCursor
        end repeat
    end if
    
    return theError
end ConvertSQLCursorToArray

Re: Order of operations

Posted: Mon May 19, 2014 9:24 pm
by Simon
Yep!
In cases like this I tend to make a tool to do the job

Code: Select all

on mouseUp
repeat with x = 1 to the number of buttons in this cd
set the script of btn x to ...
I'm guessing that "2C" is what is changing so you'd have to manage that in the repeat loop. And be prepared to close your app without saving because this can really make a mess of things. I keep the code of the btn with the repeat in a text file so I can load and try again with changes.

Simon