Disable save button (1 Viewer)

ivonsurf123

Registered User.
Local time
Today, 07:04
Joined
Dec 8, 2017
Messages
69
Hello,

I am trying to disable save button when the cboYear is not <> currentYear,
right now the code I have is disabling all the years including the current year when I filter the cboYear, Do I need to add this code somewhere else like on Load or Current? Or Am I missing something else? Please Help. Thank you.

Code:
Private Sub cboYear_AfterUpdate()

    Dim myDate
    
    myDate = Year(Date)
    
    If (Me.cboYear) <> myDate Then
        Me.cmdSave.Enabled = False
    Else
        Me.cmdSave.Enabled = True
    End If
    
    If IsNull(Me.cboYear) Then
        Me.subform_Register_PublicHolidays.Form.Filter = ""
        Me.subform_Register_PublicHolidays.Form.FilterOn = False
    Else
        Me.subform_Register_PublicHolidays.Form.Filter = "Year(PHDate)=" & Me.cboYear
        Me.subform_Register_PublicHolidays.Form.FilterOn = True
    End If


Proc_Exit:
   Exit Sub
Proc_Error:
   MsgBox "Error " & Err.Number & " in setting subform filter:" & vbCrLf & Err.Description
   Resume Proc_Exit
End Sub
 

jleach

Registered User.
Local time
Today, 10:04
Joined
Jan 4, 2012
Messages
308
The logic looks correct... try being more explicit with types (you're leaving a lot up to interpretation by the compile/runtime):

Code:
Dim myDate [B]As Integer[/B]
MyDate = Year(Date)

If [B]CInt([/B]Me.cboYear[B].Value)[/B] <> MyDate Then
...

(if the combo is nullable, wrap that check in NZ() as well)
 

ivonsurf123

Registered User.
Local time
Today, 07:04
Joined
Dec 8, 2017
Messages
69
OMG! jleach that was beautiful CInt did it. Thank you so much!
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:04
Joined
Feb 19, 2002
Messages
43,223
NOT really solved. You have a gaping hole in your logic.

Validation belongs in the BEFOREUpdate event so you can PREVENT data from being saved if it is incorrect. Using the AfterUpdate event is akin to shutting the barn door after the horses have escaped. Comparing two fields for = or <> will NOT return the expected results if one of the fields is Null. So a <> b can NEVER be true if either a or b is null.

Also, keep in mind that code in control level events won't ever run if the control isn't updated or even entered so most validation code should actually be performed in the FORM's BeforeUpdate event.
 

ivonsurf123

Registered User.
Local time
Today, 07:04
Joined
Dec 8, 2017
Messages
69
Thank you Pat! I Will check my code and test it to follow your advised, I appreciate it.
 

Users who are viewing this thread

Top Bottom