How to make a Add key disabled after adding one record unless

J

Jack

Hi,
I have a form where I am adding records. There are two boxes where one has
to put the lineitemnumber and another box where one has to put the
description. Unless the above two boxes are filled in the Add command box
need to be disabled. Now, the problem is after adding one record the Add
button is enabled and I cannot disable it. I would appreciate any help to
solve this.
The error message is:
The methos is not supported
The following is the code:
Private Sub cmdadd_Click()

On Error GoTo Err_cmdadd_Click
If (IsNull(Me.LineItemID) = True) Or (IsNull(Me.Description) = True) Then
MsgBox ("You cannot save null values to the system")
End If
Dim db As Database
Dim rstApp As Recordset

Dim strSQL As String
Dim strLineItemID As String

Set db = CurrentDb
Set rstApp = db.OpenRecordset("tblLineItems", dbOpenDynaset)
strLineItemID = Trim(Me.LineItemID)

If MsgBox("OK to Add " & strLineItemID & " ?", vbYesNo, "Confirm Add") =
vbYes Then
With rstApp
.AddNew
!LineItemID = Me.LineItemID
!Description = Me.Description
strSQL = "SELECT [tblLineItems].[LineItemID],
[tblLineItems].[Description] FROM tblLineItems order by tblLineItems"

.Update
MsgBox ("Records added to system")
End With

Else
MsgBox "Add Cancelled"
End If
Forms![frmAddLineItems]![LineItemID] = ""
Forms![frmAddLineItems]![Description] = ""
Me!LineItemID.SetFocus = True
Me!cmdadd.Enabled = False
Exit Sub
Exit_cmdadd_Click:
Exit Sub
Err_cmdadd_Click:
MsgBox Err.Description
Resume Exit_cmdadd_Click

End Sub
 
C

Carl Rapson

Jack said:
Hi,
I have a form where I am adding records. There are two boxes where one has
to put the lineitemnumber and another box where one has to put the
description. Unless the above two boxes are filled in the Add command box
need to be disabled. Now, the problem is after adding one record the Add
button is enabled and I cannot disable it. I would appreciate any help to
solve this.
The error message is:
The methos is not supported
The following is the code:
Private Sub cmdadd_Click()

On Error GoTo Err_cmdadd_Click
If (IsNull(Me.LineItemID) = True) Or (IsNull(Me.Description) = True)
Then
MsgBox ("You cannot save null values to the system")
End If
Dim db As Database
Dim rstApp As Recordset

Dim strSQL As String
Dim strLineItemID As String

Set db = CurrentDb
Set rstApp = db.OpenRecordset("tblLineItems", dbOpenDynaset)
strLineItemID = Trim(Me.LineItemID)

If MsgBox("OK to Add " & strLineItemID & " ?", vbYesNo, "Confirm Add")
=
vbYes Then
With rstApp
.AddNew
!LineItemID = Me.LineItemID
!Description = Me.Description
strSQL = "SELECT [tblLineItems].[LineItemID],
[tblLineItems].[Description] FROM tblLineItems order by tblLineItems"

.Update
MsgBox ("Records added to system")
End With

Else
MsgBox "Add Cancelled"
End If
Forms![frmAddLineItems]![LineItemID] = ""
Forms![frmAddLineItems]![Description] = ""
Me!LineItemID.SetFocus = True
Me!cmdadd.Enabled = False
Exit Sub
Exit_cmdadd_Click:
Exit Sub
Err_cmdadd_Click:
MsgBox Err.Description
Resume Exit_cmdadd_Click

End Sub

The easiest way for me has been to check for values in both fields in each
field's AfterUpdate event, and if both are non-Null enable the button. The
error you're getting is probably caused by the fact that you can't disable a
command button while it has focus, and the button has focus because you're
in the button's Click event. Just shift focus to some other control, and
then you can disable the button.

Carl Rapson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top