Click here to Skip to main content
15,917,174 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying to build a report that builds a pdf file. I'm using a console app that uses MS Access as database. Within the Access database I call a query that contains a user function that performs calculations. That where the issue comes in. Every time I run the console app I get the following error message:

Undefined function 'FldVal' in expression


The query containing the 'FldVal' function runs perfectly from within Access but not from the console app. Here is the code that blows-up:

public static DataSet Get941Data(string rpt, int year, string qtr)
{
    DataSet ds = new DataSet();
    string CommandText = "SELECT  * FROM  qry941PDF";

    using (OleDbConnection connection = new OleDbConnection(DigiplexPDFs.AppSettings.GetAccessPathConnectionString()))
    {
        using (OleDbCommand cmd = new OleDbCommand(CommandText, connection))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("qtr", qtr);
            cmd.Parameters.AddWithValue("rpt", rpt + year);
            cmd.Parameters.AddWithValue("year", year);

            connection.Open();

            OleDbDataAdapter adapter = new OleDbDataAdapter(cmd);

            adapter.Fill(ds);

            return ds;
        }
    }
}


It crashes on the adapter.Fill(ds) line. Somehow I'm not able to run this query with a function. Is there anyway around this?

CORRECTION: If the above is incorrect I'd like a different approach. Is there a Stored Procedure type call to Access available where I can pass parameters?
Posted
Updated 8-Sep-11 11:21am
v2

You have defined three parameters into your command but there are no placeholders in the actual SQL statement? Should you have something like:
C#
string CommandText = "SELECT  * FROM  qry941PDF WHERE SomeField = ? AND AnotherField = ? ... ";


Also it's not advisable to use * in the query. A better practice is to list the columns you want to fetch.
 
Share this answer
 
Comments
ehwash 8-Sep-11 17:18pm    
Maybe I should not be using a commandText object. The query is expecting parameters like a stored procedure. A better question perhaps is can Access handle a stored procedure type call from a console app?
Wendelius 8-Sep-11 17:27pm    
Ok, so qry941PDF is stored query. In that case using procedures is much better approach. If I recall correctly the stored procedure may not have output parameters but it can return results using a query inside the procedure. So yes, just define a procedure with correct parameters and give it a try.
ehwash 8-Sep-11 17:58pm    
Yeah, I just tried that. to no avail. I still get the function call is undefined. Within qry941PDF is a user-defined function 'FldVal' It does the work. I've even declared the function public but it still comes back undefined.
Wendelius 9-Sep-11 1:02am    
Did you define parameters for the stored procedure and in the procedure passed these to your query? Another option could be that you con't use the stored query inside the procedure, but re-write it as a normal parameterized SQL query.
Found a solution. However, not the one I was looking for. As it turns out an User-Defined Function [UDF] can not be apart of a query being accessed externally. My query runs fine within Access but if called via an external connection an undefined function exception is thrown. Bummer!!

I like using UDFs in Access because they make for cleaner queries and run fairly fast.

So to get around this limitation, eh feature, I rewrote a couple of queries to use iif (Immediate If) statements and removed the function calls, not as pretty. Then I had to make two calls to Access from my console app. The first to get the keys and the second to calculate the values. Its not as fast nor as elegant but it works.

Thanks Mika for your help.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900