Full Version: How To Enable A Command Button Only When All Controls Populated
UtterAccess Forums > Microsoft® Access > Access Forms
probably a bonehead question, but ...

I'd like to disable a Save button on an Access form until such time as all controls contain a value. I could put code in every control's AfterUpdate event to check the contents of all controls and then enable/disable the Save button based on the result, but is that the only way?


I would use the code behind the Save button to first loop through all the controls and test for a value. If the loop encounters an null value, then exit the loop set focus to the offending control and display a message that data needes to be entered.
If you need any more help with this let us know.
Here's some boilerplate code I use for doing this kind of thing:

Private Sub cmdSave_Click()

  Dim ctl As Control

  Dim count As Integer

  count = 0

  For Each ctl In Me.Controls

  If (ctl.ControlType = acTextBox) Or (ctl.ControlType = acComboBox) Then
   If Nz(ctl, "") = "" Then
    count = count + 1
   End If

   End If


  If count > 0 Then
   MsgBox "All Fields Must Be Filled In!"
   Exit Sub
   DoCmd.RunCommand acCmdSaveRecord
  End If

End Sub

Linq ;0)>
I'd tackle that slightly differently.
I'd build a from level sub that enabled/disabled the command button.

And I'd call that from the after update of each of the text boxes. (and the on current of the form.)

My thinking for that is
I'm developing once, users are using this all day every day.
So let's stop them from making a mistake, rather then telling them they've done it wrong (again).

I'd also change the background colour of all the 'mandatory' fields, so they know which ones have to be completed. So no need for a message, you can't hit save unless all the fields have data.
This is a "lo-fi" version of UA. To view the full version with more information, formatting and images, please click here.