Compile subroutine List_Missing_Records_In_SQL(Table, KeyNames, NumToList)

/*****************************************************************************\
	This is a utility function. For every record in the table, it calls the
	Copy_Record_To_SQL method. Use this for new tables or when you want to
	re-sync the SQL database.
	
	History
	-------
	04/06/2010    KRF    Original Programmer
\*****************************************************************************/

Equ Update_Interval$ to 1000

If Assigned(KeyNames) else KeyNames = ""
If Assigned(NumToList) else NumToList = 0
Errors = ""

Declare subroutine SRP_Com
Declare function Copy_Record_To_SQL, GetTickCount, Get.RecCount, SRP_Com, SRP_Rotate_Array

NumChecked = 0

StartTime = GetTickCount()
* InitialTimek = GetTickCount()

// Connect to the ODBC
If Len(KeyNames) then
    
    Columns = KeyNames
    Swap "*" with ", " in Columns
    
    Convert "*" to @VM in KeyNames
    
    If SRP_Com(Connection, "CREATE", "ADODB.Connection") then
        
        // Connect to the database via ODBC    
        SRP_Com(Connection, "SET", "Mode", adModeShareDenyNone)
        SRP_Com(Connection, "SET", "CursorLocation", adUseClient)
        If SRP_Com(Connection, "CALL", "Open", "LSL2SQL") EQ "" then
                
            List = ""
            Open Table to hTable then
                ClearSelect
                Select hTable
                RecCount = Get.RecCount(hTable, Status, 0)
                Done = 0
                Loop
                    ReadNext Key else Done = 1
                Until Done
                    
                    Key = '"TEST_KEVIN"'
                    
                    // Create the where clause expression from the array of keys
                    Convert "*" to @VM in Key
                    KeyExpression = SRP_Rotate_Array(KeyNames:@FM:Key)
                    Swap @VM with " = " in KeyExpression
                    Swap @FM with " AND " in KeyExpression
                    
                    // See if the record exists
                    Script = "Select Count(*) From ":Table:" where ":KeyExpression
                    RecordSetResult = SRP_Com(Connection, "CALL", "Execute", Script)
                    RecordExists = SRP_Com(RecordSetResult, "CALL", "GetString", adClipString, 1, @VM, @FM)[1, \0D\]
                    SRP_Com(RecordSetResult, "RELEASE")
                    If RecordExists NE "1" then
                        List := Key:@FM
                    end
                            
                    // Update progress            
                    NumChecked += 1
                    If GetTickCount() - StartTime GE Update_Interval$ then
                        TotalToCheck = If NumToList GT 0 then NumToList else RecCount
                        Call SRP_Editor_Send_Info("Processed: ":NumChecked:" / ":TotalToCheck)
                        StartTime = GetTickCount()
                    end
                    
                    // Limit to the amount given, if any
                    If NumToList GT 0 AND NumChecked GE NumToList then Done = 1
                    
                Repeat
            end
            List[-1, 1] = ""
    
            // Write the list to SYSLISTS
            Open "SYSLISTS" to hSYSLISTS then
                Write Errors to hSYSLISTS, "COUNT_MISSING_RECORDS_IN_SQL"
                Call SRP_Editor_Open("Record", "SYSLISTS":@FM:"COUNT_MISSING_RECORDS_IN_SQL")
            end
                
        end else
            Ans = SRP_Com(Connection, "ERROR")
        end
        
        SRP_Com(Connection, "CALL", "Close")
        SRP_Com(Connection, "RELEASE")
    end
end

* Call MSG(@Window, "Total Time = ":GetTickCount() - InitialTime:"ms")

Return
