Microsoft KB Archive/255790

= How To Use a DataCombo ActiveX Control in Visual FoxPro =

Article ID: 255790

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q255790



SUMMARY
The Microsoft DataCombo control (Msdatlst.ocx) cannot directly use Microsoft Visual FoxPro tables as a data source. The DataCombo control can, however, be bound to an ActiveX Data Objects (ADO) recordset. This article illustrates how to bind a DataCombo ActiveX control to an ADO recordset from within a Visual FoxPro application.

The code in this article requires installation of Microsoft Visual Studio Service Pack 3.



MORE INFORMATION
NOTE: The Microsoft DataCombo control has not been tested with and is not supported by Microsoft Visual FoxPro.

In order to bind a DataCombo ActiveX control to an ADO recordset, a recordset that can be bookmarked or a client-side ADO recordset must first be created. After this ADO recordset has been created, the DataCombo.BoundColumn, DataCombo.ListField, and DataCombo.RowSource properties are used to bind the control with the ADO recordset, as follows: WITH THISFORM.DataCombo1 .BOUNDCOLUMN = myrs.FIELDS(0).NAME && Source Field being bound to   .ListField = myrs.FIELDS(0).NAME    && Source Display Field .ROWSOURCE = myrs                  && Source Recordset ENDWITH   Create a program file named DataCombo.prg using the following code: oX = CREATEOBJECT('MYFORM') oX.SHOW

READ EVENTS

DEFINE CLASS myform AS FORM CAPTION = "DataCombo Control Form" HEIGHT = 250 LEFT = 0 TOP = 0 WIDTH = 375 NAME = "myform"

ADD OBJECT DataCombo1 AS OLECONTROL WITH ; OLECLASS = "MSDataListLib.DataCombo.1",; TOP     = 25, ; LEFT    = 25, ; HEIGHT  = 22, ; WIDTH   = 175, ; NAME    = "DataCombo1"

ADD OBJECT textBox1 AS TEXTBOX WITH ; TOP    = 65, ; LEFT   = 25, ; HEIGHT = 25, ; WIDTH  = 150, ; NAME   = "textBox1", ; CONTROLSOURCE = "demo.cCustName"

ADD OBJECT commandButton2 AS COMMANDBUTTON WITH ; TOP    = 225, ; LEFT   = 275, ; HEIGHT = 25, ; CAPTION = "\<Close", ; NAME   = "commandButton2"

PROCEDURE LOAD #DEFINE adUseClient 3 #DEFINE adLockOptimistic 3 WITH THIS .ADDPROPERTY("oConn") .oConn = CREATEOBJECT("ADODB.CONNECTION")
 * !*     Create an ADO Connection to the FoxPro Samples

.oConn.CursorLocation = adUseClient
 * !*    Need a Client Side Cursor for this

.oConn.OPEN("DRIVER=Microsoft Visual FoxPro Driver;" + ;               "SourceType=DBC;SourceDB=" + HOME(2) + "DATA\TESTDATA.DBC;" + ;                "Exclusive=NO;BackGroundFetch=NO;NULL=NO;Collate=MACHINE")
 * !*    Open the data source - make sure the Samples directory is correctly
 * !*    set in Tools:Options:File Locations

.ADDPROPERTY("oRS") .oRS = CREATEOBJECT("ADODB.RECORDSET")
 * !*    Create an ADO Recordset to be used as the datasource for the DataCombo control

.oRS.ActiveConnection = THISFORM.oConn
 * !*    Set the connection property of the recordset

.oRS.LockType = adLockOptimistic .oRS.OPEN("SELECT contact FROM CUSTOMER",,,, 1) ENDWITH
 * !*    The recordset inherits the cursorlocation of the connection

IF !FILE('demo.dbf') CREATE TABLE demo (cCustName c(30)) APPEND BLANK ENDIF

IF !USED('demo') USE demo ENDIF ENDPROC

PROCEDURE INIT LOCAL loField

WITH THIS.DataCombo1 loField = THISFORM.oRS.FIELDS(0)
 * !*    Set properties for the DataCombo control

.TEXT = loField.VALUE       && Display value .BOUNDCOLUMN = loField.NAME && Source Field being bound to            .ListField = loField.NAME    && Source Display Field .ROWSOURCE = THISFORM.oRS   && Source Recordset ENDWITH ENDPROC

PROCEDURE DataCombo1.DBLCLICK LPARAMETERS area

SELECT demo IF RECCOUNT = 0 INSERT INTO demo VALUES (THIS.TEXT) ELSE UPDATE demo SET cCustName = THIS.TEXT ENDIF THISFORM.REFRESH ENDPROC

PROCEDURE commandButton2.CLICK THISFORM.RELEASE ENDPROC

PROCEDURE UNLOAD WITH THIS .oRS.CLOSE .oConn.CLOSE .oRS = .NULL. .oConn = .NULL. ENDWITH CLOSE TABLES ALL CLEAR EVENTS ENDPROC ENDDEFINE  Save and run DataCombo.prg. Click on the DataCombo control to expand the drop-down box. Double-click the DataCombo control and note that the text box value has been set to the value displayed in the DataCombo control.

(c) Microsoft Corporation 2000, All Rights Reserved. Contributions by John Desch, Microsoft Corporation.

