• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1355
  • Last Modified:

ODBC scalar function with ADO

I am having problems trying to use escalar function floor in a query I use to open a recordset.
The query I am trying to run is
"SELECT tablefield FROM Table WHERE {fn floor(tablefield)} = 1 "

The similar one:
"SELECT tablefield FROM Table WHERE tablefield = 1 "
runs OK

The provider is "Provider=Microsoft.Jet.OLEDB.4.0"

When I use an ODBC Viewer Application both work OK.
When trying to open a recordset from VBA just the second works.

Can you please help me.
0
Christiano_Pereira
Asked:
Christiano_Pereira
  • 9
  • 8
1 Solution
 
joekendallCommented:
Is this in Access?
0
 
Christiano_PereiraAuthor Commented:
Yes, this is access. Can you help me in this case?
0
 
joekendallCommented:
How are you opening the recordset in VBA? Can you  post the code?

Joe
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Christiano_PereiraAuthor Commented:
I built a LoadTable function as below:
When I pass the query = "SELECT tablefield FROM Table WHERE {fn floor(tablefield)} = 1 "
It fails in the line marked with (**)
and the error is something like "Malformed GUID in query expression..."

Do you have an idea why it is failing?
Tks Joe.

Sub LoadTable(query As String, cnnconn As ADODB.Connection, rsDates As ADODB.Recordset)
    Dim cmdCommand As ADODB.Command
   
    ' Set the command text.
    Set cmdCommand = New ADODB.Command
    Set cmdCommand.ActiveConnection = cnnconn
    With cmdCommand
        .CommandText = query
        .CommandType = adCmdText
        .Execute   '(**)
    End With
   
    ' Open the recordset.
    If rsDates Is Nothing Then
        Set rsDates = New ADODB.Recordset
    Else
        If rsDates.State = adStateOpen Then rsDates.Close
    End If
    Set rsDates.ActiveConnection = cnnconn
    rsDates.cursorLocation = adUseClient
    rsDates.cursorType = adOpenStatic
    rsDates.locktype = adLockReadOnly
    rsDates.Open cmdCommand
    Set rsDates.ActiveConnection = Nothing
End Sub

Sub DBConnOpen(dataBasePath As String, cnnconn As ADODB.Connection)
    Dim cmdCommand As ADODB.Command
   
    If Not cnnconn Is Nothing Then
        If cnnconn.State = adStateOpen Then
            cnnconn.Close
        End If
    End If
    Set cnnconn = New ADODB.Connection

    With cnnconn
        .ConnectionString = _
            "Provider=Microsoft.Jet.OLEDB.4.0"
        .Open dataBasePath
    End With
End Sub
0
 
joekendallCommented:
Where does the function floor come in? Is it a function you made?
0
 
Christiano_PereiraAuthor Commented:
It is a scalar function inside the query that Access should evaluate.
I don't know if it works. I saw that construction in JETSQT 4.0 help and it is knwon as ODBC scalar function. They are:
Numerical:
FLOOR, ABS, CEILING, MOD, SQRT,...
Strings:
LTRIM, RTRIM, SUBSTRING, CONCAT,...
DateTime:
CURDATE, CURTIME,NOW,HOUR,....

I know that they work in SQL without the need of the {fn .... }
but they do not work in a query to open a recordset in VBA.
I don't know why.
This link is an example of the way it should work:
http://telecom.fit.edu/cfdocs/user/ug190006.htm

I just need to include a floor() function inside a query (in a Access Database) to open a recordset. Do you know how to do this.

Thanks a lot.
0
 
joekendallCommented:
I want to make sure I have this clear. Are you connecting to another Access database with your recordset?

If so, it will not support the Floor function. You would have to create one yourself.

Joe
0
 
Christiano_PereiraAuthor Commented:
Joe,
I am just reading data from a datatable in an access database and populating my ADODB.recordset in excel vba. There is no floor() function in case?
What is the best thing to do in this case?
 
0
 
joekendallCommented:
Access does not have a Floor function unless there is one with Access 2003 that is. If you need the Floor function, you will have to write one or have someone write one for you.

I am looking into the Floor function as it is used in Oracle now.

Joe
0
 
Christiano_PereiraAuthor Commented:
Ok, Joe
I am waiting for your help.
Thanks a lot.
0
 
joekendallCommented:
Here are the functions. Paste them in a module.

'Single Value Functions
'
'Floor returns the smallest whole number that is less than or equal to a specific value.
'
'Ceil returns the smallest whole number that is greater than or equal to a specific value.
'
'Created by Joe Kendall 11/4/2003

Public Function Floor(ByVal Value As Double) As Double
    If Value = 0 Then
        Floor = 0
        Exit Function
    End If
   
    If Value > 0 Then
        If InStr(1, Value, ".") > 0 Then
            Floor = CDbl(Left(Value, InStr(1, Value, ".") - 1))
        Else
            Floor = Value
        End If
    Else
        If InStr(1, Value, ".") > 0 Then
            Floor = CDbl(Left(Value, InStr(1, Value, ".") - 1) - 1)
        Else
            Floor = Value
        End If
    End If
End Function

Public Function Ceil(ByVal Value As Double) As Double
    If Value = 0 Then
        Ceil = 0
        Exit Function
    End If
   
    If Value > 0 Then
        If InStr(1, Value, ".") > 0 Then
            Ceil = CDbl(Left(Value, InStr(1, Value, ".") - 1) + 1)
        Else
            Ceil = Value
        End If
    Else
        If InStr(1, Value, ".") > 0 Then
            Ceil = CDbl(Left(Value, InStr(1, Value, ".") - 1))
        Else
            Ceil = Value
        End If
    End If
End Function

Joe
0
 
joekendallCommented:
Now, your SQL would look like this.

"SELECT tablefield FROM Table WHERE Floor(tablefield) = 1 "

Joe
0
 
Christiano_PereiraAuthor Commented:
Joe,
This solution looks great for me. But where should I write this function?
I put it in a new module in my access database but when I tryed to open my recordset using the command text you suggested I get an error message in vba:
"Undefined function 'floor' in expression"

Sorry but I am not used to access database.
Tks,
Christiano.
0
 
Christiano_PereiraAuthor Commented:
Joe, I really need your help.
0
 
joekendallCommented:
I will have to get back with you on it.

Joe
0
 
joekendallCommented:
Can you pull your information using the function in Access into a new table in Access? Then you could just query that table with Excel.

Joe
0
 
Christiano_PereiraAuthor Commented:
Hi Joe,
Thanks for your help.
My final approach to this problem is to use something like
"SELECT tablefield FROM Table WHERE tablefield >= 1
 AND tablefield < 1 + 1 "

I really would like to have the floor function but as I don't have it this is a reasonable solution. I will give you the points cause you helped me find this solution.

Rgds,
PC2
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 9
  • 8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now