Programmatic changes to form won't save

2019-08-07 07:29发布

问题:

Does anyone have an idea what could prevent either of these commands from actually saving a form? Is there a setting I'm missing that could make the form "read-only"? I'm toggling the control.enabled property on the form on load, but I can't seen to make it stick once I've closed the form.

DoCmd.Close acForm, Me.Name, acSaveYes
DoCmd.Save acForm, Me.Name

Edit: Some psuedo code to clarify. This function runs on form load to enable/disable certain controls and works 100% as expected.

Private Sub Form_Load()
     If IsNull(TempVars.Item(Me.Name)) Then
         TempVars.Add Me.Name,1

         If somecondition = true then
             Me.Controls.Item("somecontrol").enabled = True
         Else
             Me.Controls.Item("somecontrol").enabled = False
         End If
     End If
End Sub

This click event closes the form and opens another form. The problem is, the control.enabled settings I set on form load do not save.

Private Sub button_OnClick()
    DoCmd.OpenForm "someotherform", acnormal
    DoCmd.Close acForm, Me.Name, acSaveYes
End Sub

The next time I open the first form, the control properties will not be set, and I need to set the conrol.enabled property again.

回答1:

I've not been able to find any documentation, but it appears that you can not save design changes that are made in Normal mode. I ended up declaring a global collection type variable. I stored concatenated strings in this collection variable of the convention "Form.Name & Control.Name". If the form had never been opened, I ran the long check on which controls should be enabled, and stored them in this collection global. If the form had been opened at least once, I looped through the controls on the form checking to see if they were in my global collection of "authorized" controls. It feels really hackish, but it works pretty well.



回答2:

The code you have (Close, then Save) fails on my testing because the Me.Name is empty after executing the Close. Although I don't know what other code you may have, I was able to get the Form to close once I switched the order.

I suggest you place the code in the 'Open' event as follows:

Private Sub Form_Open(Cancel As Integer)
    DoCmd.Save acForm, Me.Name
    DoCmd.Close acForm, Me.Name, acSaveYes
End Sub


回答3:

Do you want to change the enabled property on a condition? If you just need that control disabled IF the record ... then you'll want to move the enabling the form's Current event, which fires at every record navigation. Load and Open are too early and don't repeat at navigation.

Private Sub Form_Current()
  If Me!X = True Then
    Me.Controls("somecontrol").Enabled = False
  Else
    Me.Controls("somecontrol").Enabled = True
  End If
End Sub


标签: access-vba