You might take a look at this HowTo:
http://www.infopathdev.com/howto/tutorials/default.aspx?i=0bb37b4de44348408680a53a449f2353 There is some code there that uses value typed into the XML to filter the results of the query.
If you want to use code to create a custom SQL Query, take a look at this code (in C#, but you should be able to convert):
// Get value from XML
string name = thisXDocument.DOM.selectSingleNode( "//my:Name" ).text;
// Set the query
ADOAdapter dbConn = (ADOAdapter)thisXDocument.DataObjects["Project-WorkItems"].QueryAdapter;
dbConn.Command = "SELECT col1, col2 FROM table WHERE Name = \"" + name + "\"";
// Issue the query
thisXDocument.DataObjects["Project-WorkItems"].Query();
Matt Faus / Microsoft InfoPath MVPQdabra® Software / Streamline data gathering to turn process into knowledge