custom access 2002 sql function not working (1 Viewer)

spacether

New member
Local time
Tomorrow, 08:13
Joined
Oct 1, 2005
Messages
6
I found a function to concatenate the results of a sql query. I want to use it as an aggregate string function in a sql statement. The function is:

module: 'modConcat' code:
------------------------------------------------

Code:
Option Compare Database
Option Explicit

Public Function Concat(pstrSQL As String, Optional pstrDelim As String = ",") As String
    'example
    'tblFamily with FamID as numeric primary key
    'tblFamMem with FamID, FirstName, DOB,...
    'return a comma separated list of FirstNames
    'for a FamID
    '    John, Mary, Susan
    'in a Query
    'SELECT FamID,
    'Concatenate("SELECT FirstName FROM tblFamMem
    '     WHERE FamID =" & [FamID]) as FirstNames
    'FROM tblFamily
    '

    '======For DAO uncomment next 4 lines=======
    '======     comment out ADO below    =======
    'Dim db As DAO.Database
    'Dim rs As DAO.Recordset
    'Set db = CurrentDb
    'Set rs = db.OpenRecordset(pstrSQL)

    '======For ADO uncomment next two lines=====
    '======     comment out DAO above     ======
    Dim rs As New ADODB.Recordset
    'MsgBox pstrSQL
    rs.Open pstrSQL, CurrentProject.Connection, _
            adOpenKeyset, adLockOptimistic
    Dim strConcat As String 'build return string
    With rs
        If Not .EOF Then
            .MoveFirst
            Do While Not .EOF
                strConcat = strConcat & _
                    .Fields(0) & pstrDelim
                .MoveNext
            Loop
        End If
        .Close
    End With
    Set rs = Nothing
    '====== uncomment next line for DAO ========
    'Set db = Nothing
    If Len(strConcat) > 0 Then
        strConcat = Left(strConcat, _
            Len(strConcat) - Len(pstrDelim))
    End If
    Concat = strConcat
End Function
-------------------------------------------------------------
The sample sql statement I am trying to execute is:

SELECT DISTINCT [Name], Concat("SELECT Position FROM BoardMembers WHERE Name='"+[Name]+"' ",'') AS Positions
FROM BoardMembers;

When I use that sql in a query in access, I get the error:
Method 'Open' of object '_Recordset' failed

And when I tuse the sql call on my asp webpage, I get the error:
Microsoft OLE DB Provider for ODBC Drivers error '80040e14'
[Microsoft][ODBC Microsoft Access Driver] Undefined function 'Concat' in expression.

The database works fine, and the sql is tracing correctly. My ultimate goal is to use this Concat function on from a sql statement on the webpage, but I get the undefined function error. Do you know how to make the function call work?
 
Last edited:

WayneRyan

AWF VIP
Local time
Today, 21:13
Joined
Nov 19, 2002
Messages
7,122
Spacether,

Don't know exactly what context you're using this in, but using ", " as the delimiter
it should be something like this.


Code:
SELECT DISTINCT [Name], 
       Concat("SELECT Position FROM BoardMembers WHERE Name = '" & [Name] & "'", ", ") AS Positions
FROM BoardMembers;

btw, Name is reserved and can lead to upredictable results.

Wayne
 

spacether

New member
Local time
Tomorrow, 08:13
Joined
Oct 1, 2005
Messages
6
WayneRyan, I used your query, but I am still getting the same errors. In the Visual Basic Editor under Tools -> References I have checked off:
Visual Basic For Applications
Microsoft Access 10.0 Object Libraries
OLE Automation
Microsoft ActiveX Data Objects 2.1 Library

Should I be getting these errors? Are there problems with my connection string? Is it possible to call custom functing in a module from a SQL statement on an ASP page?
 

WayneRyan

AWF VIP
Local time
Today, 21:13
Joined
Nov 19, 2002
Messages
7,122
Space,

Have the ConCat function display the value of pstrSQL in a Message Box.

It would help to know what the SQL string is.

Wayne
 

spacether

New member
Local time
Tomorrow, 08:13
Joined
Oct 1, 2005
Messages
6
The pstrSQL comes in fine. When I use a msgbox to display the sql I get:
SELECT Position FROM BoardMembers WHERE Name='Samantha Michaels'

If I then enter that sql into a query, it returns the correct result. So the error isn't in the sql string being passed.
 

WayneRyan

AWF VIP
Local time
Today, 21:13
Joined
Nov 19, 2002
Messages
7,122
Space,

Now that we know that the string's correct ... what does your ODBC
connect to?

If the other end is SQL server, the ConCat function has to reside on
that server.

Wayne
 

spacether

New member
Local time
Tomorrow, 08:13
Joined
Oct 1, 2005
Messages
6
It looks like they're using: Windows 2000 Server - .NET
The details are here: http://ase.tufts.edu/its/webdeptorgappdevpol.htm

And my connection string from the asp page is:
Code:
Set con = Server.CreateObject("ADODB.Connection")'Create an ADO connection object
con.Open "DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath("db1.mdb")
 

WayneRyan

AWF VIP
Local time
Today, 21:13
Joined
Nov 19, 2002
Messages
7,122
Space,

If you are using ODBC to connect to another Access database, I have a
couple of comments.

It might be easier to just link the table to YOUR Access database.
Files --> Get External Information --> Link Tables
Then you can treat it like any other table, and your Concat function
can reside in YOUR database in a Public Module.

If you do use ODBC to execute the query, then the ConCat function
should probably reside in a Public Module in the Target Database.
Your app just passes the Query to the ODBC driver, the only tools
that it has available are the target DB and the query. It has to
find the function in that domain.

Wayne
 

spacether

New member
Local time
Tomorrow, 08:13
Joined
Oct 1, 2005
Messages
6
WayneRyan,
I'm only using one database; my db1.mdb, and my query and module with the public function ConCat are in it. Doesn't that put ConCat in the correct domain?

I've changed my connection string to Jet:

Code:
Dim con, sql, ADOCommand, rs
Set ADOCommand = Server.CreateObject("ADODB.Command")
Set rs = Server.CreateObject("ADODB.Recordset")
Set con = Server.CreateObject("ADODB.Connection")
con.Provider="Microsoft.Jet.OLEDB.4.0"
con.Open Server.MapPath("db1.mdb")

With ADOCommand
    .ActiveConnection = con
    .CommandText = "board_member_concat"
    .CommandType = 4 'stored proc
End With
rs.Open ADOCommand

Where the "board_member_concat" query is:
Code:
SELECT DISTINCT [Name], 
       ConCat("SELECT Position FROM BoardMembers WHERE Name = '" & [Name] & "'", ", ") AS Positions
FROM BoardMembers;
And the query and function ConCat are in my database which has been uploaded to the server.

But I still get the error:
Microsoft JET Database Engine error '80040e14'

Undefined function 'ConCat' in expression.
 

spacether

New member
Local time
Tomorrow, 08:13
Joined
Oct 1, 2005
Messages
6
Maybe this is the problem:
Jet does not support the use of user-defined functions in SQL. The fact
that you can do it in Access with a Jet database is misleading, it cannot be
done with any other technology.

The thing to do is to apply your function as you construct your SQL
statement, rather than make it part of the SQL.
from http://www.ureader.com/message/1376557.aspx
 

Users who are viewing this thread

Top Bottom