Microsoft KB Archive/105953

= MSQuery: Obtaining Login String from DDE Request to Query =

Article ID: 105953

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft Excel 97 Standard Edition
 * Microsoft Excel 95 Standard Edition
 * Microsoft Excel 5.0 Standard Edition
 * Microsoft Query 2000

-



This article was previously published under Q105953



SUMMARY
To automate the querying process between Microsoft Query and another program, you can create a macro that logs into a data source from within Microsoft Query. To do this, the macro must have the correct connection string. You can obtain this connection string from an existing query through a dynamic data exchange (DDE) channel to the query.



MORE INFORMATION
To obtain the connection string required by the Microsoft Query, call ODBCLOGON by initiating a DDE session to a query that is currently logged into a data source and perform a DDE request of the connectionstring item:


 * 1) Start Microsoft Query and log on to a data source.
 * 2) In Microsoft Query, create a query (QUERY1).
 * 3) Start Microsoft Excel and create one of the following macros.

Visual Basic Code Example
Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft Support professionals can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400. Sub get_connection

' Open a DDE channel to MSQuery and return the channel number chan = Application.DDEInitiate("Msquery", "Query1") ' Return the array that contains the connection string connect = Application.DDERequest(chan, "ConnectionString") ' Terminate the DDE channel Application.DDETerminate chan

End Sub The DDERequest method returns the array "connect" that contains a valid connection string that can be used by the ODBCLOGON command in order to connect through a DDE control.

NOTE: If you are using Microsoft Query 97, you must modify the line that initiates the DDE channel. This line must include the correct name for the query. In Microsoft Query 97, the query name is similar to "Query 1 from ." For example, if the data source that the query uses is named "My DataSource," change the DDEInitiate line to the following: chan = Application.DDEInitiate("Msquery", "Query 1 from My DataSource")

Microsoft Excel Version 4.0 Macro Example
Microsoft provides macro examples for illustration only, without warranty either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. This macro is provided as is and Microsoft in no way guaranties that the following code can be used in all situations and will not support modifications of the code to suit specific customer requirements. =SET.NAME("CHAN",INITIATE("MSQUERY","QUERY1")) =SET.NAME("Connect",REQUEST(CHAN,"CONNECTIONSTRING")) =TERMINATE(CHAN) =RETURN The defined name CONNECT will contain a valid connection string that can be used by the ODBCLOGON command in order to connect through a DDE control.

NOTE: If you are using Microsoft Query 97, you must modify the line that initiates the DDE channel. This line must include the correct name for the query. In Microsoft Query 97, the query name is similar to "Query 1 from ." For example, if the data source that the query uses is named "My DataSource," change the DDEInitiate line to the following: =SET.NAME("CHAN",INITIATE("MSQUERY","Query 1 from My DataSource"))

