Function returns zero if no data found

Posted on 2007-10-14
Last Modified: 2013-12-25
I need this user defined function to return a zero in the cell if no data was found in the .mdb

Const MDB = "C:\rubyfiles\tanks\gasdata.mdb"

Function getGASdata(pstrQuery As String)
    getGASdata = DAO.DBEngine.OpenDatabase(MDB, , True).OpenRecordset(pstrQuery)(0)
End Function

Question by:mweidner
    1 Comment
    LVL 46

    Accepted Solution

    Hi mweidner,

    Try this....

        Function getGASdata(pstrQuery As String)
            Dim rs As Recordset
            Set rs = DAO.DBEngine.OpenDatabase(MDB, , True).OpenRecordset(pstrQuery)
            If rs.EOF And rs.BOF Then
                getGASdata = 0
                getGASdata = rs(0)
            End If
            Set rs = Nothing
        End Function



    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Join & Write a Comment

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
    The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
    Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now