Solved

ActiveX control and Datasource, ie bind my activeX ctrl to ADO control

Posted on 2002-05-06
26
454 Views
Last Modified: 2012-06-27
Hi,

I am learning how to make an ActiveX controls.

I have been creating my own 'CoolTextBox' with properties properties like 'ChooseType'(which takes Numbers, letters or both) and 'ChooseCase'(which takes upppercase, lower case or both) etc. These works just fine.

I went a step further, and wanted my 'CooltextBox' to be bound to my ADO control in my Vbapplication.

Thru the ActiveX control Interface wizard, i mapped the Datasource property with datasource property of the text1. I also wanted to bind the Datafield property, but did'nt find it in the available properties.
I guess, the DataSource and the DataField are the 2 properties one needs to have, so that it can be bound to the ADO control in the Vb Application which would be using the cooltextbox control.

But it gives the follwoing error:
"User-defined datatype not defined" at the Datasource property.

Should i be giving any references. Where am i going wrong?
What are the different properties that i should bind to the text box in ActiveX control which will be simlar to any other textbox bound to the ADO control in my vb appln.


Priya
0
Comment
Question by:priya_pbk
  • 16
  • 10
26 Comments
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6990956
For eaxmple:

Private Sub UserControl_Initialize()
'Assign Connection string
Adodc1.ConnectionString = "DSN=XYZ"

'Assign Recordsource
Adodc1.RecordSource = "ABC"

'Refresh Or Requery
Adodc1.Refresh


'already mapped the connection to text

'Map fields
Text1.DataField = "FIELD NAME"

End Sub
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6991125
sorry hiranmaya...for not accepting the proposed answer.

As i said, i could not locate the DataField property in the ActiveX InterFace Wizard.
My usercontrol design contains only a text box. Now should i add a adodc control to it also.

My requirement is simple:
-To start off, i want a text box which will accept,numbers or letter(uppercase, lower case or both) which i did
- Now I want this userControl to have the other properties that a normal text box have, like the Datasource, DataField and other necessary properties, so that I can bind these properties to the ADODB control in my VB appln which uses my ocx control in the form.

My issues are:
1)when i mapped the Datasouce to the Datasouce of the Text1..it gives me the following error:
"User-defined data type not defined"
2) and I did'nt find the DataField property in the available properties thru ActiveX Interface Wizard.

I hope this is not confusing.

0
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6991186
1.Pls set the let and get property in the activeX control before you compile ...

2. Use the control in the new project.
UserControl1.MyDatasource = ADODC1

Below is the set and let property for Database connection

Public Property Get MyDataSource() As ADODB.Connection
    Set MyDataSource = Text1.DataSource
End Property

Public Property Let MyDataSource(ByVal New_MyDataSource As ADODB.Connection)
    Set Text1.DataSource = New_MyDataSource
    PropertyChanged "MyDataSource"
End Property

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
Set Text1.DataSource = PropBag.ReadProperty("MyDataSource")
End Sub

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
    Call PropBag.WriteProperty("MyDataSource", Text1.DataSource)
End Sub
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6993290
sorry again!!

i am getting the following error :
"Data value named 'MyDataSource' not found"

at the usercontrol_read() properties
ie at the line :
Set Text1.DataSource = PropBag.ReadProperty("MyDataSource")

Now how do i fix this:

also how will my text box come to know the following:
1)Datasource (will bind to adodc1 or adodc2 ..whatever..that's what we are trying to do..)
2)Datafield ..which coln will be bound to the text box in my vbproject..should'nt i be having another let and get property for the datafield property like MyDataField property?

Should i be declaring any variables or give reference to any application. I have given reference to Microsoft ActiveX DataObjects 2.6 Library.

I have'nt complied the code..but just testing by the form in the ocx project. It shows the error when I am running it.

As I said, since i am new to the making of ActiveX, therefore so many qts..

i hope you can help me with this!

priya
0
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6993296
But you have to define let and get property anyhow that i can say! Goodluck.
0
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6993380
Set for ref: Microsoft Datasource Interfaces and compile.

'*******************************************************
'Default Property Values:
Const m_def_mydatasource = 0
'Property Variables:
Dim m_DataMembers As DataMembers
Dim m_mydatasource As Variant


Public Property Get DataMember() As String
    DataMember = Text1.DataMember
End Property

Public Property Let DataMember(ByVal New_DataMember As String)
    Text1.DataMember() = New_DataMember
    PropertyChanged "DataMember"
End Property

Public Property Get DataMembers() As DataMembers
    Set DataMembers = m_DataMembers
End Property

Public Property Set DataMembers(ByVal New_DataMembers As DataMembers)
    Set m_DataMembers = New_DataMembers
    PropertyChanged "DataMembers"
End Property


Public Property Get DataSource() As DataSource
    Set DataSource = Text1.DataSource
End Property

Public Property Set DataSource(ByVal New_DataSource As DataSource)
    Set Text1.DataSource = New_DataSource
    PropertyChanged "DataSource"
End Property


Public Property Get mydatasource() As Variant
    mydatasource = m_mydatasource
End Property

Public Property Let mydatasource(ByVal New_mydatasource As Variant)
    m_mydatasource = New_mydatasource
    PropertyChanged "mydatasource"
End Property

'Initialize Properties for User Control
Private Sub UserControl_InitProperties()
    m_mydatasource = m_def_mydatasource
End Sub

'Load property values from storage
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

    Set DataFormat = PropBag.ReadProperty("DataFormat", Nothing)
    Text1.DataMember = PropBag.ReadProperty("DataMember", "")
    Set m_DataMembers = PropBag.ReadProperty("DataMembers", Nothing)
    Set DataSource = PropBag.ReadProperty("DataSource", Nothing)
    m_mydatasource = PropBag.ReadProperty("mydatasource", m_def_mydatasource)
End Sub

'Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)

    Call PropBag.WriteProperty("DataFormat", DataFormat, Nothing)
    Call PropBag.WriteProperty("DataMember", Text1.DataMember, "")
    Call PropBag.WriteProperty("DataMembers", m_DataMembers, Nothing)
    Call PropBag.WriteProperty("DataSource", DataSource, Nothing)
    Call PropBag.WriteProperty("mydatasource", m_mydatasource, m_def_mydatasource)
End Sub

'******************************************************

And in a new project, add the user control
and for example try for ADODC connection referecene:

Set UserControl11.DataSource = Adodc1
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6993382
i did define the let and get property for MyDataSource and also the changes in read and write properties.I wrote exactly as you had said..still it gives me the 'Runtime error' ie
"Data value named 'MyDataSource' not found"

Any workaround??
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6993519
ok. thru with one of the step. I was able to bind my SuperTextbox with ADODC in my VB appln. The problem becoz I was not giving any reference to Microsoft DataSources Interface(which i did'nt know)

But still, there is a problem/issues.

1) How do i bind my superTextBox with one of the column of the Adodc control(say companyName of the Company Table). I started writing the let and get property for DataField, simlar to the Datamember property. I did this and my application crashed thrice. Any more refrences to be given.

2)Also i wanted to know, when we could accomplish binding the Adodc control thru the let and get property of Datasource, why do we have a second property viz, mydatasource with a default value=0. What does this do?

3)why do we have a DataMember(let and get)property and again a DataMembers(let and get) property. how does this thing work. As far as i know, the Datasource and DataMember are 2 properties which works in conjuction with each other and again why 2 let and get properties for datamember and dataMembers(not seen at design time in the properties window)

Thanks a lot and hope u can help..priya

0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6993637
are you suggesting the same answer..ie 05/06/2002 06:39AM PST or is this some bug in displaying..This is not clear.!
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6993645
Will I ever finish making my ActiveX Control!
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6995990
Update:

This is how my code looks now in the Usercontrol Code:
--------------------------------------------------------

'Set for ref: Microsoft Datasource Interfaces and compile.
'*******************************************************
'Default Property Values:
Const m_def_mydatasource = 0
'Property Variables:
Dim m_DataMembers As DataMembers
Dim m_mydatasource As Variant

Private Sub UserControl_Resize()
 With Text1
    .Height = UserControl.ScaleHeight
    .Top = UserControl.ScaleTop
    .Width = UserControl.ScaleWidth
    .Left = UserControl.ScaleLeft
 End With
End Sub

Public Property Get DataMember() As String
   DataMember = Text1.DataMember
End Property

Public Property Let DataMember(ByVal New_DataMember As String)
   Text1.DataMember() = New_DataMember
   PropertyChanged "DataMember"
End Property

Public Property Get DataMembers() As DataMembers
   Set DataMembers = m_DataMembers
End Property

Public Property Set DataMembers(ByVal New_DataMembers As DataMembers)
   Set m_DataMembers = New_DataMembers
   PropertyChanged "DataMembers"
End Property

Public Property Get DataSource() As DataSource
   Set DataSource = Text1.DataSource
End Property

Public Property Set DataSource(ByVal New_DataSource As DataSource)
   Set Text1.DataSource = New_DataSource
   PropertyChanged "DataSource"
End Property

'Initialize Properties for User Control
Private Sub UserControl_InitProperties()
   m_mydatasource = m_def_mydatasource
End Sub

'Load property values from storage
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
   Set DataFormat = PropBag.ReadProperty("DataFormat", Nothing)
   Text1.DataMember = PropBag.ReadProperty("DataMember", "")
   Set m_DataMembers = PropBag.ReadProperty("DataMembers", Nothing)
   Set DataSource = PropBag.ReadProperty("DataSource", Nothing)
    ' m_mydatasource = PropBag.ReadProperty("mydatasource", m_def_mydatasource)
End Sub


'Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
   Call PropBag.WriteProperty("DataFormat", DataFormat, Nothing)
   Call PropBag.WriteProperty("DataMember", Text1.DataMember, "")
   Call PropBag.WriteProperty("DataMembers", m_DataMembers, Nothing)
   Call PropBag.WriteProperty("DataSource", DataSource, Nothing)
      'Call PropBag.WriteProperty("mydatasource", m_mydatasource, m_def_mydatasource)
End Sub

'------------------------------------------------------

'My VB application'..Standard Exe

I have my superTextbox in my form and an ADODC control in my form.
At design time I have set the following:
-I have bounded the Adodc1 with a Company Table.
-datasource of the SuperTextBox as Adodc1.

Now since I wanted the DataField property of the text box to be mapped to the SuperTextBox...I wrote the foll code in the usercontrol

-------------------------------------------


Public Property Get MyDataField() As String
   MyDataField = Text1.DataField
End Property

Public Property Let MyDataField(ByVal New_MyDataField As String)
   Text1.DataField() = New_MyDataField
   PropertyChanged "MyDataField"
End Property

Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
   Call PropBag.WriteProperty("MyDataField", Text1.DataField, "")
End Sub

Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
   Text1.DataField = PropBag.ReadProperty("MyDataField", "")
End Sub

--------------------------------------------------------

This is what happened when i was trying to set the properties of the SuperControl at "DESIGN TIME"
-SuperControl.Datasource=Adodc1(fine..no problem)
-SuperControl.MyDatafield=companyName( name of one of the col of company).

When I did this it showed me the first companyName of the table at DESIGN TIME, then when I executed the standard exe form, the application terminated giving me the vbexe error..that Memory could not be "read" thus crashing the application.

Where am I going wrong..hope you can help me to try to solve this and dont worry abt the points, I will surely double it.

Also I am still not clear with the DataMembers and Data Member details.

I hope you can help me..

priya



0
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6996027
Use activex control wizard in the VB6. Preject->Add User Control->VB ActiveX Control Interface Wizard. It will help you a lot.

You can add datafield property same as datamember or other prop to the user control and try now!
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6996050
yes, I am using the ActiveX Interface Wizard. I did'nt want the default properties like font, backcolor etc..right now, coz i needed the above properties in the code first.

But the sad part is that I did'nt get this particular property i.e 'DataField' in the Wizard while mapping, but other properties like datasouce, datamembers, datamember, dataformat,drawmode, drawstyle, drawwidth and I dont see any 'dataField' property at all.
I also tried adding a new property "DataField" thru wizard..gives me a message " The member name you have created is already being used by the Extender object"

Since you say the DataField property is available at the time of mapping, but i dont seem to find it at all.

Is 'nt it supposed to show all the properties of the text box(constituent controls) while mapping!




0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 2

Author Comment

by:priya_pbk
ID: 6996170
ok, here's the update.

It seems this is a bug in Microsoft. Pls refer to this article:
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q189924

so, was this a sheer waste..any one with a workaround for fixing this bug!
0
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6997882
Hi pbk,

Try Now.

For User control:

Const m_def_mydatasource = 0
Dim m_DataMembers As DataMembers

Public Property Get DataField() As String
    DataField = Text1.DataField
End Property

Public Property Let DataField(ByVal New_DataField As String)
    Text1.DataField = New_DataField
    PropertyChanged "DataField"
End Property

Public Property Get DataMember() As String
    DataMember = Text1.DataMember
End Property

Public Property Let DataMember(ByVal New_DataMember As String)
    Text1.DataMember() = New_DataMember
    PropertyChanged "DataMember"
End Property

Public Property Get DataMembers() As DataMembers
    Set DataMembers = m_DataMembers
End Property

Public Property Set DataMembers(ByVal New_DataMembers As DataMembers)
    Set m_DataMembers = New_DataMembers
    PropertyChanged "DataMembers"
End Property


Public Property Get DataSource() As DataSource
    Set DataSource = Text1.DataSource
End Property

Public Property Set DataSource(ByVal New_DataSource As DataSource)
    Set Text1.DataSource = New_DataSource
    PropertyChanged "DataSource"
End Property

'Initialize Properties for User Control
Private Sub UserControl_InitProperties()
    m_mydatasource = m_def_mydatasource
End Sub

'Load property values from storage
Private Sub UserControl_ReadProperties(PropBag As PropertyBag)

    Set DataFormat = PropBag.ReadProperty("DataFormat", Nothing)
    Text1.DataField = PropBag.ReadProperty("DataField", "")
    Text1.DataMember = PropBag.ReadProperty("DataMember", "")
    Set m_DataMembers = PropBag.ReadProperty("DataMembers", Nothing)
    Set DataSource = PropBag.ReadProperty("DataSource", Nothing)
End Sub

'Write property values to storage
Private Sub UserControl_WriteProperties(PropBag As PropertyBag)

    Call PropBag.WriteProperty("DataFormat", DataFormat, Nothing)
    Call PropBag.WriteProperty("DataField", Text1.DataField, "")
    Call PropBag.WriteProperty("DataMember", Text1.DataMember, "")
    Call PropBag.WriteProperty("DataMembers", m_DataMembers, Nothing)
    Call PropBag.WriteProperty("DataSource", DataSource, Nothing)
End Sub

For New Exe project (for testing):
1. Add one ADODC
2. Add compiled user control
3. Add Text1
4. Bind ADODC connection with connection string, And record source
5. Bind datasource and datafield property of user control and text box for testing.
6.Everything navigates smoothly.
0
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6997887
Sorry forgot to tell. In the usercontrol, click tools->procedure attributes-> for data member click advance option and check property is data bound.
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6998141
hi hiranmaya,

i would really appreciate if you can try this code yourself, because I have been trying to do this since so long. Now I did exactly as you said. Exactly the same code. But gave me vb6.exe error. ie could not read the memory and the application crashed.

I also bound the dataField thru the procedure attributes. If you had noticed in the article i had mentioned as a link, in the DataField Get and let procedures it is not...
Text1.DataField=... (this procdure makes the vb appln crash)
but
Text1.Text=...

as i had said this happens to be bug with the some of the databound properties of Microsoft which is not fixed.

if you dont mind, I would appreciate if you can try this code by making this ocx.


0
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6998157
I have tried and tested..I can navigate through data also. Sorry I can not help you more.
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6998160
ok..did the following changes and made it run for the first time with proper values.

changes are as follows:
Public Property Get DataField() As String
   DataField = Text1.Text '(it was text1.datafield)
End Property

Public Property Let DataField(ByVal New_DataField As String)
   '(this too was text1.datafield)
   Text1.Text = New_DataField
   PropertyChanged "DataField"
End Property

Also bound the datamember thru procedure attributes.

Runs fine for the first time, but as soon as one comes to design time, all the properties are gone. One needs to set it again, ie datasource and datafield etc.

It also seems when one compiles the code to make an exe, the properties are all gone. So Tough luck!

0
 
LVL 1

Expert Comment

by:hiranmaya
ID: 6998175
When i did from myside with the new code and compiled the activex control, in the new project i can see the all datafield,datamember properties for that user control.
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 6998190
Have you tried compiling the Vb project and make it an executable. Then try running the exe. Do you get to see the details in the ActiveX control text box.
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 7007727
Thanks hiranmaya. I appreciate your effort to help me with this code. As I had mentioned earlier, I have increased the points to 100, as I had promised. Even though my code is still not in a workable condition, I sincerely thank you for your effort as I was able to gain some insight and knowledge in ActiveX controls--thanks,priya.
0
 
LVL 1

Accepted Solution

by:
hiranmaya earned 100 total points
ID: 7007738
It is not abt points that we are working on! While resolving we also get some extra knowledge.
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 7007745
Thanks hiranmaya. I appreciate your effort to help me with this code. As I had mentioned earlier, I have increased the points to 100, as I had promised. Even though my code is still not in a workable condition, I sincerely thank you for your effort as I was able to gain some insight and knowledge in ActiveX controls--thanks,priya.
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 7007749
thanks. I hope you did too!
0
 
LVL 2

Author Comment

by:priya_pbk
ID: 7007762
thanks. I hope you did too!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now