Hi Hilary, Sorry to be back so soon! Guess what? :)
Yeah, I'm running into a problem. Since I'm using C#, I referred to this thread instead of the one with Microsoft. I studied the discussion and followed all your instructions including the use of first and second half substring manipulation. However, I'm getting this error when I click Run Query button:
Failed connection to data source: InfoPath cannot connect to a data source.
The query cannot be run for the following DataObject: Main connection
InfoPath cannot run the specified query.
[0x80040E14][Microsoft Office Access Database Engine] Syntax error (missing operator) in query expression '[Version] where ([ProjectName] like 'Data%')'.
[0x80040E14][MSDataShape] Data provider failed while executing a provider command.
A required data source is not available.
Here's my code for the button:
public void btnRunQuery_Clicked(object sender, ClickedEventArgs e)
{
//get the value of the query field we want to use with a wild card
XPathNavigator queryField = MainDataSource.CreateNavigator().SelectSingleNode("/dfs:myFields/dfs:queryFields/q:tblProjects/@ProjectName", NamespaceManager);
string querySeed = queryField.Value;
//clear the query field so that it is ignored in the query
queryField.SetValue("");
//create our custom where clause -- you have some choices here. I've added in the wildcard. You could choose not to, if your users are savvy and can fill it in themselves when searching, all up to you.
string customQuery = " where ([ProjectName] like '" + querySeed + "%')";
//get the original command (we'll need to set it back)
AdoQueryConnection newQuery = (AdoQueryConnection)MainDataSource.QueryConnection;
string origCommand = newQuery.Command.ToString();
//set the query to use the custom command
int firstBrace = origCommand.IndexOf("}");
string origCommand1stHalf = origCommand.Substring(0, firstBrace);
string origCommand2ndHalf = origCommand.Substring(firstBrace,origCommand.Length-origCommand1stHalf.Length);
newQuery.Command = origCommand1stHalf + customQuery + origCommand2ndHalf;
//annnnnnddddd, now we run the query using our shiny new command
newQuery.Execute();
//put everything back the way it was.... you're going to need it!
newQuery.Command = origCommand;
//set the query field back for the user, so they know what they typed in!
queryField.SetValue(querySeed);
}
Here's my OrigCommand string:
shape
{select [ProjectID],[ProjectNo],[ProjectName],[ProjectDesc],[Version],[Active] from [tblProjects] as [tblProjects] order by [ProjectName],[Version]} as [tblProjects]
append
((shape
{select [AssetID],[AssetNo],[AssetName],[ProjectID],[AssetClassID],[AssetValue],[IntRate],[YrStarted],
[YrCapitalized],[YrAmortized],[RegionID],[DeptID],[CostCentreID] from [tblAssetsMaster]}
append
({select [AssetDetailID],[AssetID],[FYear],[NBV],[Amort],[Interest] from [tblAssetsDetail]}
relate [AssetID] TO [AssetID]) as [tblAssetsDetail])
relate [ProjectID] TO [ProjectID]) as [tblAssetsMaster]
Here's my CustomQuery string:
where ([ProjectName] like 'Data%')
Everything seems correct but it's not. When I don't add customQuery string to the newQuery.Command, I don't get the error... but ofcourse I don't get the result I want. What am I missing here? Thanks.