2017-09-27 56 views
0

我需要將這個訪問SQL查詢到VBA查詢 - >轉換訪問SQL要VBA

SELECT informationTable.userID, 
ConcatRelated('itemSold','[informationTable]',"userID='" & [userID] & "'") AS NameOfItemSold 
FROM informationTable 
GROUP BY informationTable.userID; 

我試過的部份VBA

DoCmd.RunSQL ("SELECT informationTable.userID, 
ConcatRelated('itemsold','[informationTable]','userID= ' & Chr(34) & [userID] & Chr(34) & ') AS NameOfItemSold 
Into CRInformationTable 
FROM informationTable 
GROUP BY informationTable.userID;") 

,但我得到的

錯誤

RunSQL操作需要一個包含SQL語句的參數

+2

哪裏是你的代碼的其餘部分?您向我們展示的唯一東西是SQL語句。您將SQL語句與VBA代碼組合在一起,因爲VBA不是一種查詢類型。 – braX

+0

您必須將SQL語句轉換爲VBA'string'類型變量,並且有特定的語法。 –

+3

另外,不能'運行'一個SELECT sql。 SELECT sql用於設置RecordSource對象或表單/報告RecordSource屬性。只有sql可以運行 - UPDATE,INSERT,DELETE,SELECT INTO。您可以打開一個SELECT查詢對象。但爲什麼打開查詢對象?爲什麼不使用查詢創建報告作爲RecordSource? – June7

回答

0

我做了一些測試。假設用戶ID是數字類型字段,看看這對你的作品:

DoCmd.RunSQL ("SELECT DISTINCT informationTable.userID, " & _ 
    "ConcatRelated('itemsold','[informationTable]','userID=' & [userID]) AS NameOfItemSold " & _ 
    "INTO CRInformationTable FROM informationTable;") 

如果用戶ID是文本類型:

"ConcatRelated('itemsold','[informationTable]','userID=" & Chr(34) & "' & [userID] & '" & Chr(34) & "') AS NameOfItemSold " & _ 

人權委員會,而不是(34):

"ConcatRelated('itemsold','[informationTable]','userID=""' & [userID] & '""') AS NameOfItemSold " & _ 
+0

[userID]是一個簡短的文本類型字段。 – BellHopByDayAmetuerCoderByNigh

+0

哎呀!你有沒有發現我的額外)錯字?再次編輯答案。 – June7

0

我我已經多次使用這個漂亮的工具,並取得了巨大的成功。

Creating the form 

The form just needs two text boxes, and a command button. SQL statements can be quite long, so you put the text boxes on different pages of a tab control. 

    Create a new form (in design view.) 
    Add a tab control. 
    In the first page of the tab control, add a unbound text box. 
    Set its Name property to txtSql. 
    Increase its Height and Width so you can see many long lines at once. 
    In the second page of the tab control, add another unbound text box. 
    Name it txtVBA, and increase its height and width. 
    Above the tab control, add a command button. 
    Name it cmdSql2Vba. 
    Set its On Click property to [Event Procedure]. 
    Click the Build button (...) beside this property. 
    When Access opens the code window, set up the code like this: 

    Private Sub cmdSql2Vba_Click() 
     Dim strSql As String 
     'Purpose: Convert a SQL statement into a string to paste into VBA code. 
     Const strcLineEnd = " "" & vbCrLf & _" & vbCrLf & """" 

     If IsNull(Me.txtSQL) Then 
      Beep 
     Else 
      strSql = Me.txtSQL 
      strSql = Replace(strSql, """", """""") 'Double up any quotes. 
      strSql = Replace(strSql, vbCrLf, strcLineEnd) 
      strSql = "strSql = """ & strSql & """" 
      Me.txtVBA = strSql 
      Me.txtVBA.SetFocus 
      RunCommand acCmdCopy 
     End If 
    End Sub 

Using the form 

To use the form: 

    Open your query in SQL View, and copy the SQL statement to clipboard (Ctrl+C.) 
    Paste into the first text box (Ctrl+V.) 
    Click the button. 
    Paste into a new line in your VBA procedure (Ctrl+V.) 

enter image description here

http://allenbrowne.com/ser-71.html