The DTC SDK includes a Query Builder sample that shows how a DTC can:
This topic takes some of the Visual Basic code from the sample and breaks it into steps you can use as a pattern for implementing the same functionality in your DTC. The following picture shows the Query Builder DTC on a Visual InterDev document.
For simplicity, this walkthrough covers only the Query Builder portion of the sample. For examples of the launching the URL Picker or the Color Picker, see the Visual Basic project included with this SDK. The description assumes you already have an ActiveX control created and that you understand how to use choices in a DTC. If you want more information on creating a DTC or using choices, see the guide to DTC Development and Deployment.
Also, in your own DTC, you need to add a handler for the SQL string the builder generates; this sample does not provide one. Typically such a handler would generate ASP code to retrieve a data set by establishing a data connection and running the acquired SQL string as a command. For more information about data connections, see the online documentation for ASP and for working with data in Visual InterDev.
Note When you test your Query Builder DTC in a Web page, make sure the Web project has at least one data connection to a "live" data source. The Query Builder requires that a "live" data source be available in order to create a SQL statement.
The following procedures assume that the control's form has the following simple user interface elements:
When creating a DTC, use property pages instead of inline user interface for setting a DTC's properties. An inline user interface is the graphical representation of the DTC within the hosting document such as a form or other object with controls that accept user input. Although Visual InterDev 6.0 supports inline user interface, this is not a guarantee that all DTC hosts are required to support it now or in the future.
To call the Query Builder from your DTC, you need the same interfaces required by a DTC plus the following additional items:
The VID60 Data Services Support Type Library provides the services you need for communicating with the shell and calling the Query Builder. In order to use the Query Builder, your DTC needs to get the site information required to use choices and to gain access to the container as soon as the DTC is initialized. To accomplish this, the sample uses the InitServices function. Your DTC needs one as well. This function communicates with the container to locate the Database Services object. It then notifies this object that the DTC needs database services. After establishing communication, the Database Services object responds by publishing a dynamic choice that has a tag, "QueryBuilder," for each data connection in the Web project. For more information about using tags, Defining a Choice or Identifying a Choice of Interest.
To establish communication with Visual InterDev Services
From the Project Menu, choose References. In the References dialog box, select VID60 Data Services Support Type Library 1.0.
Implements IProvideRuntimeText
Implements IDesignTimeControl
Implements IQueryBuilderSink
Dim MySite As IDesignTimeControlSite
Private Property Set IDesignTimeControl_DesignTimeControlSite(ByVal Site As IDesignTimeControlSite)
Set MySite = Site
End Property
Private Sub InitServices()
End Sub
Const guidService = "{af31b8c0-bdcc-11d0-b218-00c04fd70811}"
Const iidService = "{6FF02AB1-809B-11d1-9D27-006008058731}"
Note These IDs are not case-sensitive.
Private Sub InitServices()
Dim srvDTCServices As IDTCExtendedServices
Dim oleObject As IOleObject
Dim serviceProvider As IServiceProvider
End Sub
Private Sub InitServices()
Dim srvDTCServices As IDTCExtendedServices
Dim oleObject As IOleObject
Dim serviceProvider As IServiceProvider
cmdBuilder.Enabled = False
End Sub
Private Sub InitServices()
Dim srvDTCServices As IDTCExtendedServices
Dim oleObject As IOleObject
Dim serviceProvider As IServiceProvider
cmdBuilder.Enabled = False
Set oleObject = Me
Set serviceProvider = oleObject.GetClientSite
End Sub
Private Sub InitServices()
Dim srvDTCServices As IDTCExtendedServices
Dim oleObject As IOleObject
Dim serviceProvider As IServiceProvider
cmdBuilder.Enabled = False
Set oleObject = MeSet
serviceProvider = oleObject.GetClientSite
Set srvDTCServices = serviceProvider.QueryService(IIDFromString(guidService), IIDFromString(iidService))
End Sub
Private Sub InitServices()
Dim srvDTCServices As IDTCExtendedServicesDim oleObject As IOleObject
Dim serviceProvider As IServiceProvider
cmdBuilder.Enabled = False
Set oleObject = Me
Set serviceProvider = oleObject.GetClientSite
Set srvDTCServices = serviceProvider.QueryService(IIDFromString(guidService), IIDFromString(iidService))
If Not srvDTCServices Is Nothing Then
srvDTCServices.UsesDataBaseServices
End If
End Sub
The Visual InterDev design-time control service now publishes a choice of type "VID60.DataSourceName" for each data source in the Web project.
Before the Query Builder can be used, it needs a ChoiceSink that implements the IQueryBuilderSink interface. After specifying the sink, your DTC can launch the Query Builder. In the Query Builder sample, the launch handler is in the Click event on the button.
To specify the launch handler
Dim mQueryBuilder As Object
Private Sub cmdBuilder_Click()
If (Not mQueryBuilder Is Nothing) Then
mQueryBuilder.SetQueryBuilderSink Me
mQueryBuilder.LaunchQueryBuilder "Query Builder"
End If
End Sub
In the sample, those methods are:
Private Sub UserControl_InitProperties()
txtSQL.Text = ""
InitServices
End Sub
To save the query string
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
On Error Resume Next
txtSQL.Text = PropBag.ReadProperty("SQLText")
InitServices
End Sub
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
PropBag.WriteProperty "SQLText", txtSQL.Text
End Sub
Private Property Let IQueryBuilderSink_SQLString(ByVal SQLText As String)
txtSQL.Text = SQLText
End Property
Private Property Get IQueryBuilderSink_SQLString() As String
IQueryBuilderSink_SQLString = txtSQL.Text
End Property
The Query Builder sample includes a combo box for selecting from the list of possible data sources. This list is based on the data sources available through the Web project.
To populate the drop-down list with available data sources
Private Sub comboConnection_DropDown()
Dim chs As Choices
Dim ch As Choice
Dim cf As ChoiceFilter
Set cf = MySite.CreateChoiceFilter("VID60.DataSourceName")
comboConnection.Clear
End Sub
Private Sub comboConnection_DropDown()
Dim chs As Choices
Dim ch As Choice
Dim cf As ChoiceFilter
Set cf = MySite.CreateChoiceFilter("VID60.DataSourceName")
comboConnection.Clear
Set chs = MySite.AvailableChoices.Filter(cf)
For Each ch In chs
comboConnection.AddItem ch.Text
Next
End Sub
Note Since this example is using a dynamic choice, it uses the OnRebind method instead of a ChoiceSink. Dynamic choices only exist during a rebind. This example looks for all the VID60.DataSourceName choices; there is one for each data source in the project.
Private Sub IDesignTimeControl_OnRebind(ByVal Choices As DTC60.Choices)
Dim cf As ChoiceFilter
Dim chs As Choices
Dim ch As Choice
Set cf = MySite.CreateChoiceFilter("VID60.DataSourceName")
Set chs = Choices.Filter(cf)
For Each ch In chs
Dim s As String
Dim i As Integer
Dim qb As Object
For each of the choices, see if there is a query builder. A more advanced DTC would try to match ch.Text
against the currently selected data source name, but in the interest of simplicity, we won't do that here.
On Error Resume Next
Set qb = ch.Tags("QueryBuilder")
If Not qb Is Nothing Then
The tag isn't empty, so the Query Builder is available.
Set mQueryBuilder = qb
cmdBuilder.Enabled = True
Exit Sub
End If
Next
If the Query Builder doesn't have any available choices, disable the button that would launch it.
Set mQueryBuilder = Nothing
cmdBuilder.Enabled = False
End Sub
To complete the DTC, you need to specify how you want the DTC to use the query string from the Query Builder. For more information about specifying runtime text, see the Web Design-Time Control SDK or the Runtime Text Reference.
Private Function IProvideRuntimeText_GetRuntimeText() As String
' Your runtime text goes here.
End Function