sql从表单文本框检索数据

yh2wf1be  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(608)

对不起,我是vba中的noobie,我在ms access中的窗体中创建了一个按钮,它根据我在窗体中的输入从表中检索数据。文本框是name quotence\u ref,下面是我的代码。where语句我想使用我的文本框输入作为筛选不相关条目的标准。但它返回错误号:3061=参数太少。应为1。我可以帮助我在下面的代码吗?谢谢!

Dim xlApp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
Dim SQL As String
Dim rs1 As DAO.Recordset
Dim i As Integer

'Show user work is being performed
DoCmd.Hourglass True

'*********************************************
'              RETRIEVE DATA
'*********************************************

SQL = "SELECT Quotation_Detail.[Quotation_Ref] AS [Quotation_Ref], Quotation_Detail.[L/I] AS [L/I], Quotation_Detail.Qty AS [QTY], Quotation_Detail.[U/I] AS [U/I], Quotation_Detail.[P/N] AS [P/N], " & _
"Quotation_Detail.Description AS [Description], Quotation_Detail.MFR AS [MFR], Quotation_Detail.MFC AS [MFC], Quotation_Detail.[A/C] AS [A/C] " & _
"FROM Quotation INNER JOIN Quotation_Detail ON Quotation.[Quotation_Ref] = Quotation_Detail.[Quotation_Ref]" & _
"WHERE [Quotation].[Quotation_Ref] = Me!Quotation_Ref.Text"

'Execute query and populate recordset
Set rs1 = CurrentDb.OpenRecordset(SQL, dbOpenSnapshot)
ogq8wdun

ogq8wdun1#

您需要将文本框中的值连接到sql字符串中:

SQL = "SELECT Quotation_Detail.[Quotation_Ref] AS [Quotation_Ref], Quotation_Detail.[L/I] AS [L/I], Quotation_Detail.Qty AS [QTY], Quotation_Detail.[U/I] AS [U/I], Quotation_Detail.[P/N] AS [P/N], " & _
"Quotation_Detail.Description AS [Description], Quotation_Detail.MFR AS [MFR], Quotation_Detail.MFC AS [MFC], Quotation_Detail.[A/C] AS [A/C] " & _
"FROM Quotation INNER JOIN Quotation_Detail ON Quotation.[Quotation_Ref] = Quotation_Detail.[Quotation_Ref]" & _
"WHERE [Quotation].[Quotation_Ref] ='" &  Me!Quotation_Ref & "';"

我是这么想的 Quotation_Ref 是一个文本字符串,所以我用单引号将其括起来-如果是数字,则不需要这些引号。另外,你应该使用 .Value 控件的属性( .Text 仅用于从控件中获取“未提交”的数据),由于这是默认属性,因此可以忽略它。
当做,

相关问题