Disabling a button under certain criteria

sdelgado

New member
Local time
Yesterday, 21:45
Joined
Sep 1, 2004
Messages
8
In the code below, I want this button to disable if data is missing in the 'batchnumber' text box. The setfocus is because I can't disable a button that has the focus. The problem is the script is apparently ignoring my if and running the else. Can anyone see where I messed up????? :confused:

Private Sub btnNewKid_Click()
On Error GoTo Err_btnNewKid_Click

If Me!BatchNumber < 1 Then
With PeriodEndDate
.setfocus
with btnNewKid
.Enabled = False
End With
End With
GoTo Exit_btnNewKid_Click

else

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, , acMenuVer70
DoCmd.GoToRecord , , acNewRec
DoCmd.Close acTable, "tblChildMatch", acSaveNo
Me!PeriodEndDate.SetFocus

End If

Exit_btnNewKid_Click:
Exit Sub

Err_btnNewKid_Click:
MsgBox Err.Description
Resume Exit_btnNewKid_Click

End Sub
 
try this...
Code:
If IsNull(BatchNumber) Or BatchNumber = "" Or BatchNumber < 1 Then
    PeriodEndDate.SetFocus
    btnNewKid.Enabled = False
    Exit Sub
Else
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.RunCommand acCmdRecordsGoToNew
    [COLOR=SeaGreen]'why in the world is your table open to the user?!?[/COLOR]
'    DoCmd.Close acTable, "tblChildMatch", acSaveNo
    PeriodEndDate.SetFocus
End If
 
I gave it a try but when I try the button on the form it says 'Object is Required'

With that table, that is actually a line of code I need to delete but had forgotten it till I sent it out to forums for help this morning. Thanks for pointing it out though.

:o
 
Does your code compile?

Also, you need to ensure that your are testing the value of the text box [not the name of the field in the table]. Check that you have correctly named your text boxes for they should have a prefix like 'txt' or how ever you define your naming convention. Like this...
Code:
If IsNull(txtBatchNumber) Or txtBatchNumber= "" Or txtBatchNumber < 1 Then
    txtPeriodEndDate.SetFocus
    btnNewKid.Enabled = False
    Exit Sub
Else
    DoCmd.RunCommand acCmdSaveRecord
    DoCmd.RunCommand acCmdRecordsGoToNew
    txtPeriodEndDate.SetFocus
End If
 
What Finally worked

:) If Me!BatchNumber < 1 Or Trim(Me!BatchNumber).Value = "" Then
With PeriodEndDate
.SetFocus
With Me!btnNewKid
.Enabled = False
End With
End With
GoTo Exit_btnNewKid_Click
Else

DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,

acMenuVer70
DoCmd.GoToRecord , , acNewRec
Me!PeriodEndDate.SetFocus

End If :)
 

Users who are viewing this thread

Back
Top Bottom