Disable Button When Text/Combo Fields Are Null (1 Viewer)

Gaddy

Wookie
Local time
Today, 06:01
Joined
Dec 16, 2012
Messages
46
Hi All,

On a form, I want to disable the save button, 'cmdSave' whilst the form's mandatory fields have been left blank.

I've put in a smart tag, called, 'Req' against each required field and have used the following code on the forms current event.

Code:
Private Sub Form_Current()

Dim ctl As Control

For Each ctl In Me.Controls

If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
    
    If ctl.Tag <> "Req" Then
Exit Sub
End If

If ctl.Tag = "Req" And IsNull(ctl.Value) Then
    Me.cmdSave.Enabled = False
Else
    Me.cmdSave.Enabled = True
End If

End If

Next ctl

End Sub

The save button is disabled, but it won't enable again after each field has data entered against it.

I also have this code in the AfterUpdate event in each required field:

Code:
Private Sub cboErrorID_AfterUpdate()

Call Form_Current

End Sub

Any ideas?
 
Last edited:

pbaldy

Wino Moderator
Staff member
Local time
Yesterday, 22:01
Joined
Aug 30, 2003
Messages
36,133
The code is going to exit on the first control that doesn't have "Req" in the tag. I don't think you want either of the first 2 tests. You're also going to end up with the result of the last control tested, whatever it is.
 

Gaddy

Wookie
Local time
Today, 06:01
Joined
Dec 16, 2012
Messages
46
To be honest, this was copied from elsewhere. All I want to do is make an easier way to disable the save button by putting in the tag rather than typing code each combobox/text box...

How can I adapt that code - can you give me an example?

I think I am half-way there:

Code:
Private Sub Form_Current()

Dim ctl As Control

For Each ctl In Me.Controls

If ctl.ControlType = acTextBox Or ctl.ControlType = acComboBox Or ctl.ControlType = acListBox Then
    
If ctl.Tag = "Req" And IsNull(ctl.Value) Then
    Me.cmdSave.Enabled = False
Else
    Me.cmdSave.Enabled = True
End If

End If

Next ctl

End Sub

But I had to put in the tag "Req" for each field for it to work though lol
 
Last edited:

Mihail

Registered User.
Local time
Today, 08:01
Joined
Jan 22, 2011
Messages
2,373
Code:
Private Sub Form_Current()
Dim ctl As Control
    Me.cmdSave.Enabled = False
    For Each ctl In Me.Controls
        If ctl.Tag = "Req" Then
            If IsNull(ctl.Value) Then
Exit Sub
            End If
        End If
    Next ctl
    Me.cmdSave.Enabled = True
End Sub
 

MarkK

bit cruncher
Local time
Yesterday, 22:01
Joined
Mar 17, 2004
Messages
8,187
I wouldn't use the Tag property of controls in design view. You can never, at a glance, find out which controls are tagged. By contrast, if you explicitly list the participating controls in code, it becomes very clear very quickly which will be checked.

What you can do is create an array of the controls you need, and then use that, so . . .
Code:
Property Get MyRequiredControls As Variant
[COLOR="Green"]' Custom array of required controls [/COLOR]
  MyRequiredControls = Array(Me.txtField1, Me.txtField2, Me.txtField3)
End Property

Function RequiredControlsAreFilled As Boolean
[COLOR="Green"]'  Returns True if all controls in the array are filled, else returns False[/COLOR]
  dim var
  for each var in MyRequiredControls
    if Nz(var.value, "") = "" then
[COLOR="Green"]      'found one control not filled, return false[/COLOR]
      RequiredControlsAreFilled = false
      Exit Function
    end if
  next
[COLOR="Green"]  'if function has not exited yet, then all is well[/COLOR]
  RequiredControlsAreFilled = true
end function

. . . and now, various consumers can check the validation function, and set the status of the command button . . .

Code:
Sub Form_Current()
[COLOR="Green"]'  the button is enabled if all controls are filled, else not[/COLOR]
  me.cmdSave.Enabled = RequiredControlsAreFilled
End Sub
 

Gaddy

Wookie
Local time
Today, 06:01
Joined
Dec 16, 2012
Messages
46
Please disregard.
 
Last edited:

Mihail

Registered User.
Local time
Today, 08:01
Joined
Jan 22, 2011
Messages
2,373
Lagbolt's approach is better than my one because is more flexible, and I've said "thank you" for shared it. But. basicaly our approaches are the same and my one should work. Have you tested ? Where is the error ?
 

Gaddy

Wookie
Local time
Today, 06:01
Joined
Dec 16, 2012
Messages
46
Lagbolt's approach is better than my one because is more flexible, and I've said "thank you" for shared it. But. basicaly our approaches are the same and my one should work. Have you tested ? Where is the error ?

Apologies, I've tried it again and it does work. I can't get Lagbolt's to work though.

Code:
Option Compare Database

Property Get MyRequiredControls() As Variant
    MyRequiredControls = Array(Me.cboUserID, Me.cboUtilityID, Me.txtErrorDate, Me.cboErrorID)
End Property

Function RequiredControlsAreFilled() As Boolean
    Dim Var
    For Each Var In MyRequiredControls
        If Nz(Var.Value, "") = "" Then
            RequiredControlsAreFilled = False
            Exit Function
        End If
    Next
    RequiredControlsAreFilled = True
End Function

Sub Form_Current()
    Me.cmdSave.Enabled = RequiredControlsAreFilled
End Sub

Private Sub cmdCancel_Click()
    If Me.Dirty Then
        Me.Undo
    End If
        DoCmd.Close
        DoCmd.OpenForm "frmMainMenu"
End Sub

Private Sub cmdSave_Click()
    Me.txtLogDate = Now()
    Me.AdminID = Forms!frmLogin!cboUser.Column(4)
    MsgBox "Error successfully recorded", vbInformation, "Saved on " & Me.txtLogDate & ""
    DoCmd.GoToRecord , , acNewRec
End Sub
 
Last edited:

MarkK

bit cruncher
Local time
Yesterday, 22:01
Joined
Mar 17, 2004
Messages
8,187
If you want help with a problem, please describe the symptoms of the problem in detail. That it "doesn't work" is not enough info to troubleshoot from.
 

Gaddy

Wookie
Local time
Today, 06:01
Joined
Dec 16, 2012
Messages
46
Sorry, I've only had three hours sleep today.

The save button is greyed out regardless of the fields being filled in.
 

MarkK

bit cruncher
Local time
Yesterday, 22:01
Joined
Mar 17, 2004
Messages
8,187
The code I posted does not respond to updates of individual controls. There is a Form_Current handler only. If you handle the AfterUpdate event of each control and run code like . . .
Code:
Sub TestControl_AfterUpdate()
    Me.cmdSave.Enabled = RequiredControlsAreFilled
End Sub
. . . then the enabled status of the save button may be impacted.
 

Gaddy

Wookie
Local time
Today, 06:01
Joined
Dec 16, 2012
Messages
46
Thanks, but the save button is still greyed out after updating cboUserID...

EDIT: Got there in the end. Had to put the code into every AfterUpdate for each required field which is stated in the array.

I'm learning. As I declared all my comboboxes and textboxes as a array (as one), if one is null then the array is false still - am I on the right tracks?

Thanks, have learnt quite a lot tonight.


After filling in all the required fields and pressing esc to clear, the save button is enabled, but needs to be greyed out. I know it's probably simple, but how do I do it?
 
Last edited:

MarkK

bit cruncher
Local time
Yesterday, 22:01
Joined
Mar 17, 2004
Messages
8,187
Pressing escape to clear? Why do you press escape? To clear what?
 

Gaddy

Wookie
Local time
Today, 06:01
Joined
Dec 16, 2012
Messages
46
Pressing escape to clear? Why do you press escape? To clear what?

Hi,

If the user starts to fill out the form and realises that they've put the wrong info in, it's easier to just press 'Esc' and start over.

EDIT: I've fixed by adding:
Code:
Private Sub Form_Undo(Cancel As Integer)
 Me.cmdSave.Enabled = False
End Sub
 
Last edited:

Users who are viewing this thread

Top Bottom