vba - How to write an Excel function which returns a value from an ACCESS database? -
i have database in access 2013
date | data
01/06/2015 | 1
02/06/2015 | 2
and want write function returns values db.
=getdata("fordate") is possible ? have tried doesn't want work
public function getdata(id date) string set oconnection = new adodb.connection dim orecordset ador.recordset oconnection.open "microsoft.ace.oledb.12.0;data source=c:\users\user\desktop\cainv_db.accdb;" & "trusted_connection=yes;" set orecordset = oconnection.execute("select " & rdate & " cb_exchange rdate = " & id) if orecordset.eof getdata = "n/a" else getdata = orecordset(1) end if end function
so need use code (insert database path):
public function getdata(whatdate date) variant dim db database dim rs recordset set db = dbengine.opendatabase("c:\temp\desktop\test.mdb") set rs = db.openrecordset("select usd cb_exchange rdate = #" & format(whatdate, "m\/d\/yyyy") & "#", dbopendynaset) ' date format must if rs.recordcount > 0 rs.movefirst getdata = rs!usd ' of course must enter correct column name end if end function
Comments
Post a Comment