Solved

ODBC scalar function with ADO

Posted on 2003-11-03
17
1,276 Views
Last Modified: 2007-12-19
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
Comment
Question by:Christiano_Pereira
  • 9
  • 8
17 Comments
 
LVL 11

Expert Comment

by:joekendall
Comment Utility
Is this in Access?
0
 

Author Comment

by:Christiano_Pereira
Comment Utility
Yes, this is access. Can you help me in this case?
0
 
LVL 11

Expert Comment

by:joekendall
Comment Utility
How are you opening the recordset in VBA? Can you  post the code?

Joe
0
 

Author Comment

by:Christiano_Pereira
Comment Utility
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
 
LVL 11

Expert Comment

by:joekendall
Comment Utility
Where does the function floor come in? Is it a function you made?
0
 

Author Comment

by:Christiano_Pereira
Comment Utility
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
 
LVL 11

Expert Comment

by:joekendall
Comment Utility
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
 

Author Comment

by:Christiano_Pereira
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 11

Expert Comment

by:joekendall
Comment Utility
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
 

Author Comment

by:Christiano_Pereira
Comment Utility
Ok, Joe
I am waiting for your help.
Thanks a lot.
0
 
LVL 11

Expert Comment

by:joekendall
Comment Utility
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
 
LVL 11

Expert Comment

by:joekendall
Comment Utility
Now, your SQL would look like this.

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

Joe
0
 

Author Comment

by:Christiano_Pereira
Comment Utility
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
 

Author Comment

by:Christiano_Pereira
Comment Utility
Joe, I really need your help.
0
 
LVL 11

Expert Comment

by:joekendall
Comment Utility
I will have to get back with you on it.

Joe
0
 
LVL 11

Accepted Solution

by:
joekendall earned 100 total points
Comment Utility
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
 

Author Comment

by:Christiano_Pereira
Comment Utility
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

771 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

7 Experts available now in Live!

Get 1:1 Help Now