是否可以在 VB6 中使用 SQL 查询填充 Farpoint Spread 6.0 vaSpread 组件?

Is it possible to populate a Farpoint Spread 6.0 vaSpread component using a SQL query in VB6?

我在 SQL Server 2008 R2 上使用 T-SQL 编写了一个查询,它提供了我需要在 Visual Basic 6 上名为 SSlist 的 vaSpread 组件上显示的正确信息。我有已经打开了与数据库的连接,但我很难找到有关如何使用我的 T-SQL 查询直接填充 vaSpread 组件的资源。我只需要完全按照我在 Microsoft SQL Server Management Studio 中执行它时的显示方式来显示它。

我的查询是:

SELECT QC.LINE_CD AS 'Line Code', QC.LINE_NM AS 'Line Name', PN.GUBUN, WO.WRK_QTY AS 'Work QTY', CM.LINE_TARGET AS 'Line Target',
    CM.RETURN_TARGET AS 'Return Target', SUM(PN.R_QTY) AS 'Rework QTY', SUM(PN.S_QTY) AS 'Scrap QTY',
    SUM(PN.UPRC_AMT) AS 'UPRC AMT', (SUM(COALESCE(PN.UPRC_AMT,0)*PN.S_QTY)+SUM(PN.R_QTY)*3.8) AS 'Cost'
FROM QC_LINE_MST AS QC

LEFT JOIN (SELECT PE.LINE_CD, PE.WRK_YMD, PE.CUST_CD, PE.GUBUN, PE.ITMNO, PE.R_QTY, PE.S_QTY, ND.UPRC_AMT FROM PROC_ERR AS PE
            INNER JOIN (SELECT ITMNO, CUST_CD, UPRC_AMT FROM NOW_DANGA) AS ND ON PE.ITMNO = ND.ITMNO AND PE.CUST_CD = ND.CUST_CD 
            WHERE PE.WRK_YMD BETWEEN '20161116' AND '20161201' AND (PE.R_QTY <> 0 OR PE.S_QTY <> 0) 
            ) AS PN ON QC.LINE_CD = PN.LINE_CD

LEFT JOIN (SELECT A.CODE, A.DSCP AS LINE_TARGET, B.DSCP AS RETURN_TARGET FROM COD_MST AS A
            INNER JOIN (SELECT CODE, DSCP FROM COD_MST WHERE GUBN='QC09'
            ) AS B ON A.CODE = B.CODE
            WHERE A.GUBN='QC08') CM ON QC.LINE_CD = CM.CODE

LEFT JOIN (SELECT LINE_CD, SUM(WRK_QTY) AS WRK_QTY FROM WRK_ORD
            WHERE WRK_YMD BETWEEN '20161116' AND '20161201' GROUP BY LINE_CD
            ) AS WO ON QC.LINE_CD = WO.LINE_CD

GROUP BY QC.LINE_CD, QC.LINE_NM, WO.WRK_QTY, PN.GUBUN, CM.LINE_TARGET, CM.RETURN_TARGET
ORDER BY QC.LINE_CD

我在网上搜索过,试图弄清楚如何使用此查询填充我的 vaSpread,但要么我找错了地方,要么 Farpoint Spread 6.0 上的资源稀缺。如果有人对如何实现这一点有任何想法,或者可以指导我阅读一些有用的文献,将不胜感激。另外,如果有人对如何清理我的 SQL 有任何想法 查询并提高效率,这也很受欢迎。我对此很陌生。谢谢,如果我需要提供更多信息,请告诉我!我期待着阅读您的建议。

经过更多研究后,我了解到应该使用 FarPoint 6.0 FpSpread 组件(支持 OLEDB)而不是使用 FarPoint 6.0 vaSpread 组件(支持 OLEDB)以自动填充传播 sheet.但是,自动填充新的 FpSpread 组件的方法需要:

1) 连接到数据库的 ADODC 组件

2) 数据库上的一个存储过程

看到我已经有了一个活动连接并且还需要在计算中使用某些列记录来填充其他列,我决定采用使用 FOR 循环的手动 spreadsheet 填充方法。我的代码附在下面,这样任何有类似问题的人都可以使用我的代码来获得灵感。

 With SSlist

    //SQL Query to USA_ERP.QC_LINE_MST Table to receive total number of Rows in Record Set 
    SqlStmt = CSQL("SELECT COUNT(*) AS 'Count' FROM QC_LINE_MST")
    Rs.Open SqlStmt, CN, adOpenForwardOnly, adLockReadOnly
    LastRow = Val(Rs.Fields("Count"))
    RowB4Last = Val(Rs.Fields("Count")) - 1
    .MaxRows = LastRow
    Rs.Close

    //Formatting for Last Row (Totals row)
    For RowCount = 1 To LastRow
        .Row = RowCount
        .RowHeight(.Row) = 18
        //Font and cell formatting for Line Columns
        For ColCount = 1 To 1
            .Col = ColCount
            .CellType = CellTypeStaticText
            .TypeHAlign = TypeHAlignCenter
            .FontBold = True
            .TypeVAlign = TypeVAlignCenter
        Next
        If .Row = LastRow Then
            //Merge for Totals label of Last Row (Totals row)
            For ColCount = 1 To 2
               .Col = ColCount
               .Text = "Totals"
               .RowMerge = MergeRestricted
            Next
            //Font and cell formatting for Last Row (Totals row)
            For ColCount = 1 To 15
              .Col = ColCount
              .CellType = CellTypeStaticText
              .TypeHAlign = TypeHAlignCenter
              .FontBold = True
              .TypeVAlign = TypeVAlignCenter
            Next
       End If
    Next

    //Main SQL Query to USA_ERP Database
    SqlStmt = CSQL("SELECT QC.LINE_CD AS 'Line Code', QC.LINE_NM AS 'Line Name', PN.GUBUN, WO.WRK_QTY AS 'Work QTY', CM.LINE_TARGET AS 'Line Target', " & _
                            "CM.RETURN_TARGET AS 'Return Target', SUM(PN.R_QTY) AS 'Rework QTY', SUM(PN.S_QTY) AS 'Scrap QTY', " & _
                            "SUM(PN.UPRC_AMT) AS 'UPRC AMT', (SUM(COALESCE(PN.UPRC_AMT,0)*PN.S_QTY)+SUM(PN.R_QTY)*3.8) AS 'Cost' " & _
                    "FROM QC_LINE_MST AS QC " & _
                        "LEFT JOIN (SELECT PE.LINE_CD, PE.WRK_YMD, PE.CUST_CD, PE.GUBUN, PE.ITMNO, PE.R_QTY, PE.S_QTY, ND.UPRC_AMT FROM PROC_ERR AS PE " & _
                                    "INNER JOIN (SELECT ITMNO, CUST_CD, UPRC_AMT FROM NOW_DANGA) AS ND ON PE.ITMNO = ND.ITMNO AND PE.CUST_CD = ND.CUST_CD  " & _
                                    "WHERE PE.WRK_YMD BETWEEN '$S' AND '$S' AND (PE.R_QTY <> 0 OR PE.S_QTY <> 0) " & _
                                    ") AS PN ON QC.LINE_CD = PN.LINE_CD " & _
                        "LEFT JOIN (SELECT A.CODE, A.DSCP AS LINE_TARGET, B.DSCP AS RETURN_TARGET FROM COD_MST AS A " & _
                                    "INNER JOIN (SELECT CODE, DSCP FROM COD_MST WHERE GUBN='QC09' " & _
                                    ") AS B ON A.CODE = B.CODE " & _
                                    "WHERE A.GUBN='QC08') CM ON QC.LINE_CD = CM.CODE " & _
                        "LEFT JOIN (SELECT LINE_CD, SUM(WRK_QTY) AS WRK_QTY FROM WRK_ORD " & _
                                    "WHERE WRK_YMD BETWEEN '$S' AND '$S' GROUP BY LINE_CD " & _
                                    ") AS WO ON QC.LINE_CD = WO.LINE_CD " & _
                    "GROUP BY QC.LINE_CD, QC.LINE_NM, WO.WRK_QTY, PN.GUBUN, CM.LINE_TARGET, CM.RETURN_TARGET " & _
                    "ORDER BY QC.LINE_CD " _
                    , Format(DTPDate(0).Value, "YYYYMMDD"), Format(DTPDate(1).Value, "YYYYMMDD"), Format(DTPDate(0).Value, "YYYYMMDD"), Format(DTPDate(1).Value, "YYYYMMDD"))

    Rs.Open SqlStmt, CN, adOpenForwardOnly, adLockReadOnly

    While Not Rs.EOF

        //Start at First Row for First Record from RecordSet (Rs), loop through all Records from RecordSet (Rs)
        For RowCount = 1 To LastRow
            .Row = RowCount
            //Initialize/Re-initialize calculation variables for every Record
            LineScrap = 0
            CustomerScrap = 0
            ResidentScrap = 0
            ReworkQTY = 0
            FailCost = 0

            //Check to see if LastRow (Totals Row)
            If .Row = LastRow Then

                //If LastRow, populate columns with Total values
                For ColCount = 1 To 15
                .Col = ColCount
                    If .Col = 1 Then

                    ElseIf .Col = 2 Then
                        .ColMerge = MergeRestricted
                    ElseIf .Col = 3 Then
                        .Text = TotalProduction
                    ElseIf .Col = 4 Then
                        .Text = Val(Rs.Fields("Line Target"))
                    ElseIf .Col = 5 Then
                        .Text = TotalRework
                    ElseIf .Col = 6 Then
                        .Text = TotalScrap
                    ElseIf .Col = 7 Then
                        .Text = TotalReworkPPM
                    ElseIf .Col = 8 Then
                        .Text = TotalScrapPPM
                    ElseIf .Col = 9 Then
                        .Text = TotalFailCosts
                    ElseIf .Col = 10 Then
                        .Text = Val(Rs.Fields("Return Target"))
                    ElseIf .Col = 11 Then
                        .Text = TotalCustReturn
                    ElseIf .Col = 12 Then
                        .Text = TotalOnSiteReturn
                    ElseIf .Col = 13 Then
                        .Text = TotalCustReturnPPM
                    ElseIf .Col = 14 Then
                        .Text = TotalOnSiteReturnPPM
                    ElseIf .Col = 15 Then
                        .Text = TotalScrapPPM
                    Else
                    End If
                Next
                //Close database connection
                Rs.Close
                //Exit Subroutine logic
                Exit Sub

            End If

            //Choose the correct variable to store "Scrap QTY" value from RecordSet (Rs) based on "GUBUN" value of Record
            If IsNull(Rs.Fields("Scrap QTY")) = False Then
                If Trim(Rs.Fields("GUBUN")) = "Customer" Then
                    CustomerScrap = Val(Rs.Fields("Scrap QTY"))
                ElseIf Trim(Rs.Fields("GUBUN")) = "On Site" Then
                    ResidentScrap = Val(Rs.Fields("Scrap QTY"))
                ElseIf Trim(Rs.Fields("GUBUN")) = "MIP NG" Then
                    LineScrap = Val(Rs.Fields("Scrap QTY"))
                End If
            //If "Scrap QTY" is NULL then set correct variable to 0 based on "GUBUN" value of Record
            Else
                 If Trim(Rs.Fields("GUBUN")) = "Customer" Then
                    CustomerScrap = 0
                 ElseIf Trim(Rs.Fields("GUBUN")) = "On Site" Then
                    ResidentScrap = 0
                 Else
                    LineScrap = 0
                 End If
            End If

            //Store "Rework QTY" in correct variable
            //If "Rework QTY" is NULL, store 0
            If IsNull(Rs.Fields("Rework QTY")) = False Then
                ReworkQTY = Val(Rs.Fields("Rework QTY"))
            Else
                ReworkQTY = 0
            End If


            //Populate spread (SSList) with correct values using RecordSet (Rs) and calculated variables
            //Line Column
            .Col = 1
                .Text = Rs.Fields("Line Code")
            //Model Column
            .Col = 2
                .Text = Rs.Fields("Line Name")
            //Prod (EA) Column
            .Col = 3
                //If "Work QTY" Record is Null set cell value to 0
                If IsNull(Rs.Fields("Work QTY")) = False Then
                    .Text = Trim(Val(Rs.Fields("Work QTY")) + LineScrap)
                Else
                    .Text = 0
                End If
                //Calculate running total for 'Prod (EA)' Column through all Records/loops
                TotalProduction = TotalProduction + Val(.Text)
            //In Line Target (PPM) Column
            .Col = 4
                //If "Line Target" Record is Null set cell value to 0
                If IsNull(Rs.Fields("Line Target")) = False Then
                    .Text = Trim(Val(Rs.Fields("Line Target")))
                Else
                    .Text = 0
                End If
            //In Line Rework QTY Column
            .Col = 5
                //If "Rework QTY" Record is Null set cell value to 0
                If IsNull(Rs.Fields("Rework QTY")) = False Then
                    .Text = ReworkQTY
                Else
                    .Text = 0
                End If
                //Calculate running total for 'In Line Rework QTY' Column through all Records/loops
                TotalRework = TotalRework + Val(.Text)
            //In Line Scrap QTY Column
            .Col = 6
                //Set cell value to LineScrap variable
                .Text = LineScrap
                //Calculate running total for 'In Line Scrap QTY' Column through all Records/loops
                TotalScrap = TotalScrap + Val(.Text)
            //In Line Rework PPM QTY Column
            .Col = 7
                //If "Work QTY" Record is Null set cell value to 0
                If IsNull(Rs.Fields("Work QTY")) = False Then
                    .Text = Round(ReworkQTY / (Val(Rs.Fields("Work QTY")) + LineScrap) * 10 ^ 6, 6)
                Else
                    .Text = 0
                End If
                //Calculate running total for 'In Line Rework PPM QTY' Column through all Records/loops
                TotalReworkPPM = TotalReworkPPM + Val(.Text)
            //In Line Scrap PPM QTY Column
            .Col = 8
                //If "Work QTY" is Null set cell value to 0
                If IsNull(Rs.Fields("Work QTY")) = False Then
                    .Text = Round(LineScrap / (Val(Rs.Fields("Work QTY")) + LineScrap) * 10 ^ 6, 6)
                Else
                    .Text = 0
                End If
                //Calculate runing total for 'In Line Scrap PPM QTY' Column through all Records/loops
                TotalScrapPPM = TotalScrapPPM + Val(.Text)
            //In Line Fail Costs ($) Column
            .Col = 9
                //If "GUBUN" Record is "MIP NG" and "Cost" Record is Not Null set cell value to "Cost" Record
                //Otherwise, set cell value to 0
                If Trim(Rs.Fields("GUBUN")) = "MIP NG" Then
                    If IsNull(Trim(Rs.Fields("Cost"))) = False Then
                        .Text = Val(Rs.Fields("Cost"))
                    Else
                        .Text = 0
                    End If
                Else
                    .Text = 0
                End If
                //Calculate running total for 'In Line Fail Costs ($)' Column through all Records/loops
                TotalFailCosts = TotalFailCosts + Val(.Text)
            //Customer Return Target PPM QTY Column
            .Col = 10
                //If "Return Target" Record is Null set cell value to 0
                If IsNull(Rs.Fields("Return Target")) = False Then
                    .Text = Trim(Val(Rs.Fields("Return Target")))
                Else
                    .Text = 0
                End If
            //Customer Return QTY Column
            .Col = 11
                //Set cell value to CustomerScrap variable
                .Text = CustomerScrap
                //Calculate running total for 'Customer Return QTY' Column through all Records/loops
                TotalCustReturn = TotalCustReturn + Val(.Text)
            //On Site Return QTY Column
            .Col = 12
                //Set cell value to ResidentScrap variable
                .Text = ResidentScrap
                //Calculate running total for 'On Site Return QTY' Column through all Records/loops
                TotalOnSiteReturn = TotalOnSiteReturn + Val(.Text)
            //Customer Return PPM QTY Column
            .Col = 13
                //If "Work QTY" Record is Null set cell value to 0
                If IsNull(Rs.Fields("Work QTY")) = False Then
                    .Text = Round(CustomerScrap / (Val(Rs.Fields("Work QTY")) + LineScrap) * 10 ^ 6, 2)
                Else
                    .Text = 0
                End If
                //Calculate running total for 'Customer Return PPM QTY' Column through all Records/loops
                TotalCustReturnPPM = TotalCustReturnPPM + Val(.Text)
            //On Site Return PPM QTY Column
            .Col = 14
                //If "Work QTY" Record is Null set cell value to 0
                If IsNull(Rs.Fields("Work QTY")) = False Then
                    .Text = Round(ResidentScrap / (Val(Rs.Fields("Work QTY")) + LineScrap) * 10 ^ 6, 2)
                Else
                    .Text = 0
                End If
                //Calculate running total for 'On Site Return PPM QTY' Column through all Records/loops
                TotalOnSiteReturnPPM = TotalOnSiteReturnPPM + Val(.Text)
            //Total Loss PPM Column
            .Col = 15
                //If "Work QTY" Record is Null set cell value to 0
                If IsNull(Rs.Fields("Work QTY")) = False Then
                    .Text = Round((CustomerScrap + LineScrap) / (Val(Rs.Fields("Work QTY")) + LineScrap) * 10 ^ 6, 0)
                Else
                    .Text = 0
                End If
                //Calculate running total for 'Total Loss PPM' Column through all Records/loops
                TotalLossPPM = TotalLossPPM + Val(.Text)
            //Move to the next Record in RecordSet (Rs)
            Rs.MoveNext
        Next

    Wend
    End With

此代码 运行 与数据库 CN 和 RecordSet Rs 的活动连接。 FOR 循环基本上遍历每一行的每一列,并根据逻辑用所需的正确值填充每个单元格,在每一行之后移动到 RecordSet 中的下一个 Record。我的 SQL 查询 RecordSet 中的最后一行是总计行,仅在某些列中有数据。当到达最后一行时,它会使用计算出的 运行ning 总数或 RecordSet 中的值(如果可用)填充单元格。填充 table 的最后一行后,子例程结束。

我不知道是否有人对这个问题感兴趣,但希望这可以帮助别人。这可能不是填充 FarPoint vaSpread 组件的理想或最有效的方式,但它在 100% 的时间内都有效,并且根据您的 SQL 查询,您可以做出这个未来的证明。特别是,我设置了我的查询,因此所有连接都发生在单个引用 table (QC.LINE_MST) 上,其中填充了我希望看到的行代码或 "Line_CD" table。这使我能够向该引用 table 添加新的 "Line_CD",以便我的查询和我的程序将在下一次查询时选择它。此逻辑还处理来自 SQL table 的 NULL 值,在进行任何计算或填充单元格之前将所有 NULL 值设置为 0。唯一需要更新此逻辑的时间是当您想向 table 添加新的信息列时,我个人不需要这样做。

如果有人对代码有任何建议,如何提高代码效率或格式更清晰,请在下面发表评论。