简体   繁体   中英

to use module in sql ms-Access-2010

I have defined a procedure in my module, returning the path of the database and I want to use that path in my sql query.

Procedure in module :

Public Function pathOfBillingSoftware() As String

    pathOfBillingSoftware = """path"""

End Function

I am using the function above in my sql query :

SELECT *
FROM tableName IN  pathOfBillingSoftware();

It is giving me the error: "error in from clause"

but when I am using it in vba code , it is working fine

a = pathOfBillingSoftware()

  sql = "INSERT INTO tableName  " & _
     "IN " & a & _
    " SELECT * FROM tableName"

    currentdb.Execute sql

Any Solution ?

This is dynamic SQL. You seem to already have found the solution: use VBA code. You can only use functions to return values in static SQL.

If you want to return operators or other things that aren't standard values, you need to use dynamic SQL, and thus need to use VBA.

You can create queries through VBA if you want, but note that once they are created, they are static.

Sample code to create a query with your function

CurrentDb.CreateQueryDef "MyQuery", "SELECT * FROM tableName IN " & pathOfBillingSoftware();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM