PDA

View Full Version : Problems With Screen.ActiveForm.Controls



Mattster2020
01-13-2009, 04:36 AM
Morning All,

I have recently used the below code to create two command buttons and a text box, the command buttons are use to increment and deincrement values contained within the textbox. This functionality works fine when tested on the form Frm_Timesheet_Capture_1 directly.

The problem ocurs due to Frm_Timesheet_Capture_1 being used as a sub form on form Frm_Timesheet. The line highlighted in red below is the problem, I think.

Ive tried to use the property Screen.ActiveControl.Controls to reference the subform, but this doesnt work.

Could anyone point me in the right direction as to how I would get this working?

Function IncOrDecDate(intIncOrDec As Integer, strDateCtrl)
Dim intI As Integer
Dim objDateCtrl As Object

For intI = 1 To 1500 ' Start loop.
If intI Mod 100 = 0 Then ' If loop has repeated
' 100 times.
DoEvents ' Yield to operating
' system.
End If
Next intI
Set objDateCtrl = Screen.ActiveForm.Controls(strDateCtrl)
objDateCtrl.Value = objDateCtrl.Value + intIncOrDec
End Function

CreganTur
01-13-2009, 06:35 AM
When you post code please wrap it in VBA tags (click the green VBA button when you post). This will format it according to VBIDE, which makes it easy to read.


The problem ocurs due to Frm_Timesheet_Capture_1 being used as a sub form on form Frm_Timesheet
You pointed out your problem right here. You're trying to access it as a normal form when it's being used as a sub form! YOu have to refer to it as a subform.

Creating controls at runtime is a practice that is generally frowned upon because it reduces your control over what your database does, and can result in a lot of unexpected errors and problems.

Mattster2020
01-13-2009, 06:51 AM
Thanks CreganTur,

I thought that may have been my problem, although Im not sure how to get around the issue using code, ive read that using Screen.ActiveControl may solve the problem, but I seem to run into problems with this.

Function IncOrDecDate(intIncOrDec As Integer, strDateCtrl)
Dim intI As Integer
Dim objDateCtrl As Object

For intI = 1 To 1500 ' Start loop.
If intI Mod 100 = 0 Then ' If loop has repeated
' 100 times.
DoEvents ' Yield to operating
' system.
End If
Next intI
Set objDateCtrl = Screen.ActiveControl.Controls(strDateCtrl)
objDateCtrl.Value = objDateCtrl.Value + intIncOrDec
End Function