Compile function SQL_Insert_MV(Connection, TableName, Keys, MvFieldsList, RepeatFields)

/*****************************************************************************\
	Writes an associated set of multie-valued fields to the given table.
	
	History
	-------
	04/14/2010    KRF   Original Programmer
\*****************************************************************************/

$insert Microsoft_Ado_Equates

If Assigned(Connection) else Connection = 0
If Assigned(TableName) else TableName = ""
If Assigned(Keys) else Keys = ""
If Assigned(MvFieldsList) else MvFieldsList = ""
If Assigned(RepeatFields) else RepeatFields = ""

Declare subroutine SRP_Com
Declare function SRP_Com, SQL_Insert, SRP_Rotate_Array
Ans = ""

// Rotate the MvFields array so we can quickly get the data we want
MvFieldsArray = SRP_Rotate_Array(MvFieldsList, @RM, @FM)

// Get the data names and rows
DataNames = MvFieldsArray[1, @RM]
DataCols = MvFieldsArray[Col2() + 1, @RM]
RepeatFlags = MvFieldsArray[Col2() + 1, @RM]
DataRows = SRP_Rotate_Array(DataCols)
LenDataRows = Len(DataRows)
Convert @FM to @VM in DataNames
       
// Process each data row
iDataRow = 0
DataRowPos = 1
Loop
   
    // Extract the data ([] is faster than <>)
    DataRow = DataRows[DataRowPos, @FM]
    DataRowPos = Col2() + 1
    iDataRow += 1
    
    // Add the MV_NO key part
    MvKeys = Keys:@FM:"MV_NO":@VM:iDataRow
    
    // Get the data fields
    DataFields = SRP_Rotate_Array(DataNames:@FM:DataRow)
    
    // Add repeating fields
    If RepeatFields NE "" then
        DataFields = Insert(DataFields, -1, 0, 0, RepeatFields)
    end
    
    // Write the record
    Ans = SQL_Insert(Connection, TableName, MvKeys, DataFields)

Until DataRowPos GE LenDataRows OR Ans NE ""
Repeat

Return Ans
