簡體   English   中英

MS Access SQL語句進行追加

[英]MS Access SQL statement for append

我對MS Access和SQL比較陌生。

我在訪問數據庫中有一個查詢,用於將一個表追加到另一個表,並按特定的列升序排列,然后使用該列添加列,以分配唯一的標識符。

查詢本身在訪問中運行良好,但是,我想將其寫入表單的VBA代碼中,以便在單擊按鈕時進行操作。

Access中的SQL視圖為我提供了以下代碼,但是當我將其寫入代碼時,則表明缺少運算符,有人可以提出建議嗎?

碼:

DoCmd.RunSQL ("INSERT INTO tblImport ( CustomerAccount, OrderReference, OrderDate, DueDate, CustomerRef," & _
    "StockNo, OrderQuantity, AddressName, Add1, Add2, Add3, Town, County, PostCode, Country, ServiceLine )" & _
    "SELECT tblImport2.CustomerAccount, tblImport2.OrderReference, tblImport2.OrderDate, tblImport2.DueDate," & _
    "tblImport2.CustomerRef, tblImport2.StockNo, tblImport2.OrderQuantity, tblImport2.AddressName, tblImport2.Add1," & _
    "tblImport2.Add2, tblImport2.Add3, tblImport2.Town, tblImport2.County, tblImport2.PostCode, tblImport2.Country," & _
    "tblImport2.ServiceLine" & _
    "FROM tblImport2" & _
    "ORDER BY tblImport2.OrderReference")

保存查詢。 然后可以從代碼中調用它:

CurrentDb.QueryDefs("NameOfYourAppendQuery").Execute

還是沒有查詢:

Dim Sql As String

Sql = "<Your SQL string>"
Debug.Print Sql     ' Check SQL.
CurrentDb.Execute Sql

您是否知道在VBA中指定多行字符串(如此處的查詢),您需要像這樣將這些行連接在一起? 否則, VBA不會知道它應該是單個字符串。

mySql = "INSERT INTO tblImport ( CustomerAccount, OrderReference, OrderDate, DueDate, CustomerRef, StockNo, OrderQuantity, AddressName, Add1, Add2, Add3, Town, County, PostCode, Country, ServiceLine ) " & _
"SELECT tblImport2.CustomerAccount, tblImport2.OrderReference, tblImport2.OrderDate, tblImport2.DueDate, tblImport2.CustomerRef, tblImport2.StockNo, tblImport2.OrderQuantity, tblImport2.AddressName, tblImport2.Add1, tblImport2.Add2, tblImport2.Add3, tblImport2.Town, tblImport2.County, tblImport2.PostCode, tblImport2.Country, " & _
"tblImport2.ServiceLine " & _
"FROM tblImport2 " & _
"ORDER BY tblImport2.OrderReference; "

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM