|
-
Apr 18th, 2019, 09:51 PM
#1
Thread Starter
Fanatic Member
Undefined function in query
I've got a DataAdapter that I'm using to load a DataTable. I've configured the DataAdpater's SelectCommand as follows (note the "GetAssetPrice" function that is embedded in the SELECT statement)...
Code:
Dim selectData As New OleDb.OleDbCommand("SELECT Accounts.AccountName, Assets.AssetSymbol, Assets.AssetName, GetAssetPrice(Assets.RecID, #" & Date.Today & "#, ""ClosePrice"") AS AssetPrice, Positions.* " & _
"FROM Assets INNER JOIN (Accounts INNER JOIN Positions ON Accounts.RecID = Positions.AccountID) ON Assets.RecID = Positions.AssetID " & _
"ORDER BY Accounts.AccountName, Assets.AssetName", con)
daPositions.SelectCommand = selectData
The following error is raised when my code populates the DataTable (via the DataAdapter's SelectCommand):

The GetAssetPrice function works in my VB.Net application. For example:
Code:
Debug.Print(GetAssetPrice(20, Date.Now, "ClosePrice").ToString)
A similar query works in MS Access:
Code:
SELECT Accounts.AccountName, Assets.AssetSymbol, Assets.AssetName, GetAssetPrice([Assets].[RecID],Now(),'ClosePrice') AS AssetPrice, [AssetPrice]*[Positions].[PositionShares] AS PositionBalance, Positions.*
FROM Assets INNER JOIN (Accounts INNER JOIN Positions ON Accounts.RecID = Positions.AccountID) ON Assets.RecID = Positions.AssetID
WHERE (((Accounts.RecID)=20));
I don't know why it doesn't work in my VB.Net SelectData statement.
I've been looking at this for a couple of hours and I'm stuck...can someone please point me in the right direction to fix this error?
Last edited by Mark@SF; Apr 18th, 2019 at 10:36 PM.
-
Apr 18th, 2019, 10:44 PM
#2
Re: Undefined function in query
The problem is you've enclosed GetAssetPrice in your sql string. Something like...
"SELECT Something, " & GetAssetPrice(etc) & " Something else"
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 18th, 2019, 11:30 PM
#3
Thread Starter
Fanatic Member
Re: Undefined function in query
Hi .paul. -
How would I pass the "Assets.RecID" field value to the GetAssetPrice function if I don't enclose the function in my SQL string?
I think I'm just missing something really obvious
-
Apr 18th, 2019, 11:38 PM
#4
Re: Undefined function in query
I'm not 100% sure of this...
GetAssetPrice( & "Assets.RecID, #" & Date.Today & "#, ""ClosePrice"" & ) & " AS AssetPrice,"
Basically, you've got to construct a string for the sql part, but your function isn't recognised in sql, because it isn't sql
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 18th, 2019, 11:40 PM
#5
Re: Undefined function in query
It could be that a string isn't ideal for your query. I'm fairly sure you can construct a query with xml. I'll see if i can find an example...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 18th, 2019, 11:44 PM
#6
Re: Undefined function in query
Linq to sql might be easier in your situation...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 18th, 2019, 11:52 PM
#7
Thread Starter
Fanatic Member
Re: Undefined function in query
Right, I need the SQL to convert the GetAssetPrice() function into a decimal value: GetAssetPrice(20,#4/18/2019#,"ClosePrice") = 133.56
And do this for each row of data that the SQL returns.
I'm really stumped why this works in MS Access, but not in VB.Net...
-
Apr 18th, 2019, 11:55 PM
#8
Re: Undefined function in query
LINQ to SQL
Creating a DataContext
Querying a database
You'll see LINQ gives you much easier control than string concatenation...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 18th, 2019, 11:56 PM
#9
Re: Undefined function in query
 Originally Posted by Mark@SF
Right, I need the SQL to convert the GetAssetPrice() function into a decimal value: GetAssetPrice(20,#4/18/2019#,"ClosePrice") = 133.56
And do this for each row of data that the SQL returns.
I'm really stumped why this works in MS Access, but not in VB.Net...
It works in Access because you're not concatenating a string in your query. Try the LINQ queries
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 19th, 2019, 12:00 AM
#10
Thread Starter
Fanatic Member
Re: Undefined function in query
Ok, thanks. I've never worked with LINQ so I'll have to do some homework...
-
Apr 19th, 2019, 12:26 AM
#11
Re: Undefined function in query
Hi,
did you try the Access query 1:1
I can't try this
Code:
Dim sSQL As String = " SELECT Accounts.AccountName,"
sSQL &= " Assets.AssetSymbol, "
sSQL &= " Assets.AssetName, "
sSQL &= " GetAssetPrice([Assets].[RecID],Now(),'ClosePrice') AS AssetPrice, "
sSQL &= " [AssetPrice]*[Positions].[PositionShares] AS PositionBalance, "
sSQL &= " Positions.*"
sSQL &= " FROM Assets INNER JOIN "
sSQL &= " (Accounts INNER JOIN Positions ON Accounts.RecID = Positions.AccountID) "
sSQL &= " ON Assets.RecID = Positions.AssetID"
sSQL &= " WHERE (((Accounts.RecID)=20));"
Dim selectData As New OleDb.OleDbCommand(sSQL, con)
daPositions.SelectCommand = selectData
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
Apr 19th, 2019, 10:42 AM
#12
Re: Undefined function in query
When you did this in Access, where was the function? It wasn't calling the function from your .NET project, since how would it even know about that project, let alone load the page? Do you have the function defined in Access as a VBA function?
My usual boring signature: Nothing
 
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|