我正在尝试使用一个函数在Access表中查找Excel单元格中的值,并从Access表中匹配行中的列返回值。
我正在使用下面的代码,但它只是继续返回#value,即使我在Access数据库中得到完全匹配。
Dim adoCN As ADODB.Connection
Dim strSQL As String
Const DatabasePath As String = "\\aur\hobo_data\Corporate\Corporate\3DOCK2
\D_IMA\Teams\Data Architecture and Management\Projects\Payments Transformation\02 -
Documents\23 - Data Architecture Deliverables\11 - ODS & Data Mart Model\04 Fundtech
Data Provision\Payments Transformation.mdb"
'Function argument descriptions
'LookupFieldName - the field you wish to search
'LookupValue - the value in LookupFieldName you're searching for
'ReturnField - the matching field containing the value you wish to return
Public Function DBVLookUp(TableName As String, _
LookUpFieldName As Long, _
LookupValue As String, _
ReturnField As String) As Variant
Dim adoRS As ADODB.Recordset
If adoCN Is Nothing Then SetUpConnection
Set adoRS = New ADODB.Recordset
strSQL = "SELECT [" & LookUpFieldName & "], [" & ReturnField & _
"] FROM [" & TableName & _
"] WHERE [" & LookUpFieldName & "]='" & LookupValue & "';"
'If lookup value is a number, then remove the two
adoRS.Open strSQL, adoCN, adOpenForwardOnly, adLockReadOnly
If adoRS.BOF And adoRS.EOF Then
DBVLookUp = "Value not Found"
Else
DBVLookUp = adoRS.Fields(ReturnField).Value
End If
adoRS.Close
End Function
Sub SetUpConnection()
On Error GoTo ErrHandler
Set adoCN = New Connection
adoCN.Provider = "Microsoft.Jet.OLEDB.4.0" 'Change to 3.51 for Access 97
adoCN.ConnectionString = DatabasePath
adoCN.Open
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation, "An error occurred"
End Sub