1。我收到“运行时错误'1004':无当前记录。”我尝试了!Customers.Address我尝试了.Fields(5)我也试过了![Customers.Address]但是它们都不起作用。在尝试访问数据之前,我先打印出数据。它成功了,我打印出了我期望的报价的公司地址。
2。我试图一次使用一个引号,并使用输入引号。因此,我在代码中添加了“ WHERE”子句(已被注释掉)但是,它不起作用。
这里是代码(我在复制代码时遇到格式问题。所以我使用了图像):
Dim appExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim SQL As String
Dim rsl As DAO.Recordset
Dim i As Integer
Dim Message, Title, Default, MyValue
'user input for the quotation no.
Message = "Plz Enter Quotation No" ' Set prompt.
Title = "InputBox Demo" ' Set title.
Default = "SHA-202-0001" ' Set default.
' Display message, title, and default value.
MyValue = InputBox(Message, Title, Default)
'Show user work is being performed
DoCmd.Hourglass (True)
'******************************************************
' RETRIEVE DATA
'******************************************************
'SQL statement to retrieve Article_No from Quotation_Detail table
'SQL = "SELECT Article_No AS [Article No]" & _
'"FROM Quotation_Detail " & _
'"ORDER BY Article_No "
'SQL statement to retrieve Short Name, Company, Person, Telefone, USCI Num from Customers Table and Quotation No,
'Buyer, Revision, Info, Article_No, QUantity, Matchcode and RMB_price from Quotation Query 1
SQL = "SELECT Customers.[Short Name], Customers.Company, Customers.Person, Customers.Telefone, Customers.[E-Mail]," & _
"Customers.Address , Customers.City, Customers.Postcode, Customers.Province, Customers.[USCI Num]," & _
"[Quotation Query1].Buyer , [Quotation Query1].[Our Reference]," & _
"[Quotation Query1].[Quotation Date] , [Quotation Query1].Revision, [Quotation Query1].Info," & _
"[Quotation Query1].Article_No , [Quotation Query1].Quantity, [Quotation Query1].Matchcode," & _
"[Quotation Query1].RMB_price , [Quotation Query Query].[Quotation No], [Quotation Query Query].[Sum Of total]" '& _
"FROM (Customers INNER JOIN ([Quotation Query Query] INNER JOIN Quotation ON [Quotation Query Query].[Quotation No] = Quotation.[Quotation No]) ON Customers.[Short Name] = Quotation.[Buyer]) INNER JOIN [Quotation Query1] ON Customers.[Short Name] = [Quotation Query1].[Buyer]" & _
'"WHERE [Quotation Query Query].[Quotation No] = Myvalue"
'To select only one quotation with the quotation no. to make the quotation but failed with WHERE clause
'Execute query and populate recordset
Set rsl = CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)
'If no data, don't bother opening Excel, just quit
If rsl.RecordCount = 0 Then
MsgBox "No data selected for export", vbInformation + vbOKOnly, "No data exported"
GoTo SubExit
End If
'Loop each row to print data in rsl(0) to test what's in the rsl
'rsl(0) is the short name
'rsl(1) is the company name
' Contrl G to see what have been printed
Do While Not rsl.EOF
Debug.Print rsl(5)
rsl.MoveNext
Loop
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open("C:\Users\Cindy\Desktop\Quotation_Master.xlsx")
appExcel.Visible = True
Set xlSheet = myWorkbook.Worksheets(1)
With xlSheet
.Name = "Quotation"
.Cells.Font.Name = "Calibri"
.Cells.Font.Size = 11
'build quotation info
.Range("C16").Value = MyValue
.Range("A6").Value = rsl(5)
.Range("A9").Value = rsl!Customers.Postcode
.Range("B12").Value = rsl!Customers.Telefone
.Range("B13").Value = rsl!Customers.[E-Mail]
.Range("B14").Value = rsl!Customers.[USCI Num]
.Range("F16").Value = rsl![Quotation Query1].Revision
.Range("B17").Value = rsl![Quotation Query1].[Quotation Date]
在每行末尾留一个空格。
SQL = "SELECT Customers.[Short Name], Customers.Company, Customers.Person, Customers.Telefone, Customers.[E-Mail]," & _
"Customers.Address , Customers.City, Customers.Postcode, Customers.Province, Customers.[USCI Num]," & _
"[Quotation Query1].Buyer , [Quotation Query1].[Our Reference]," & _
"[Quotation Query1].[Quotation Date] , [Quotation Query1].Revision, [Quotation Query1].Info," & _
"[Quotation Query1].Article_No , [Quotation Query1].Quantity, [Quotation Query1].Matchcode," & _
"[Quotation Query1].RMB_price , [Quotation Query Query].[Quotation No], [Quotation Query Query].[Sum Of total]" & _
" FROM (Customers INNER JOIN ([Quotation Query Query] INNER JOIN Quotation ON [Quotation Query Query].[Quotation No] = Quotation.[Quotation No]) ON Customers.[Short Name] = Quotation.[Buyer]) INNER JOIN [Quotation Query1] ON Customers.[Short Name] = [Quotation Query1].[Buyer] " & _
" WHERE [Quotation Query Query].[Quotation No] = '" & Myvalue & "' "
连接到sql字符时存在错误的余地。如果可能的话,连接字符时最好在末尾留一个空格。
[Quotation Query Query]. [Sum Of total]FROM (Customers INNER JOIN
由于以这种方式编写,因此发生错误。
而且,由于您经历了EOF调试记录集,因此您再也不会获得记录集。因此,您应该删除调试代码或第一次将记录集移到下面。
Do While Not rsl.EOF
Debug.Print rsl(5)
rsl.MoveNext
Loop
rsl.MoveFirst '<~~ this code needs