Get External Returns Incorrect Data and/or Blank Rows

This article was previously published under Q158835
This article has been archived. It is offered "as is" and will no longer be updated.
When you use the Get External Data command on the Data menu to return datafrom an external data source, the data that is returned is not correct.Some fields or rows that should contain data are blank; or some fields orrows contain incorrect data.
This problem may occur when Microsoft Excel uses the Microsoft Querydynamic data exchange (DDE) Fetch command to return the data to theworksheet.This problem occurs only under Windows 95. The problem does not occur whenyou use Microsoft Windows NT 3.51 or later.
Microsoft has confirmed this to be a problem in the Microsoft productslisted at the beginning of this article. The problem has been correctedin Microsoft Excel 97 for Windows.
For additional information on using DDE with Microsoft Query, please seethe following articles in the Microsoft Knowledge Base:
149581MSQuery: How to Retrieve Request Items Using DDERequest to MSQuery

142357XL: Visual Basic Example Using BuildSQL

105953MSQuery: Obtaining Login String from DDE Request to Query
You can use the following workarounds to avoid the problem described above.

Microsoft provides examples of Visual Basic for Applications proceduresfor illustration only, without warranty either expressed or implied,including, but not limited to the implied warranties of merchantabilityand/or fitness for a particular purpose. The Visual Basic procedures inthis article are provided 'as is' and Microsoft does not guarantee thatthey can be used in all situations. While Microsoft support professionalscan help explain the functionality of a particular macro, they will notmodify these examples to provide added functionality, nor will theyhelp you construct macros to meet your specific needs. If you havelimited programming experience, you may want to consult one of theMicrosoft Solution Providers. Solution Providers offer a wide range offee-based services, including creating custom macros. For moreinformation about Microsoft Solution Providers, call Microsoft CustomerInformation Service at (800) 426-9400.

The following methods describe macros that you can use to successfullyreturn external data to your worksheet.

Method 1: Using Data Access Objects (DAO)

You can use DAO in your macro to return the data to the worksheet. Ifyou use this method, you must specify the path to the database, theStructured Query Language (SQL) statement for the query, and thedestination cell for the data. The following macro example returns thedata without any user interaction. To use this macro, use the followingsteps:
  1. On the Insert menu, point to Macro and click Module.
  2. In the Visual Basic module, type the following code:
          Sub GetDataUsingDAO()          'Open the database.          Set Db = opendatabase("c:\my documents\db1.mdb")          'Create a recordset using a SQL statement.          Set RS = Db.OpenRecordset("Select * from Table1")          'Copy the field names starting at Sheet1!A1.          For i = 1 To RS.Fields.Count              Range("Sheet1!A1").Offset(, i - 1) = RS(i - 1).Name          Next          'Copy the results starting at Sheet1!A2.          Range("Sheet1!A2").CopyFromRecordset RS          Db.Close      End Sub					
  3. Click References on the Tools menu, select "Microsoft DAO 3.0 Object Library", and then click OK.

    The "Microsoft DAO 3.0 Object Library" check box should contain a checkmark.
  4. Click Macro on the Tools menu, click GetDataUsingDAO, and click Run to run the macro.

Method 2: Using DDE with Microsoft Query

You can use DDE with Microsoft Query to return the data to the worksheet.When you use this method, you interactively select the data source, selectthe database, and create the query. To use this macro, use the followingsteps:
  1. On the Insert menu, point to Macro and click Module.
  2. Type the following code in the module.
    Sub GetDataUsingDDE()    Dim chan As Integer    Dim r As Variant, c As Variant    Dim StartCell As Range    Dim RowsToRetrieve As String    Dim i As Integer    'Activate query - if it is not running, an error occurs and the    'error handler StartQuery will start Query.    On Error GoTo StartQuery    AppActivate "Microsoft Query"    On Error GoTo 0    'Initiate a channel to query and return control to the user.    chan = DDEInitiate("MSquery", "system")    DDEExecute chan, _        "[UserControl('&Return Data To Microsoft Excel', 3, true)]"    'Prompt the user for the cell to return the data to.    Set StartCell = Application.InputBox( _        prompt:="Select the starting cell", Type:=8)    'Obtain the number of rows and columns in the result.    r = DDERequest(chan, "NumRows")    c = DDERequest(chan, "NumCols")    'Return the headers to the first row at the starting cell.    DDEExecute chan, "[Fetch('Excel','" & StartCell.Worksheet.Name & _        "','" & StartCell.Resize(, c(1)).Address( _        ReferenceStyle:=xlR1C1) & "','R1:R1/Headers')]"    'Return the data to the worksheet 100 rows at a time.    For i = 1 To r(1) Step 100        RowsToRetrieve = "R" & i & ":R" & i + 100 - 1        DDEExecute chan, "[Fetch('Excel','" & StartCell.Worksheet.Name _            & "','" & StartCell.Offset(i).Resize(100, c(1)).Address( _            ReferenceStyle:=xlR1C1) & "','" & RowsToRetrieve & "')]"        DoEvents    Next    'Terminate the channel.    DDETerminate chan   Exit SubStartQuery:       Shell "c:\program files\common files\microsoft shared" & _           "\msquery\msqry32.exe", 2       DoEvents       ResumeEnd Sub					
NOTE: This example uses the DDE Fetch command to return the data to theworksheet 100 rows at a time. To increase or decrease the number of rowsthat are returned, modify the Step argument in the following line:
   For i = 1 To r(1) Step 100				
  1. Click Macro on the Tools menu, click GetDataUsingDDE, and click Run to run the macro.
Microsoft Query starts.

  1. Create your query and then click "Return Data to Microsoft Excel" on the File menu.
  2. When you are prompted, select a cell for the data and click OK.
For more information about Data Access Objects, click the Index tab inMicrosoft Excel Help, type the following text
data access, using
and then double-click the selected text to go to the "Using Data Access"topic.
garbage character gap missing XL7 nothing nil nada

Article ID: 158835 - Last Review: 10/04/2013 16:44:50 - Revision: 1.6

Microsoft Excel 95a, Microsoft Excel 95 Standard Edition, Microsoft Windows 95

  • kbnosurvey kbarchive kbprb kbprogramming KB158835