【问题标题】:Import to Excel from Access table based on two parameters根据两个参数从 Access 表导入 Excel
【发布时间】:2012-02-26 09:55:13
【问题描述】:

我正在尝试基于两个参数将数据从 Access 导入 Excel。我有一个工具列表,其中指定了项目编号(参数 1)和工具类型(参数 2)。如何过滤掉不满足用户输入这两个参数的工具?

我看到了这个帖子:Import to Excel from Access table based on parameters

但它没有谈论多个参数。这是我目前所处的位置:

    Dim cn As Object
Dim rs As Object
Dim strFile As String
Dim strCon As String
Dim strSQL As String
Dim s As String
Dim i As Integer, j As Integer

''Access database

strFile = "D:\Tool_Database\Tool_Database.mdb"

''This is the Jet 4 connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strFile & ";"

''Late binding, so no reference is needed

Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

cn.Open strCon

'Find the name of the tool that was selected
Dim SelectedTool As String, SelectedProj
Set SelectedTool = Tools_ListBox.Selected
Set SelectedProj = Project_ListBox.Selected

strSQL = "SELECT * " _
        & "FROM ToolFiles " _
        & "WHERE Tool_Name = '" & SelectedTool & "'"

rs.Open strSQL, cn, 3, 3

Worksheets("ToolList").Cells(2, 1).CopyFromRecordset rs

rs.Close
Set rs = Nothing
cn.Close
Set cn = Nothing

显然 strSQL 语句是我需要集中注意力并将值插入 SelectedProj 的地方。

谢谢!

【问题讨论】:

    标签: excel ms-access vba


    【解决方案1】:

    如果您只是想将 SelectedProj 添加到 SQL 语句中,这应该是诀窍(其中 ProjectType 是字段的名称):

    strSQL = "SELECT * " _
            & "FROM ToolFiles " _
            & "WHERE Tool_Name = '" & SelectedTool & "' " _
            & "AND ProjectType = '" & SelectedProj & "'"
    

    【讨论】:

      【解决方案2】:

      如果项目被选中,则 selected 属性返回 True,这在上面的示例中没有意义。也许您正在寻找类似的东西

      SelectedTool = Tools_listbox.Items(Tools_listbox.SelectedItem)
      

      请注意,您也没有针对 SelectedTool 的声明,这很顽皮,但我想它应该是一个字符串,在这种情况下您不应该使用 Set。

      【讨论】:

        猜你喜欢
        • 2012-01-18
        • 2014-11-19
        • 1970-01-01
        • 2019-11-28
        • 1970-01-01
        • 2011-12-06
        • 1970-01-01
        • 2010-12-04
        • 1970-01-01
        相关资源
        最近更新 更多