PDA

View Full Version : Dlookup only works when Access is open



AAhrenst
04-10-2018, 12:05 PM
Dear members,

I have an issue with using DLookup for a Access databasefrom Excel. I admit I don’t have much experience but I am using DAO object andworks fine for me using Dlookup, .OpenRecordset, and looping though the recordset with findnext etc. However, for Dloopup it only works when Access is openwhich is not very practical. I get error 2950 on Dlookup. “Application defined or object defined error”. Open Access and it works again?
I got add the MS Access 16.0 Object Library and the MSDAO 3.6 Object Library. Been searching the net for this particular issue but I don’tsee any reference.

In summary:
Public db As DAO.Database
Public dbT As DAO.RecordSet
Set db = DBEngine.OpenDatabase("C:\Users\MyName\DBName.accdb")
Set dbT = db.OpenRecordset("Table1",dbOpenDynaset, dbReadOnly)
Dim varX as sting

TiD = "1-123456"
LookupT = "[Some Type]"
Criteria = "[Order ID] = '" & TiD &"'"
with dbT
varX = DLookup(LookupT, "Table1",Criteria)
end with

also db.close does not seem to close Access. Need to close Excel to edit database.

Thanks and regards
Alex