By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,829 Members | 673 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,829 IT Pros & Developers. It's quick & easy.

Can you stop vba code from running after a DoCmd.Close?

P: 41
I have a rather complicated routine in VBA that performs many checks.

One of the checks will close the window doen using DoCmd.Close, but unfortunately the code tries to continue after this point and causes an error because it is trying to access data on the form I have just closed.

is there a way to tell VBA to DoCmd.Close and then stop running the current subroutine?

I tried End Sub but that causes an error too.
Jan 14 '08 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 1,206
What error are you receiving?

Post a snippet of the offending code if you can.
Jan 14 '08 #2

P: 41
What error are you receiving?

Post a snippet of the offending code if you can.

Well the error being received is due to the code continuing and attempting to perform actions based on the form that I have just closed.

Basically after some calculations I want the routine to close the form and stop if a condition is met. if not it will then continue and go to another record.

obviously I dont want the code to continue after the form closes since it is going to try and open a new record on a form that is no longer open.

I know i can work arround this by adding another condition to the logic after the "close" check but this seems a little unecesary if there is a way to prematurely perform an End Sub.

here is the section of the code causing the problem:
Expand|Select|Wrap|Line Numbers
  1. Me.goto = DLookup("[answer links to]", "answer", "[answer id] = list2")
  2. Me.quit = DLookup("[answer ends]", "answer", "[answer id] = list2")
  4. 'check to see if this is the last question
  6. If Me.goto = 83 Then
  7. MsgBox "Questionnaire complete"
  8. DoCmd.Close
  9. End If
  10. If Me.Questionlinks = 83 Then
  11. MsgBox "Questionnaire complete"
  12. DoCmd.Close
  13. End If
  15. 'after checks have been made and data saved open new question
  17. If Me.multiple = 0 Then
  19. 'if its a select one question then...
Jan 14 '08 #3

Expert 100+
P: 1,206
I guess there are two approaches you could take. One would be to use an else block.

Expand|Select|Wrap|Line Numbers
  1. If Me.goto = 83 And Me.QuestionLinks = 83 Then
  2.     Docmd.Close
  3. Else
  4. 'The rest of your code
  5. End If
Or you could try the Exit Sub command.
Expand|Select|Wrap|Line Numbers
  1. If Me.goto = 83 Then
  2. Docmd.Close
  3. Exit Sub
  4. End If
Jan 14 '08 #4

P: 41

Exit Sub is exactly what i was looking for.

I could have worked arround it with Else blocks but i think it would have become unecesarily complex (there are many other sections after this piece of code.)

thanks again.
Jan 15 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.