My code not work ===> Case vbNo

S

shiro

I have code that doesn't work in case vbNo and don't know why.
Please help.The code works on form BeforeUpdate even but not on
my command button click even.It return me
Variable not define
to the statement Cancel = True


Any guidance would be greatly appreciated
Thank's



Private Sub AddSpec_cmd_Click()
On Error GoTo Err_AddSpec_cmd_Click

Dim strMessage As String
Dim intResponse As Integer
intResponse = MsgBox("Is the spec correct?", vbYesNoCancel, "Confirm")
Select Case intResponse
Case vbYes
If IsNull(Me.Model) Then
strMessage = strMessage & _
" Enter Model Name" & vbCrLf
End If

If InputVoltage.Value < 11 Then
strMessage = strMessage & _
" Input correct voltage rate " & vbCrLf
End If

If Len(strMessage) = 0 Then
DoCmd.DoMenuItem acFormBar, acRecordsMenu, acSaveRecord, ,
acMenuVer70

Else
MsgBox strMessage, vbOKOnly, "Error"
End If

Case vbNo
If MsgBox("Delete data?", vbOKCancel, "Confirm") = vbOK Then
Me.Undo
End If
Cancel = True

Case vbCancel
Cancel = True

End Select

Exit_AddSpec_cmd_Click:
Exit Sub

Err_AddSpec_cmd_Click:
MsgBox Err.Description
Resume Exit_AddSpec_cmd_Click:

End Sub
 
L

Linq Adams via AccessMonster.com

But in case someone searchs and finds this thread but not the other, the
reason is that Cancel, in an OnClick event, is not defined

Private Sub AddSpec_cmd_Click()

while in a BeforeUpdate event it is defined as an Integer:

Private Sub Form_BeforeUpdate(Cancel As Integer)

--
There's ALWAYS more than one way to skin a cat!

Answers/posts based on Access 2000/2003

Message posted via AccessMonster.com
 
S

shiro

Thank's fro quick reply,
but I found new problem in my BeforeUpdate even ( maybe )
It is occured if I close the form.

If I had filled my PK field then close the form,
the data still can be saved,although there are still another
fields leave blank and need to be fill ( mentioned in my code ).
The Messages appear like usually,but it can't prevent from adding
new record.

How to prevent this.Thank's for provide time for helping me.
 
J

Jeanette Cunningham

Shiro,
the message box that asks users if the specs are ok, then asks them if they
want to cancel is making this whole thing more difficult to code for you.
Here is a simpler way.
Remove the message box mentioned above.
In the before update event have code to check for missing entries. If your
code finds missing entries, cancel the update and tell the user what to
complete (via a message box, this message box can give users the option to
cancel or you could have a separate button to delete or cancel the record)

The user has another try at completing the missing entries, clicks the
button again, the before update event code runs again.
If the user has filled in the required fields then you let the update go
through and the record is saved.

Jeanette Cunningham
 
S

shiro

Ms Jeanette
I had follow your suggestion and put the code to BeforeUpdate even
of my form.The problem now :
The changes or new record *still can* be saved although the code found
no data in the required field.Why?.
That is what I'm asking of.Sorry if I'm not easy understand with your
explanation.Again thank's.
 
J

John W. Vinson

Ms Jeanette
I had follow your suggestion and put the code to BeforeUpdate even
of my form.The problem now :
The changes or new record *still can* be saved although the code found
no data in the required field.Why?.
That is what I'm asking of.Sorry if I'm not easy understand with your
explanation.Again thank's.

Please post your form's BeforeUpdate code.

John W. Vinson [MVP]
 
J

Jeanette Cunningham

Shiro,
if your code finds no data in a required control, you need to cancel the
update by setting the value of Cancel to true

Cancel = True
Here is an example.

If IsNull(Me.Model) Then
strMessage = strMessage & _
" Enter Model Name" & vbCrLf
End If

If InputVoltage.Value < 11 Then
strMessage = strMessage & _
" Input correct voltage rate " & vbCrLf
End If

If IsNull(Me.txtNextRequiredControl) then
strMessage = strMessage & "next message" & vbCrLf
End If

'you do the above checks for each control that requires a value
'when you have finished checking the controls you build the message

If Len(strMessage) > 0 Then
'cancel the update and tell user about the problem
Cancel = True
'you may need some code to tidy up strMsg here
'that's up to you
Msgbox strMessage, vbInformation, "your title here"
Else
'all controls have required data
' don't do anything to cancel the update and
'access will save the data in the table
End if

Jeanette Cunningham
 
S

shiro

You're right Ms Jeannette,
I miss the Cancel update when my code complete the checking.
Now it works well,thank's all.
 

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