Hi

I have a c# console app that calls some access 2007 queries. One of these queries throws an error "Undefined function 'MonthName' in expression".

I understand that some functions that are available in VBA cant be called from c# and if i comment out the 'monthname' function the call works fine. However it would be better if i could find a way to work round the issue instead of deleting the function.

Can anyone shed any light?

Many thanks.

Dani AI

Generated

Short summary and cause: 's error happens because MonthName is an Access/VBA runtime function that Access hosts when you open the .mdb/.accdb in the Access app, but the Jet/ACE engine used by OLEDB/ODBC from a C# process does not expose all VBA functions. Queries that rely on those VBA-only functions will raise "Undefined function 'MonthName' in expression" when run outside the Access host. 's suggestion to do the conversion in C# is the simplest, most robust fix.

Recommended quick fixes

  • Change the stored query to return a month number (or the raw date) instead of trying to return a month name, then format it in the C# client.
  • Alternatively, add a small 12-row lookup table (month number → month name) in Access and join to it from the query — that keeps everything SQL-only and will work over OLEDB.

C# examples (handle DBNulls and culture)

// from a DateTime field
if (reader["DDStartDate"] != DBNull.Value) {
    DateTime dt = (DateTime)reader["DDStartDate"];
    string monthName = dt.ToString("MMMM", CultureInfo.CurrentCulture);
}

// from an integer month number returned by the query
int m = Convert.ToInt32(reader["MonthNumber"]);
string monthName = CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(m);

Other notes

  • Creating a public VBA UDF and expecting it to be callable over OLEDB generally won’t work unless the query is executed inside an Access instance (COM automation), which is heavyweight and not recommended for server or console apps.
  • Watch for NULL/DBNull values when the IIf expression in the query returns Null.

Recommended Answers

All 2 Replies

What's the query and can't you just get the month name in C# if you need to?

What's the query and can't you just get the month name in C# if you need to?

Hi Momerath,

yes I think that may be the way to go. I was just wondering if there was something in c# that i was missing!

The query is below

GFMonthName: IIf([Frequency]=1 And [Type]="Regular",MonthName(DatePart("m",[DD Start Date])),Null)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.