简体   繁体   English

如何使用VBA动态SQL SELECT语句调用MS Access参数查询

[英]How to call MS Access Parameter queries using VBA dynamic SQL SELECT statements

I've searched MSDN, StackOverflow, SQLServer Central and too many sites to mention. 我搜索了MSDN,StackOverflow,SQLServer Central和太多要提及的站点。 I've been trying for a couple of days to find a way to execute parameter queries in MS Access using dynamic SQL SELECT statements in Excel VBA code. 我已经尝试了几天,以找到一种使用Excel VBA代码中的动态SQL SELECT语句在MS Access中执行参数查询的方法。 The system I'm using works extremely well with SQL Server TVFs, but I have to convert it to Access due to losing server support. 我正在使用的系统与SQL Server TVF极为兼容,但是由于失去服务器支持,我不得不将其转换为Access。 The VBA code starts by looping through an 'input' sheet in Excel picking up parameter values and function/query names that is used to build dynamic SQL SELECT statements. VBA代码首先循环浏览Excel中的“输入”表,以拾取用于构建动态SQL SELECT语句的参数值和函数/查询名称。 Here is the code that builds the connection and calls the Access Query for just one of the queries (there are 20) that requires only 1 input paramter: 以下是构建连接并为仅需要一个输入参数的查询之一(有20个)调用访问查询的代码:

Dim strSQL

' set up our connection
strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\PathToDB Tables 2013-12-13.accdb;"
Set conn = CreateObject("ADODB.Connection")
conn.Open strConnection
' open the view and create the report
Set rs = CreateObject("ADODB.recordset")
Application.ODBCTimeout = 0
conn.CommandTimeout = 0
strSQL = "select * FROM "
strSQL = strSQL & strFunction
strSQL = strSQL & " (" & strParameters & ");"
'strSQL = strSQL & strOrderBy
rs.Open strSQL, conn

The value in strSQL at this point is SELECT * FROM Report_1_2_StaffAdds (#12/31/2013#); 此时,strSQL中的值为SELECT * FROM Report_1_2_StaffAdds (#12/31/2013#);

The error occurs in the line rs.Open strSQL, conn With the error message 'Syntax error in FROM clause.' 错误发生在rs.Open strSQL,conn行中,错误消息为“ FROM子句中的语法错误”。

Here is the MS Access Query: (Query name is Report_1_2_StaffAdds) 这是MS Access查询:(查询名称为Report_1_2_StaffAdds)

PARAMETERS [previous_month] DateTime; SELECT [1_2_StaffAddsPart1].Unit, [1_2_StaffAddsPart1].Role, [1_2_StaffAddsPart1].Start_Date, [1_2_StaffAddsPart2].First_Worked, [1_2_StaffAddsPart2].Last_Worked, [1_2_StaffAddsPart1].Emp_Name, [1_2_StaffAddsPart1].Emp_Id, [1_2_StaffAddsPart2].Hours_to_Date FROM 1_2_StaffAddsPart1 INNER JOIN 1_2_StaffAddsPart2 ON [1_2_StaffAddsPart1].Emp_Id = [1_2_StaffAddsPart2].Emp_Id;

Any help will be much appreciated. 任何帮助都感激不尽。 I believe if I can get an answer to this, I can revise it to include up to 3 input parameters, depending on which Access query is being executed at a given time. 我相信,如果我能得到答案,我可以对其进行修改,使其最多包含3个输入参数,具体取决于在给定时间正在执行哪个Access查询。

It looks to me like you're attempting to include the parameter value in the string which contains your SELECT statement. 在我看来,您正在尝试将参数值包含在包含SELECT语句的字符串中。 However, I'm not sure whether I understand what you're trying to do, so I'll offer you a simple tested parameter query which opens a recordset from an ADODB.Command object. 但是,我不确定是否了解您要执行的操作,因此将为您提供一个经过测试的简单参数查询,该查询从ADODB.Command对象中打开一个记录集。

This is the Immediate window output from the code below: 这是下面代码的立即窗口输出:

PARAMETERS which_date DateTime;
SELECT * FROM tblFoo
WHERE datetime_field = [which_date];
id            datetime_field
 27           2/11/2014 10:16:58 AM 

Code: 码:

Dim cmd As Object ' ADODB.Command
Dim conn As Object ' ADODB.Connection
Dim rs As Object ' ADODB.Recordset
Dim strConnection As String
Dim strSql As String

strConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
    "Data Source=C:\share\Access\database1.mdb;"
Set conn = CreateObject("ADODB.Connection")
conn.Open strConnection

strSql = "PARAMETERS which_date DateTime;" & vbCrLf & _
    "SELECT * FROM tblFoo" & vbCrLf & _
    "WHERE datetime_field = [which_date];"
Debug.Print strSql

Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = conn
cmd.CommandText = strSql
Set rs = cmd.Execute(, CDate("2/11/2014 10:16:58 AM"))
With rs
    If Not (.BOF And .EOF) Then
        Debug.Print "id", "datetime_field"
        Debug.Print !id, !datetime_field
    End If
    .Close
End With

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM