Excel VBA - ADO 连接到 MS Access 数据库 - 过滤结果
Excel VBA - ADO Connection to MS Access DB - Filtering results
我有一个 MS Access 数据库,其中存储了一些产品信息,并且我有这些字段:
ID、客户零件号、描述、价格、意见
我还有一个 Excel 文件,其中有一列名为 Customer Part No.,在此列中我粘贴了我要过滤的 Customer's Part。
我需要一种方法来查询我的访问数据库,使用 Excel 和 VBA 到 return 只有我在客户零件号中匹配的记录。
此刻我设法连接到数据库并检索了所有记录。但是我仍然没有弄清楚如何 select 只有我匹配的记录,我想它会在 SQL 查询中,但我不知道它会怎样。
Sub ADO_Connection()
'Creating objects of Connection and Recordset
Dim conn As New Connection, rec As New Recordset
Dim DBPATH, PRVD, connString, query As String
'Declaring fully qualified name of database. Change it with your database's location and name.
DBPATH = "C:\SourcingDatabase.accdb"
'This is the connection provider. Remember this for your interview.
PRVD = "Microsoft.ace.OLEDB.12.0;"
'This is the connection string that you will require when opening the the connection.
connString = "Provider=" & PRVD & "Data Source=" & DBPATH
'opening the connection
conn.Open connString
'the query I want to run on the database.
query = "SELECT * from Data;"
'running the query on the open connection. It will get all the data in the rec object.
rec.Open query, conn
'clearing the content of the cells
Cells.ClearContents
'getting data from the recordset if any and printing it in column A of excel sheet.
If (rec.RecordCount <> 0) Then
Do While Not rec.EOF
Range("A" & Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0).Value2 = _
rec.Fields(1).Value
rec.MoveNext
Loop
End If
'closing the connections
rec.Close
conn.Close
End Sub
找到解决方案以防有人感兴趣:
下面是我的代码:
Sub ADO_Connection()
'Creating objects of Connection and Recordset
Dim conn As New Connection, rec As New Recordset
Dim DBPATH, PRVD, connString, query, partlist As String
Dim cel As Range
Dim selectedRange As Range
Set selectedRange = Application.Selection
partlist = "("
For Each cel In selectedRange.Cells
partlist = partlist & "'" & cel.Value & "',"
Next cel
partlist = Left(partlist, Len(partlist) - 1)
partlist = partlist & ")"
MsgBox partlist
'Declaring fully qualified name of database. Change it with your database's location and name.
DBPATH = "C:\SourcingDatabase.accdb"
'This is the connection provider. Remember this for your interview.
PRVD = "Microsoft.ace.OLEDB.12.0;"
'This is the connection string that you will require when opening the the connection.
connString = "Provider=" & PRVD & "Data Source=" & DBPATH
'opening the connection
conn.Open connString
'the query I want to run on the database.
query = "SELECT * from Data WHERE CustomerPartNumber IN " & partlist & ";"
MsgBox query
'running the query on the open connection. It will get all the data in the rec object.
rec.Open query, conn
'clearing the content of the cells
Cells.ClearContents
'getting data from the recordset if any and printing it in column A of excel sheet.
If (rec.RecordCount <> 0) Then
Do While Not rec.EOF
Range("C" & Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0).Value2 = rec.Fields(1).Value
rec.MoveNext
Loop
End If
'closing the connections
rec.Close
conn.Close
End Sub
我有一个 MS Access 数据库,其中存储了一些产品信息,并且我有这些字段:
ID、客户零件号、描述、价格、意见
我还有一个 Excel 文件,其中有一列名为 Customer Part No.,在此列中我粘贴了我要过滤的 Customer's Part。
我需要一种方法来查询我的访问数据库,使用 Excel 和 VBA 到 return 只有我在客户零件号中匹配的记录。
此刻我设法连接到数据库并检索了所有记录。但是我仍然没有弄清楚如何 select 只有我匹配的记录,我想它会在 SQL 查询中,但我不知道它会怎样。
Sub ADO_Connection()
'Creating objects of Connection and Recordset
Dim conn As New Connection, rec As New Recordset
Dim DBPATH, PRVD, connString, query As String
'Declaring fully qualified name of database. Change it with your database's location and name.
DBPATH = "C:\SourcingDatabase.accdb"
'This is the connection provider. Remember this for your interview.
PRVD = "Microsoft.ace.OLEDB.12.0;"
'This is the connection string that you will require when opening the the connection.
connString = "Provider=" & PRVD & "Data Source=" & DBPATH
'opening the connection
conn.Open connString
'the query I want to run on the database.
query = "SELECT * from Data;"
'running the query on the open connection. It will get all the data in the rec object.
rec.Open query, conn
'clearing the content of the cells
Cells.ClearContents
'getting data from the recordset if any and printing it in column A of excel sheet.
If (rec.RecordCount <> 0) Then
Do While Not rec.EOF
Range("A" & Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0).Value2 = _
rec.Fields(1).Value
rec.MoveNext
Loop
End If
'closing the connections
rec.Close
conn.Close
End Sub
找到解决方案以防有人感兴趣:
下面是我的代码:
Sub ADO_Connection()
'Creating objects of Connection and Recordset
Dim conn As New Connection, rec As New Recordset
Dim DBPATH, PRVD, connString, query, partlist As String
Dim cel As Range
Dim selectedRange As Range
Set selectedRange = Application.Selection
partlist = "("
For Each cel In selectedRange.Cells
partlist = partlist & "'" & cel.Value & "',"
Next cel
partlist = Left(partlist, Len(partlist) - 1)
partlist = partlist & ")"
MsgBox partlist
'Declaring fully qualified name of database. Change it with your database's location and name.
DBPATH = "C:\SourcingDatabase.accdb"
'This is the connection provider. Remember this for your interview.
PRVD = "Microsoft.ace.OLEDB.12.0;"
'This is the connection string that you will require when opening the the connection.
connString = "Provider=" & PRVD & "Data Source=" & DBPATH
'opening the connection
conn.Open connString
'the query I want to run on the database.
query = "SELECT * from Data WHERE CustomerPartNumber IN " & partlist & ";"
MsgBox query
'running the query on the open connection. It will get all the data in the rec object.
rec.Open query, conn
'clearing the content of the cells
Cells.ClearContents
'getting data from the recordset if any and printing it in column A of excel sheet.
If (rec.RecordCount <> 0) Then
Do While Not rec.EOF
Range("C" & Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0).Value2 = rec.Fields(1).Value
rec.MoveNext
Loop
End If
'closing the connections
rec.Close
conn.Close
End Sub