数据库建议VBA宏 - MS访问

时间:2012-01-28 07:21:01

标签: vba excel-vba ms-access ms-access-2007 excel

需要数据库建议。 我基本上需要一个宏,它将对超过100万个关键字的数据库进行vLookup表(约50K记录)。

如果我使用MS Access 2007作为vLookup的数据库,这个过程会变得更快吗?有没有其他方法可以通过使用不同的数据库等来加快此过程的速度?

感谢任何帮助或指示。谢谢大家的时间。

1 个答案:

答案 0 :(得分:3)

如果您已将这些关键字存储在Excel中,则值得查看ADO以将单词作为表格读取。这是一个快速示例,但它也很简单,可以加入要在INNER或LEFT JOIN中查找的单词列表,并找到缺失和匹配的单词。如果关键字通过使用内联连接字符串存储在数据库外部,也可以这样做。

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

''This is not the best way to refer to the workbook
''you want, but it is very convenient for notes
''It is probably best to use the name of the workbook.

strFile = ActiveWorkbook.FullName

''Note that if HDR=No, F1,F2 etc are used for column names,
''if HDR=Yes, the names in the first row of the range
''can be used. 
''
''This is the ACE connection string, you can get more
''here : http://www.connectionstrings.com/excel

strCon = "Provider=Microsoft.Jet.OLEDB.12.0;Data Source=" & strFile _
    & ";Extended Properties=""Excel 12.0;HDR=Yes;IMEX=1"";"

''Late binding, so no reference is needed

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

cn.Open strCon

''Watch out for case sensitivity
strSQL = "SELECT [ColumnName] " _
       & "FROM [Sheet1$] " _
       & "WHERE ColumnName ='" & strWord & "'"

rs.Open strSQL, cn, 3, 3

MsgBox rs.GetString

''Tidy up
rs.Close
Set rs=Nothing
cn.Close
Set cn=Nothing