A grid loses its data when a list box or combo box on a form uses the Requery method in Visual FoxPro (131213)



The information in this article applies to:

  • Microsoft Visual FoxPro for Windows 3.0
  • Microsoft Visual FoxPro for Windows 3.0b
  • Microsoft Visual FoxPro for Macintosh 3.0b
  • Microsoft Visual FoxPro for Windows 5.0
  • Microsoft Visual FoxPro for Windows 5.0a
  • Microsoft Visual FoxPro for Windows 6.0
  • Microsoft Visual FoxPro for Windows 7.0
  • Microsoft Visual FoxPro 8.0
  • Microsoft Visual FoxPro 9.0 Professional Edition

This article was previously published under Q131213

SYMPTOMS

Microsoft Visual FoxPro lets list boxes, combo boxes, and grids within a form to be based on the output of a query (.QPR) file. The list box and combo box controls support a Requery method. The Requery method causes the query to be updated. The grid does not support a Requery method to update the results of a query. Therefore, if the list box or combo box and the grid are populated with the results of the .QPR file, the grid loses its data when the Requery method is used on the list box or combo box.

RESOLUTION

Set the RecordSource property of the grid back to itself as shown in the following code:
   Thisform.Grid1.RecordSource = Thisform.Grid1.RecordSource
					
Notes
  • For an example, see step 7 in the "Steps to reproduce the behavior" section.
  • By changing the RecordSource property of the grid, all the properties of the grid and its columns are set back to their default values. If you must save custom properties of the grid, follow the workaround that is described in the "References" section.

STATUS

This behavior is by design.

MORE INFORMATION

Steps to reproduce the behavior

  1. Open the C:\Vfp\Samples\Mainsamp\Data\Tastrade.dbc database.
  2. Create a new form named Frmmyfrm.
  3. Add a list box to the form. Set the following properties in the properties sheet:
          List1.RowSourceType = 4 (QPR)
          List1.RowSource = qryTest.qpr
    							
  4. Add a grid to the form. Set the following properties in the properties sheet:
          Grid1.RecordSourceType = 3 (QPR)
          Grid1.RecordSource = qryTest.qpr
    							
  5. Add two command buttons to the form. Set the caption of the first button to "List Requery", and the caption of the second to "Grid RecordSource".
  6. Add the following code to the Click event of the List Requery command button:
          Thisform.List1.Requery()
    							
  7. Add the following code to the Click event of the Grid RecordSource command button and to the Activate event of the form:
          Thisform.Grid1.RecordSource = Thisform.Grid1.RecordSource
    							
    Note This command does not need to be in the Activate event of the form in Visual FoxPro 3.0 but is needed in Visual FoxPro 5.0.
  8. Close and save the form, create a new program file with the following code, save this file as Qrytest.qpr, and then close the file.
          SELECT contact_name, customer_id FROM customer INTO CURSOR curTest
    
          ** Use this code in Visual FoxPro 6.0 and later:
             SELECT contact, cust_id FROM customer INTO CURSOR curTest
    							
    Note Make sure that you add the .qpr file name extension to the file.
  9. Enter the following command in the FoxPro Command window:
           DO FORM frmmyfrm
    							
  10. Click the List Requery button and note that the data in the grid disappears. Click the Grid RecordSource button and the data reappears.

    Note Do not base your grid on a .QPR file if the data in the grid needs to be modified. Data created with a .QPR file is read-only. Creating a view would be a better alternative if data needs to be modified. This requires setting the RecordSourceType of the grid to "2 - Alias" and the RecordSource to the name of the view. The data in the grid could then be refreshed using the REQUERY() function. This is a Visual FoxPro function that can be issued on a command line by itself. The function should be preceded by an equal sign, as in the following example, to update the view with new data:
              =REQUERY()
    							

REFERENCES

For more information, click the following article number to view the article in the Microsoft Knowledge Base:

140653 A cursor-based grid is blank if the SELECT-SQL command resets the cursor in Visual FoxPro


Modification Type:MajorLast Reviewed:3/17/2005
Keywords:kbBug KB131213