Getting a value from a MS Access Database

larsson7

New Member
Joined
Jul 23, 2002
Messages
18
Hello,

I was wondering if there was any code that would 'lookup' a value in a MS Access database in the same way that a Vlookup formula looks up values in tables in Excel.

Even better, if there was a fuction already written to do this.

Thankyou for your time.

Regards,

Cam
 
Correction to my previous post

Hi, I know it's been about a year since there's been activity on this thread but I'm wondering if this code can be made into an add-in so that it can be used as a user defined function in any worksheet. I was able to get the code working but when I tried to create an add-in I get a compile error - user-defined type not defined, it's refering to adoCN As ADODB.Connection. From what I researched, it's looks like a UDF needs to begin with Function and end with End Funtion. In this case, Sub SetUpConnection() is outside of that function so it doesn't seem to work.

Is it possible?

Hope that makes sense.

Thanks in advance for any advice!
 
Upvote 0

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi all,

Sorry to be yet another person to revive this ancient thread! I used the code below to implement DBVLookUp (after much frustration...if you want to hear a funny story, ask me what the ultimate problem was...). It is working fine, my only trouble is that it only accepts .mdb, which is an Access 2003 file format. Well, 2003 was awhile ago, and I'd like to be able to keep my database in 2007 format if possible, but I'm not sure how to update the code to do so. (If I just set the path to a .accdb file I get an unsurprising error that the database format is not recognized.) My guess is that I might need to activate a different reference and use a different provider in the SetUpConnection() sub, but all the ADODB object stuff is definitely above my newfound (i.e. week-old) VBA abilities!

Thanks in advance for any help!

Dim adoCN As ADODB.Connection
Dim strSQL As String

Const DatabasePath As String = "C:\Program Files\foodmart 2000.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 String, _
**************************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
 
Upvote 0
Hi all,

Sorry to be yet another person to revive this ancient thread! I used the code below to implement DBVLookUp (after much frustration...if you want to hear a funny story, ask me what the ultimate problem was...). It is working fine, my only trouble is that it only accepts .mdb, which is an Access 2003 file format. Well, 2003 was awhile ago, and I'd like to be able to keep my database in 2007 format if possible, but I'm not sure how to update the code to do so. (If I just set the path to a .accdb file I get an unsurprising error that the database format is not recognized.) My guess is that I might need to activate a different reference and use a different provider in the SetUpConnection() sub, but all the ADODB object stuff is definitely above my newfound (i.e. week-old) VBA abilities!

Thanks in advance for any help!

Figured it out! Here is an update for anyone with a similar need. I activated the Microsoft ActiveX Data Objects 6.1 Library (the highest version available on my computer), then changed
adoCN.Provider = "Microsoft.Jet.OLEDB.4.0"
to

adoCN.Provider = "Microsoft.ACE.OLEDB.12.0"

Now I can keep my database in Access 2007 format and set my DatabasePath to "C:\Users\lnicks\Desktop\database.accdb" with no problems.

Here is the updated code:

Code:
[FONT=Courier][COLOR=#00007F]Dim[/COLOR] adoCN [COLOR=#00007F]As[/COLOR] ADODB.Connection
[COLOR=#00007F]Dim[/COLOR] strSQL [COLOR=#00007F]As[/COLOR] [COLOR=#00007F]String[/COLOR]

[COLOR=#00007F]Const[/COLOR] DatabasePath [COLOR=#00007F]As[/COLOR] [COLOR=#00007F]String[/COLOR] = "C:\Program Files\databasefile.accdb"

[COLOR=#007F00]'Function argument descriptions[/COLOR]
[COLOR=#007F00]'LookupFieldName - the field you wish to search[/COLOR]
[COLOR=#007F00]'LookupValue - the value in LookupFieldName you're searching for[/COLOR]
[COLOR=#007F00]'ReturnField - the matching field containing the value you wish to return[/COLOR]

[COLOR=#00007F]Public[/COLOR] [COLOR=#00007F]Function[/COLOR] DBVLookUp(TableName [COLOR=#00007F]As[/COLOR] [COLOR=#00007F]String[/COLOR], _
**************************LookUpFieldName [COLOR=#00007F]As[/COLOR] [COLOR=#00007F]String[/COLOR], _
**************************LookupValue [COLOR=#00007F]As[/COLOR] [COLOR=#00007F]String[/COLOR], _
**************************ReturnField [COLOR=#00007F]As[/COLOR] [COLOR=#00007F]String[/COLOR]) [COLOR=#00007F]As[/COLOR] [COLOR=#00007F]Variant[/COLOR]
****[COLOR=#00007F]Dim[/COLOR] adoRS [COLOR=#00007F]As[/COLOR] ADODB.Recordset
****[COLOR=#00007F]If[/COLOR] adoCN [COLOR=#00007F]Is[/COLOR] [COLOR=#00007F]Nothing[/COLOR] [COLOR=#00007F]Then[/COLOR] SetUpConnection

****[COLOR=#00007F]Set[/COLOR] adoRS = [COLOR=#00007F]New[/COLOR] ADODB.Recordset
****strSQL = "SELECT " & LookUpFieldName & ", " & ReturnField & _
************ " FROM " & TableName & _
************ " WHERE " & LookUpFieldName & "=" & LookupValue & ";"
************ [COLOR=#007F00]' If lookup value is a number then remove the two '[/COLOR]
****adoRS.Open strSQL, adoCN, adOpenForwardOnly, adLockReadOnly
****[COLOR=#00007F]If[/COLOR] adoRS.BOF And adoRS.EOF [COLOR=#00007F]Then[/COLOR]
********DBVLookUp = "Value not Found"
****[COLOR=#00007F]Else[/COLOR]
********DBVLookUp = adoRS.Fields(ReturnField).Value
****[COLOR=#00007F]End[/COLOR] [COLOR=#00007F]If[/COLOR]
****adoRS.Close
[COLOR=#00007F]End[/COLOR] [COLOR=#00007F]Function[/COLOR]

[COLOR=#00007F]Sub[/COLOR] SetUpConnection()
****[COLOR=#00007F]On[/COLOR] [COLOR=#00007F]Error[/COLOR] [COLOR=#00007F]GoTo[/COLOR] ErrHandler
****[COLOR=#00007F]Set[/COLOR] adoCN = [COLOR=#00007F]New[/COLOR] Connection
****adoCN.Provider = "Microsoft.Jet.OLEDB.4.0"**[COLOR=#007F00]'Change to 3.51 for Access 97[/COLOR]
****adoCN.ConnectionString = DatabasePath
****adoCN.Open
****[COLOR=#00007F]Exit[/COLOR] [COLOR=#00007F]Sub[/COLOR]
ErrHandler:
****MsgBox Err.Description, vbExclamation, "An error occurred"
[COLOR=#00007F]End[/COLOR] [COLOR=#00007F]Sub[/COLOR][/FONT]
 
Upvote 0
Hi again
I am condidering whether to upgrade to Excel 2010 64 bit but I am wondering whether the procedure will work. In this version the ActiveX controls does not work.

Regards
Evdokimos
 
Upvote 0
HI

I have tried this function but only keep getting "Value not found" as my cell result even though the string value I am looking up is definitely in the table, column I have specified in the function - see code below

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 String, _
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 & '"
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"
adoCN.ConnectionString = DatabasePath
adoCN.Open
Exit Sub
ErrHandler:
MsgBox Err.Description, vbExclamation, "An error occurred"
End Sub
 
Upvote 0
Hello, I'm reviving this old thread to thank Norie and to ask for additional help. I found a case when the code doesn't work, and wanted to ask how to adjust it (otherwise, the code works). If the lookup field in the database consists of dates, and the LookupValue is a date, the function returns #VALUE!.

For example, the dates in the database are formatted as 6/9/2022 12:00:00 PM, but are actually incremented by 100 milliseconds with each entry. I tried formatting LookupValue in Excel before plugging it into the function, and formatting the dates in the database, but without success. Any suggestions?

Thank you.
 
Upvote 0

Forum statistics

Threads
1,215,757
Messages
6,126,695
Members
449,331
Latest member
smckenzie2016

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top