MSQuery: Obtaining Login String from DDE Request to Query

ID: Q105953

The information in this article applies to:

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 <Data Source Name>." 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 <Data Source Name>." 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"))

REFERENCES

"Microsoft Query User's Guide," pages 134-144

For more information about the DDERequest Method, click the Search button in Visual Basic Reference Help and type:

   DDERequest Method

Additional reference words: 1.00 2.00 5.00 7.00 8.00 97 xl97
Keywords          : kbprg kbtool kbusage 
Version           : 1.00 2.00 97
Platform          : WINDOWS

Last Reviewed: May 19, 1999