<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://www.infopathdev.com:443/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Microsoft Access</title><link>https://www.infopathdev.com:443/forums/141.aspx</link><description>Qdabra is committed to answering InfoPath questions. However, we do not support Microsoft Access. For non-Access questions, please use the other boards.</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP3 (Build: 31118.962)</generator><item><title>Coded Query for MS Access 2007 VBScript</title><link>https://www.infopathdev.com:443/forums/thread/65498.aspx</link><pubDate>Mon, 28 Mar 2011 14:07:07 GMT</pubDate><guid isPermaLink="false">033a2e2d-04e2-4a9d-be01-a4634161eefd:65498</guid><dc:creator>stimbadia1</dc:creator><slash:comments>0</slash:comments><comments>https://www.infopathdev.com:443/forums/thread/65498.aspx</comments><wfw:commentRss>https://www.infopathdev.com:443/forums/commentrss.aspx?SectionID=141&amp;PostID=65498</wfw:commentRss><description>&lt;p&gt;Dear InfoDev experts,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;This forum has been a lifesaver! It is super-awesomeness and thank you to all who have contributed :)&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Infopath/Access 2007 and lang: VbScript.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;I want to be able to query the db to present only a selected amount of data on a different view. This will all happen when a user clicks a button.&lt;/p&gt;
&lt;p&gt;After all the research/ surfing, I understand there are two methods - either create an ADODB connection or use existing main data connection. I&amp;#39;m failing at both! :( Please help.&lt;/p&gt;
&lt;p&gt;1) Creating a new connection - below is the code I&amp;#39;m using.&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Sub CTRL38_7_OnClick(eventObj)&lt;br /&gt;&amp;#39; Connection String required to connect to MS Access database&lt;br /&gt;connectionString = &amp;quot;Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\New folder structure\Patient Safety Team work\Patient Safety Walkabouts\PSW Database\Patient Safety Test Base.accdb;&amp;quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Dim cn&lt;br /&gt;Dim cmd&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;#39; SQL statement to run&lt;br /&gt;sql = &amp;quot;SELECT RelationshipKey, UnitDes, ClinicalGroup, Specialty, ExactLocation, Date of visit, Next walkabout due FROM PSWVisitsUnique ORDER BY UnitDes&amp;quot;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;#39; Create ADO Connection/Command objects&lt;br /&gt;set cn = createobject(&amp;quot;ADODB.Connection&amp;quot;)&lt;br /&gt;set cmd = createobject(&amp;quot;ADODB.Command&amp;quot;)&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;#39; Open connection&lt;br /&gt;cn.open connectionString&lt;br /&gt;&amp;#39; Associate connection object with command object&lt;br /&gt;cmd.ActiveConnection = cn&lt;br /&gt;&amp;#39; Set the SQL statement of the command object&lt;br /&gt;cmd.CommandText = sql&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;#39; Execute query&lt;br /&gt;&lt;strong&gt;set rs = cmd.execute&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;#39; Close Connection&lt;br /&gt;cn.Close&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;End Sub&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&amp;nbsp;Here it tells me &amp;quot;unknown exception&amp;quot; at set rs = cmd.exectue&lt;/p&gt;
&lt;p&gt;2) Use existing main connection: (I&amp;#39;d like to use this method):&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Dim conn &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Set conn = XDocument.DataObjects(&amp;quot;/dfs:myFields&amp;quot;).adoQueryAdapter &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;adoQueryAdapter.Command = &amp;quot;SELECT RelationshipKey, UnitDes, ClinicalGroup, Specialty, ExactLocation, Date of visit, Next walkabout due FROM PSWVisitsUnique ORDER BY UnitDes&amp;quot; &lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;em&gt;adoDataAdapter.Query()&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Here, it does not recognise my main connection. I&amp;#39;ve tried several different names - what is the best approach, please please advise?&lt;/strong&gt;&lt;/p&gt;</description></item></channel></rss>