Programming with Control variables

usermj

Registered User.
Local time
Today, 16:47
Joined
Dec 12, 2004
Messages
39
The following code is only for testing the usage of control variable. I want to use set statement to assign the textbox object reference to the varialbe myControl.

When I clicked the button, the system always tells me 'the object reference missing'. It seems the set statement doesn't work, but I don;t know the reason. could anyone of you please spot it out for me?

Many thanks



Option Compare Database
Public myControl As control
Public frm As Form
Set frm = Forms!Form1


Sub Command4_Click()

Set myControl = frm!Text0
myControl.SetFocus

End Sub

Sub Command5_Click()
Set myControl = frm!Text2
myControl.SetFocus
End Sub
 
I know that you say, "The following code is only for testing the usage of control variable", but before going too much further might I recommend the following: -

Include Option Explicit
Compile your work
Don’t use names as supplied by the wizards
Try to remove the bang '!' from your code and use the dot '.' instead
Try to remove Public variables where possible
Use Me rather than the Form name where possible
If using the Forms collection try using Forms("FormName")
If referring to controls on another form try using Forms("FormName")("ControlName")

Regards,
Chris.
 
Include Option Explicit
Compile your work
Don’t use names as supplied by the wizards
Try to remove the bang '!' from your code and use the dot '.' instead
Try to remove Public variables where possible
Use Me rather than the Form name where possible
If using the Forms collection try using Forms("FormName")
If referring to controls on another form try using Forms("FormName")("ControlName")

Regards,
Chris.[/QUOTE]


Thanks Chris.

I did some changes based on what you sugguested. It works :)
I put 'Option Explicit' at the top and used 'Me' referring to the current form rather than use '!'.

The next problem is:

I designed a calendar user interface (using Calendar Active Control) for users to select date value. As this form will be linked with several other interface and returns the date value to a textbox on whichever form sends the request, this is the reason why I try to use Control variable. After users pick the date, the calendar interface can use that control variable to set the focus back to the form which sends the request.

So, in this case, I still need to use the Form collection, but it doesn't work :(

any idea?

thanks
 
There are many ways to do it, this is just one.

'On the calling form: -
DoCmd.OpenForm "frmCalendarControl", , , , , acDialog, Me.Name & "|" & "ControlName"
DoCmd.Close acForm, "frmCalendarControl"

'On the Calendar Control: -
Forms(Left$(Me.OpenArgs, InStr(Me.OpenArgs, "|") - 1))(Mid$(Me.OpenArgs, InStr(Me.OpenArgs, "|") + 1)) = Date

Sorry for the long line of code but have a play with that or variations of it.

Regards,
Chris.
 

Users who are viewing this thread

Back
Top Bottom