Hi
I want to display data in Excel that is pulled from an Access database. The aim is to display a cross tab query that shows a weeks worth of data regardless of whether there is data for every day of the week (always display five day columns even if Tuesday has no data...)
My solution was to create a table which stores days of the week, a user defined public function in an Access module which accepts a date parameter for the first day of the week and depending on the week day number passed to it, returns the date of each day.
This works great when running the cross tab query in access and I can see a summarised total for each week day. However, when I call the cross tab query from Excel VBA I get the error: "Undefined function ‘getDates’ in expression"
Details of the Access setup below.
Searches on different forums have found several people with the same error but no solution. There are a couple people saying it simply is not possible to call an Access function from within a query when calling that query from outside Access.
But I live in hope...! Is there a way of running an Access query containing a function from Excel VBA?
Thanks
Mark
Query: qryDay
tbl_Day contains:
PK_Day Day_Name
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
I want to display data in Excel that is pulled from an Access database. The aim is to display a cross tab query that shows a weeks worth of data regardless of whether there is data for every day of the week (always display five day columns even if Tuesday has no data...)
My solution was to create a table which stores days of the week, a user defined public function in an Access module which accepts a date parameter for the first day of the week and depending on the week day number passed to it, returns the date of each day.
This works great when running the cross tab query in access and I can see a summarised total for each week day. However, when I call the cross tab query from Excel VBA I get the error: "Undefined function ‘getDates’ in expression"
Details of the Access setup below.
Searches on different forums have found several people with the same error but no solution. There are a couple people saying it simply is not possible to call an Access function from within a query when calling that query from outside Access.
But I live in hope...! Is there a way of running an Access query containing a function from Excel VBA?
Thanks
Mark
Query: qryDay
Code:
PARAMETERS [setDate:] DateTime;
SELECT tbl_Day.PK_Day, tbl_Day.Day_Name, getWeekDates([setDate:],[PK_Day]) AS DayDate
FROM tbl_Day
WHERE (((tbl_Day.PK_Day)<6));
Code:
Public Function getWeekDates(dtSetDate As Date, lngDayOfWeek As Long) As String
'// function to return a weeks worth of dates based on parameters supplied
'// to enable query to return employee hours based on shift pattern
'//assumption is that date will always be a Monday!
getDates = DateAdd("d", (lngDayOfWeek - 1), dtSetDate)
End Function
tbl_Day contains:
PK_Day Day_Name
1 Monday
2 Tuesday
3 Wednesday
4 Thursday
5 Friday
6 Saturday
7 Sunday
Code:
Dim con As ADODB.Connection
Dim rst As ADODB.Recordset
Dim cmd As New ADODB.Command
Dim strDB As String
strDB = "Data Source=" & strDB_Path 'defined elsewhere
Set con = New ADODB.Connection
con.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strDB_Path & ";"
Set cmd.ActiveConnection = con
cmd.CommandText = "qryDay"
Set rst = cmd.Execute(, Array(CDate("21/01/2013")), adCmdStoredProc)
'fails with error at this line
Last edited: