Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open form in .exe using button in .ocx

Status
Not open for further replies.

PSchubert

Technical User
Jun 6, 2006
57
AU
Hi there,

I've made a custom .ocx control consisting of a command button and three text boxes:

[btnGo] [txtAccountNum] [txtName] [txtContact]

In my .exe, I have created a data repeater, using the .ocx. It looks something like this:

[btnGo] [txtAccountNum] [txtName] [txtContact] (record 1)
[btnGo] [txtAccountNum] [txtName] [txtContact] (record 2)
[btnGo] [txtAccountNum] [txtName] [txtContact] (record 3)

...a new line for each record--so far, so good.

How do I code [btnGo_Click] (which is part of the .ocx), to open a form in the .exe (in which the .ocx is used) AND point that form to the corresponding record?

Thanks in advance!
 
I don't believe that a user control (i.e. An OCX) knows anything about the app in which it is used so you probably can't do this completely within the OCX.

You can however raise events from within the OCX and process them within your app like this

Code:
[red]In the OCX[/red]
Public Event OpenForm(RecordID As String)

Sub btnGo_Click()
Raise Event OpenForm ("Record1")
End Sub

[red]In your EXE[/red]
Sub OCXName_OpenForm(RecordID As String)
Set frm = New FormToOpen
frm.RecordID = RecordID
frm.Open
End Sub

[red]And finally in the form that is being opened[/red]
Private mvarRecordID As String

Public Property Set RecordID(vData As String)
    Set mvarRecordID = vData
End Property

Here "OCXName" is the name by which the OCX is known in your EXE.
 
Thank you, Golom, for your response. This sounds like something I can work with. I'll let you know my outcome.
 
This is what I did:

I added a text box, [txtidxClients], to ClientsCtl.ocx.
I set its DataField property to idxClients.
I added this code to ClientsCtl.ocx:
Code:
Public Event OpenForm(RecordID As String)

Public Property Get ClientID() As String
    ClientID = txtidxClients.Text
End Property

Public Property Let ClientID(ByVal newClientID As String)
    txtidxClients.Text = newClientID
End Property

Private Sub ClientID_Change()
    PropertyChanged "ClientID"
End Sub

Public Sub btnGo_Click()
    RaiseEvent OpenForm("ClientID")
End Sub
In Tools>Procedure Attributes, I selected Name: ClientID, and in >Advanced I checked "Property is data bound," "This property binds to DataField," "Show in DataBindings collection at design time" amd "Update immediate."

Then, in the EXE, in frmClients (which contains ClientsCtl.ocx), I put the following code:
Code:
Sub ClientsCtl_OpenForm(RecordID As String)
    Set frm = New frmClient 'the name of the form to open
    frm.txtidxClients = RecordID
    frm.Open
End Sub
Then, in frmClient (which I want to open), I put:
Code:
Private mvarRecordID As String

Public Property Set RecordID(vData As String)
    Set mvarRecordID = vData
End Property

Upon compilation of the EXE, I get the following Compile Error:

"Definitions of property procedures for the same property are inconsistent, or property procedure has an optional parameter, a ParamArray, or an invalid Set final parameter"

I don't know enough to see where I've gone wrong!

 
The problem is probably here (and also probably my fault).

Code:
Public Property Set RecordID(vData As String)
    Set mvarRecordID = vData
End Property

It should be
Code:
Public Property [red]Let[/red] RecordID(vData As String)
    mvarRecordID = vData
End Property

[red]Set[/red] is used to assign values to objects. Just do the assignment without the SET keyword and use "Property Let" rather than "Property Set".

"Let" assigns a value to a variable.

I also note that you are returning the fixed string "ClientID" in the raise event. You probably want to return [red]txtidxClients.Text[/red] which is the current ID of the client that you want to display.
 
Thank you, Golom, for your assistance. You are really helping me to sneak up on it. I've made the changes you posted, and now the EXE gives a different Compile Error: Object required. It happens on:
Code:
[COLOR=red]mvarRecordID[/color] = vData
Thanks again for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top