Can you assign a control name to a variable of some type

I

idk

Hi All.

I have a simple form with a date field.

I have created another form based on the activex calendar control.

I want to pass the name of the date control on the initial form, to the from
with the activex calendar control so that each click of the activex calendar
control will update the control on the calling form.

I passed the name of the control in the openargs field, but that is a
string, and i cannot figure out how to access the original form without
hardwiring the code, which would make is useless for updating other date
controls on other forms.

HELP.

Ian.
 
D

Dan Artuso

Hi,
Here is one way to do it.

Pass the name of the form and the name of your date control in OpenArgs.
I've chosen a ";" as my delimiter. I'm using Acc97 so I have to parse this myself,
but you could use the Split function in later versions.

So here's the code to open the Cal form:

DoCmd.OpenForm "frmCal", , , , , , Me.Name & ";" & Me.txtDate.Name

Now in your Calendar form the first thing you need is a form level variable that will hold
a reference to your date control:

Dim ctlDate As Control

That would be declared at the top of the code module in the Declarations section.

Next, this would go in the Open event:

Private Sub Form_Open(Cancel As Integer)
Dim strForm As String
Dim strCtl As String

strForm = Left(Me.OpenArgs, InStr(1, Me.OpenArgs, ";", vbTextCompare) - 1)
strCtl = Mid(Me.OpenArgs, InStr(1, Me.OpenArgs, ";", vbTextCompare) + 1)

Set ctlDate = Forms(strForm).Controls(strCtl)
End Sub

Now put this in your Calendar controls AfterUpdate:

Private Sub ActiveXCtl0_AfterUpdate()
ctlDate = Me.ActiveXCtl0.Value
End Sub

And finally in the Close event:
Private Sub Form_Close()
Set ctlDate = Nothing
End Sub

Keep in mind that this will not work if subforms are involved and you should add some error checking to
the whole thing. Perhaps there is an easier way, but this is what came to mind.
 
I

idk

Hi Dan,

Thank you very much, i must admit that i was closing in on this approach,
but you certainly cleared the fog in my brian.

It does seem that there should be a more elegant way.

Thank you very much.

cheers,

Ian.
Dan Artuso said:
Hi,
Here is one way to do it.

Pass the name of the form and the name of your date control in OpenArgs.
I've chosen a ";" as my delimiter. I'm using Acc97 so I have to parse this myself,
but you could use the Split function in later versions.

So here's the code to open the Cal form:

DoCmd.OpenForm "frmCal", , , , , , Me.Name & ";" & Me.txtDate.Name

Now in your Calendar form the first thing you need is a form level variable that will hold
a reference to your date control:

Dim ctlDate As Control

That would be declared at the top of the code module in the Declarations section.

Next, this would go in the Open event:

Private Sub Form_Open(Cancel As Integer)
Dim strForm As String
Dim strCtl As String

strForm = Left(Me.OpenArgs, InStr(1, Me.OpenArgs, ";", vbTextCompare) - 1)
strCtl = Mid(Me.OpenArgs, InStr(1, Me.OpenArgs, ";", vbTextCompare) + 1)

Set ctlDate = Forms(strForm).Controls(strCtl)
End Sub

Now put this in your Calendar controls AfterUpdate:

Private Sub ActiveXCtl0_AfterUpdate()
ctlDate = Me.ActiveXCtl0.Value
End Sub

And finally in the Close event:
Private Sub Form_Close()
Set ctlDate = Nothing
End Sub

Keep in mind that this will not work if subforms are involved and you
should add some error checking to
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top