Mimic Edit fields

chrisguk

Registered User.
Local time
Today, 07:04
Joined
Mar 9, 2011
Messages
148
I have a form with various fields. When the form opens a majority of the fields are NOT enabled.

I then have a command button "Edit" that makes the fields editable with the Me.field.Enabled = True command.

When I try to mimic the save edit I get an error message stating you cannot disable a field that is in focus. My code is below:

Code:
Private Sub btnEditRecord_Click()
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!ProfileClass.Enabled = True
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!MeterpointDetail.Enabled = True
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!dteEnergised.Enabled = True
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!dteDeEnergised.Enabled = True
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!cboActiveStatus.Enabled = True
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!cboCRCStatus.Enabled = True
End Sub
Private Sub btnSaveRecord_Click()
DoCmd.RunCommand acCmdRefresh
Me.btnSaveRecord.SetFocus
If MsgBox("Are you sure you want to save?", vbYesNo, "Save") = vbYes Then
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!ProfileClass.Enabled = False
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!MeterpointDetail.Enabled = False
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!dteEnergised.Enabled = False
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!dteDeEnergised.Enabled = False
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!cboActiveStatus.Enabled = False
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form!cboCRCStatus.Enabled = False
End If
 
Assuming that the code you've shown is accurate, when you click on the btnSaveRecord Command Button, you have moved Focus to that Control, so the Error Message you're getting would appear to be bogus!

I'm curious as to why you're setting Focus to the btnSaveRecord button in the button's own OnClick event, as this is useless. But even with this in place, I haven't been able to duplicate your problem. The only time code like this should generate this error would be if it were to be placed in the OnClick or DoubleClick of the Control you're trying to disable.

My Best Guess would be that the Command Button Control is corrupted. Try deleting iot and re-creating it.

Linq ;0)>
 
BTW that code could be made a lot more concise.

At the very least, use a With block.

For example in the second procedure:

Code:
With Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form
     !ProfileClass.Enabled = True
     !MeterpointDetail.Enabled = True
     etc
End With

I would also recommend you place this Enabling routine in a procedure of its own and call it from the Save procedure.

I also expect you have another procedure that Disables them.
Put it all in one procedure with an Argument like this and call the Enable or Disable using the argument.

Code:
Private Sub SaveEnable(Enable As Boolean)
 
With Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form
     !ProfileClass.Enabled = Enable
     !MeterpointDetail.Enabled = Enable
     etc
End With
 
End Sub

Better still Tag the controls to be affected and use a For Each Loop.

Code:
Private Sub SaveEnable(Enable As Boolean)
 
Dim ctrl as Control
 
For Each ctrl In Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form
     If ctrl.Tag = "mytag" Then ctrl.Enabled = Enable
Next
 
End Sub

Better still is this idea by Stopher (post #8) to group the controls without requiring a Tag. I have not tried it but it sounds awesome.
 
And continuing:

The enabling sub would be better moved into the Form Class Module where the controls are located.

Code:
 For Each ctrl In Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form
becomes:
Code:
For Each ctrl In Me

(Not much of your original code left now is there? But we are not finished yet.)

Declare it as Public in that module and it becomes a Method of the form.

Now when the controls are to be Enabled use:

Code:
Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form.SaveEnable True

You can now call the Method of that form from anywhere by addressing the instance of the form.

If you refer to that form regularly from that module, set it as an object variable.

In the Delcarations of the Module of the form that calls the method:
Code:
Dim MyFormInstance As Form

In the OnLoad porcedure of that form:
Code:
Set MyFormInstance = Me!frmSitesAccount.Form!frmSiteUtility.Form!frmSitesUtilityMeterPoints.Form

The command to Enable the controls becomes:
Code:
MyFormInstance.SaveEnable True

Now in your Save procedure it is very easy to see what is going on without the clutter of seeing every line of code. This kind of structuring vastly simplifies and enhances the comprehension and maintence of code.
 

Users who are viewing this thread

Back
Top Bottom