error when running code

J

jon

Hi
When users click a control button on my form is a certain tick box is
unchecked it opens a pop up box and asks for confirmation.
If they click cancel I want the code to stop running.
But I am getting the error 'application-defined or object-defined error'
with the code below.
I don't know what I am doing wrong.

Can some explain what I am doing wrong as well a bit of the logic behind the
cancel.

Thanks

Jon




If Me![FreeStatePassFail] = 0 Then

blnok = confirm(" Are You sure the Job has Failed?")
If blnok = -1 Then
DisplayMessage ("oh shit")
Me![CmbFreeStsteStatus] = 4
Me![ChkFreeststateDone] = -1
Set db = CurrentDb()
Set rst = db.OpenRecordset("ComponentNos", dbOpenTable)

With rst
rst.Index = "Componet No"
rst.Seek "=", [CmbJob]
.Edit
.Fields("FreeStateCount") = 0
.Fields("FreeStatePassCount") = 0
.Update

End With
Else

Form.Cancel *** error on this line

End If
End If
 
R

Rick Brandt

Hi
When users click a control button on my form is a certain tick box is
unchecked it opens a pop up box and asks for confirmation. If they click
cancel I want the code to stop running. But I am getting the error
'application-defined or object-defined error' with the code below.
I don't know what I am doing wrong.

Can some explain what I am doing wrong as well a bit of the logic behind
the cancel.

Thanks

Jon




If Me![FreeStatePassFail] = 0 Then

blnok = confirm(" Are You sure the Job has Failed?") If blnok =
-1 Then
DisplayMessage ("oh shit")
Me![CmbFreeStsteStatus] = 4
Me![ChkFreeststateDone] = -1
Set db = CurrentDb()
Set rst = db.OpenRecordset("ComponentNos", dbOpenTable)

With rst
rst.Index = "Componet No"
rst.Seek "=", [CmbJob]
.Edit
.Fields("FreeStateCount") = 0
.Fields("FreeStatePassCount") = 0
.Update

End With
Else

Form.Cancel *** error on this line

End If
End If

Form.Cancel is not correct. What are you wanting to happen (or not
happen)? If this is the end of your code then you don't need to do
anything (do you?).

If you are trying to cancel the changes on the form you need...

Me.Undo
 
J

jon

Hi Rick
just after that I have a close form which I don't want to run so basically
if you get to this point I want to stop running the code and the form is
still open so the user can adjust the error etc.
On this occasion I can move the close form but is there a way of stopping
the code?

Thanks

Jon

Rick Brandt said:
Hi
When users click a control button on my form is a certain tick box is
unchecked it opens a pop up box and asks for confirmation. If they click
cancel I want the code to stop running. But I am getting the error
'application-defined or object-defined error' with the code below.
I don't know what I am doing wrong.

Can some explain what I am doing wrong as well a bit of the logic behind
the cancel.

Thanks

Jon




If Me![FreeStatePassFail] = 0 Then

blnok = confirm(" Are You sure the Job has Failed?") If blnok =
-1 Then
DisplayMessage ("oh shit")
Me![CmbFreeStsteStatus] = 4
Me![ChkFreeststateDone] = -1
Set db = CurrentDb()
Set rst = db.OpenRecordset("ComponentNos", dbOpenTable)

With rst
rst.Index = "Componet No"
rst.Seek "=", [CmbJob]
.Edit
.Fields("FreeStateCount") = 0
.Fields("FreeStatePassCount") = 0
.Update

End With
Else

Form.Cancel *** error on this line

End If
End If

Form.Cancel is not correct. What are you wanting to happen (or not
happen)? If this is the end of your code then you don't need to do
anything (do you?).

If you are trying to cancel the changes on the form you need...

Me.Undo
 
R

Rick Brandt

Hi Rick
just after that I have a close form which I don't want to run so
basically if you get to this point I want to stop running the code and
the form is still open so the user can adjust the error etc. On this
occasion I can move the close form but is there a way of stopping the
code?

Put the Close in your IF and then it won't be run in the ELSE.

At any spot in your code you can use...

Exit Sub

....but that is considered bad form. Code should have one entry point and
one exit point. You can also use GoTo to take your code execution to the
single exit point, but GoTo is even more reviled as bad coding practice.

Setting up your If-Then blocks or other conditional statements so that
only the code you want to run does in any given circumstance is the "most
correct" way to do it.
 

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