2012-02-03 116 views
0

我想根据两个参数从Access导入数据到Excel。我有一个指定项目编号(参数1)和工具类型(参数2)的工具列表。如何过滤出不符合用户输入这两个参数的工具?基于两个参数从Access表导入到Excel表

我看到这个线程: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。

谢谢!

回答

1

如果你只是想在SelectedProj添加到SQL语句中,这应该是招(其中项目类型是字段的名称):

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

所选属性返回True如果该项目被选中这在你上面的例子中没有意义。也许你正在寻找的东西像

SelectedTool = Tools_listbox.Items(Tools_listbox.SelectedItem) 

注意你还没有为SelectedTool声明这是调皮,但我想它应该是在这种情况下,你不应该使用Set的字符串。