2012-02-03 115 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的字符串。