Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. MS Access/OleDb: Calling custom function in SQL query

MS Access/OleDb: Calling custom function in SQL query

Scheduled Pinned Locked Moved Database
databasecsharpsql-serversysadmin
8 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    Bernhard Hiller
    wrote on last edited by
    #1

    Hi there, we store a duration value as a Long Integer in milliseconds in our database (MS Access or MS SQL Server or others). Now I want to get that value formatted in H:mm:ss, and for the Access version I wrote a short VBA function (Public Function FormatTime(inputValue As Long) As String) to do that. When I open Access, and run a query (e.g. SELECT ID, FormatTime([Duration]) From MyTable), it works fine. But our applications are written in C#, and the communication with the Access database is via OleDb. Now I get an error message that function FormatTime is unknown. Is it possible at all to use a custom function in MS Access via OleDb? Looking forward to your ideas. Bernhard

    D M P 3 Replies Last reply
    0
    • B Bernhard Hiller

      Hi there, we store a duration value as a Long Integer in milliseconds in our database (MS Access or MS SQL Server or others). Now I want to get that value formatted in H:mm:ss, and for the Access version I wrote a short VBA function (Public Function FormatTime(inputValue As Long) As String) to do that. When I open Access, and run a query (e.g. SELECT ID, FormatTime([Duration]) From MyTable), it works fine. But our applications are written in C#, and the communication with the Access database is via OleDb. Now I get an error message that function FormatTime is unknown. Is it possible at all to use a custom function in MS Access via OleDb? Looking forward to your ideas. Bernhard

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      Have you tried placing your select statement inside a MS-Access saved query ? Your C# application would then execute the saved query, not the SQL statement directly. Give it a shot.

      B 1 Reply Last reply
      0
      • D David Mujica

        Have you tried placing your select statement inside a MS-Access saved query ? Your C# application would then execute the saved query, not the SQL statement directly. Give it a shot.

        B Offline
        B Offline
        Bernhard Hiller
        wrote on last edited by
        #3

        Thanks for the idea. Since our application generates quite complex queries on the fly (and must be able to use other databases like SQL Server or Oracle also), this "stored procedure" approach is not usable.

        1 Reply Last reply
        0
        • B Bernhard Hiller

          Hi there, we store a duration value as a Long Integer in milliseconds in our database (MS Access or MS SQL Server or others). Now I want to get that value formatted in H:mm:ss, and for the Access version I wrote a short VBA function (Public Function FormatTime(inputValue As Long) As String) to do that. When I open Access, and run a query (e.g. SELECT ID, FormatTime([Duration]) From MyTable), it works fine. But our applications are written in C#, and the communication with the Access database is via OleDb. Now I get an error message that function FormatTime is unknown. Is it possible at all to use a custom function in MS Access via OleDb? Looking forward to your ideas. Bernhard

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          bhiller wrote:

          Is it possible at all to use a custom function in MS Access via OleDb?

          No. You are asking OLEDB to pass FormatTime and OLEDB does not know of this function therefore the error. You are going to have to either move the formattime into your sql statement or format the data AFTER you get the duration from the database.

          Never underestimate the power of human stupidity RAH

          B 1 Reply Last reply
          0
          • M Mycroft Holmes

            bhiller wrote:

            Is it possible at all to use a custom function in MS Access via OleDb?

            No. You are asking OLEDB to pass FormatTime and OLEDB does not know of this function therefore the error. You are going to have to either move the formattime into your sql statement or format the data AFTER you get the duration from the database.

            Never underestimate the power of human stupidity RAH

            B Offline
            B Offline
            Bernhard Hiller
            wrote on last edited by
            #5

            That's not correct: 1. The function is already included in the SQL statement: SELECT ID, FormatTime([Duration]) From MyTable 2. I can connect to a SQL Server with OleDb. Here, it works just fine. Hence it is not necessary for OleDb to know my custom functions. It seems to be a Microsoft Access problem...

            M 1 Reply Last reply
            0
            • B Bernhard Hiller

              That's not correct: 1. The function is already included in the SQL statement: SELECT ID, FormatTime([Duration]) From MyTable 2. I can connect to a SQL Server with OleDb. Here, it works just fine. Hence it is not necessary for OleDb to know my custom functions. It seems to be a Microsoft Access problem...

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              You are correct in that it will work with SQL Server, you can use a UDF in a SQL string as it is a TSQL construct in SQL Server. Your function in Access is a VBA function and I'd bet that will be the difference.

              Never underestimate the power of human stupidity RAH

              1 Reply Last reply
              0
              • B Bernhard Hiller

                Hi there, we store a duration value as a Long Integer in milliseconds in our database (MS Access or MS SQL Server or others). Now I want to get that value formatted in H:mm:ss, and for the Access version I wrote a short VBA function (Public Function FormatTime(inputValue As Long) As String) to do that. When I open Access, and run a query (e.g. SELECT ID, FormatTime([Duration]) From MyTable), it works fine. But our applications are written in C#, and the communication with the Access database is via OleDb. Now I get an error message that function FormatTime is unknown. Is it possible at all to use a custom function in MS Access via OleDb? Looking forward to your ideas. Bernhard

                P Offline
                P Offline
                PikeAg
                wrote on last edited by
                #7

                Did you ever figure this one out? I am having the exact same issue. Running a query with a custom function in Access works fine, but trying to run it through C++ using OldDB fails. :(

                B 1 Reply Last reply
                0
                • P PikeAg

                  Did you ever figure this one out? I am having the exact same issue. Running a query with a custom function in Access works fine, but trying to run it through C++ using OldDB fails. :(

                  B Offline
                  B Offline
                  Bernhard Hiller
                  wrote on last edited by
                  #8

                  Unfortunately, I haven't yet found a solution...

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups