2015-01-05 131 views
1

我正在嘗試對滿足某些條件的訪問表中的記錄進行計數,並將總計插入到一條記錄中的另一個表中。我通過vba這樣做,並試圖在那裏創建一個sql語句,但由於某種原因,它說我有太多的行延續,當我嘗試添加,並且我爲什麼很困惑。任何幫助將不勝感激。在Access VBA中計算SQL語句

intYear = InputBox("What year is it currently?", "Year Input") 

DoCmd.DeleteObject acTable, "ThisTable" 



strCreate = "CREATE TABLE MarketSegmentTotals (" & vbCrLf & _ 
      "[State Medicaid] TEXT," & vbCrLf & _ 
      "Commercial TEXT," & vbCrLf & _ 
      "HIX TEXT," & vbCrLf & _ 
      "MMP TEXT," & vbCrLf & _ 
      "[CMS Part D (CY " & intYear & ")] TEXT," & vbCrLf & _ 
      "[CMS Part D (CY " & (intYear + 1) & ")] TEXT" & vbCrLf & _ 
      ");" 


strCount = "INSERT INTO MarketSegmentTotals([State Medicaid], [Commercial], [HIX], [MMP], [CMS Part D (CY " & intYear & ")], [CMS Part D (CY " & (intYear + 1) & ")]) " & _ 
"SELECT A.cnt, B.cnt, C.cnt, D.cnt, E.cnt " & _ 
"FROM (" & _ 
    "SELECT COUNT([FORMULARY ID]) as cnt " & _ 
    "FROM ImportMetricsIDs " & _ 
    "WHERE [Market Segment]= 'State Medicaid' " & _ 
") AS A " & _ 
", (" & _ 
    "SELECT COUNT([FORMULARY ID]) as cnt " & _ 
    "FROM ImportMetricsIDs " & _ 
    "WHERE [Market Segment]= 'Commercial' " & _ 
") as B " & _ 
", (" & _ 
    "SELECT COUNT([FORMULARY ID]) as cnt " & _ 
    "FROM ImportMetricsIDs " & _ 
    "WHERE [Market Segment]= 'HIX' " & _ 
") AS C " & _ 
", (" & _ 
    "SELECT COUNT([FORMULARY ID]) as cnt " & _ 
    "FROM ImportMetricsIDs " & _ 
    "WHERE [Market Segment]= 'MMP' " & _ 
") AS D " 

回答

3

VBA限制通過換行符連接的行數。

您可以更改此設置以重新定義變量以斷開續行數。

strCount = "blahblahblah" & _ 
    "moreblahblahblah" & _ 
    "lastblahforabit" 
strCount = strCount & "evenmoreblah" & _ 
    "toomuchblahblahblah" 

或者完全消除行延續。

strCount = "blahblahblah" 
strCount = strCount & "moreblahblahblah" 
strCount = strCount & "lastblahforabit" 
strCount = strCount & "evenmoreblah" 
strCount = strCount & "toomuchblahblahblah" 
+0

有趣的factiod。這種行爲不完全在語言規範中。 [實現可能會限制中的字符數量。](http://msdn.microsoft.com/zh-cn/library/ee156843.aspx) – RubberDuck

+1

它沒有很好記錄,但在那裏在幾個不同的地方提到它。 [One](http://msdn.microsoft.com/en-us/library/office/gg264236%28v=office.15%29.aspx)[Two](http://support2.microsoft.com/?kbid = 141513)。它似乎並不一致,因爲第一個鏈接表明限制是25,但問題的代碼沒有超過這個限制,但仍然是錯誤。 – KevenDenen