2016-12-02 63 views
2

我在SQL Server 2008 R2上使用T-SQL編寫了一個查詢,該查詢提供了正確的信息,我需要在Visual Basic 6上名爲SSlist的vaSpread組件上顯示。我已經打開了與數據庫的連接,但是我難以找到關於如何使用我的T-SQL查詢直接填充vaSpread組件的資源。我只需要按照它在Microsoft SQL Server Management Studio中執行時的顯示方式進行顯示。是否可以在VB6中使用SQL查詢填充Farpoint Spread 6.0 vaSpread組件?

我的查詢是:

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蔓延的資源是6.0稀缺。如果任何人有任何想法如何實現這一點,或者可以指導我一些有用的文獻,將不勝感激。另外,如果有人對如何清理我的SQL查詢並使其更高效有任何想法,那也是值得歡迎的。我對此很新。謝謝,讓我知道如果我需要提供更多的信息!我期待着閱讀你的建議。

回答

0

在做了一些更多的研究之後,我學會了使用FarPoint 6.0 FpSpread組件(無OLEDB)來自動填充電子表格,而不是使用FarPoint 6.0 vaSpread組件(非OLEDB)。然而,該方法自動填充所需的新FpSpread控件:

1)連接到數據庫

2)數據庫

上存儲過程看到如何我已經有活性的ADODC成分連接,並且還需要在計算中使用某些列記錄來填充其他列,我決定使用FOR循環的手動電子表格羣體方法。我的代碼附在下面,以便任何有類似問題的人都可以使用我的代碼來獲得想法。

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中的下一個記錄。我的SQL查詢RecordSet中的最後一行是僅在某些列中具有數據的總計行。到達最後一行時,它將使用計算的運行總計或可用時記錄集中的值填充單元格。填充表格的最後一行後,子程序結束。

我不知道有沒有人對這個問題有任何興趣,但希望這可以幫助某人。這可能不是填充FarPoint vaSpread組件的理想或最有效的方式,但它可以在100%的時間內運行,並且取決於您的SQL查詢,您可以做出這一未來證明。特別是,我設置了查詢,因此所有連接都發生在單行參考表(QC.LINE_MST)上,該表上填有行代碼或「Line_CD」,我希望在表中看到這些參數。這使我可以將新的「Line_CD」添加到該參考表中,以便我的查詢以及我的程序將在下一個查詢中提取它。該邏輯還處理來自SQL表的NULL值,在進行任何計算或填充單元之前將所有NULL值設置爲0。這個邏輯需要更新的唯一時間是當你想向表中添加新的信息列時,我個人不需要這樣做。

如果任何人有任何關於代碼的建議,提高效率或更清晰格式的方法,請在下面留言。