Startup form crashes Access

A

Adam

I have some code written in the load event of a form in an Access 2000
database. If the database is already open and I open the form, the code runs
just fine.

However, if I set the form as my startup form (as I would like to be able to
do), then the form doesn't seem to finish loading properly (at least the
code doesn't do what it is supposed to do). Then when I try to close the
form, Access crashes and gives the following message:

'MSACCESS.exe has generated errors and will be closed by Windows. You will
need to restart the program.'

Does anyone have any idea what could cause perfectly good code not to run
when it is on a startup form, and what can be done to work around this
problem? The form doesn't try to do anything very complicated, just look up
a value of a variable in a table, and then open another form, the form it
opens depending on what it finds.

Many thanks
 
V

Van T. Dinh

Don't know why but I guess there is some timing issues
there.

1. Try putting some break points in the code for the
Forms and see how far the code gets. If you can complete
the code and open the Form after stepping through the
code. there is some problems with timing.

2. Try using the AutoExec Macro to open the Form rather
than the Startup Options ...

HTH
Van T. Dinh
MVP (Access)
 
A

Adam

Your suggestion about timing issues got me thinking. What I have done is I
have put the code in the form's timer event, rather than its load event, and
set the delay for 1 second. It now seems to work fine. I can live with a
delay of 1 second. I guess Access must need to load lots of stuff into
memory before code can run properly, and the short delay lets it do so.

However, if anyone knows differently, I'd be interested to hear any
alternative explanations.

Many thanks

Adam
 
V

Van T. Dinh

Describe what the code is supposed to do and post the code in your
Form_Timer Event Procedure (former Form_Load Event Procedure).
 
A

Adam

No problem. The database can have various different modes, each of which
requires a different form to be shown on startup. The purpose of the code is
to detect which mode the database is in and show the appropriate form. The
current mode is stored in a field in a table.

Adam

Code follows:

Private Sub Form_Timer()

' This opens only briefly, then selects another form to open instead.
' The form to open depends on the current database mode.

Dim vCurrentMode As Integer
Dim vMsg As String
Dim vNewForm As String

' This uses the function GetDbMode which I wrote in a separate module,
' which looks up the mode value in the appropriate table

vCurrentMode = GetDbMode

' NB all the bits beginning with lower case c in the part below
' are constants defined in a separate module

Select Case vCurrentMode
Case cPreSetupMode
vNewForm = cNewDbForm
Case cSetupMode
vNewForm = cSetupForm
Case cTestMode
vNewForm = cTestModeForm
Case Else
' CockUp is an error handling procedure in a separate module
CockUp "Unexpected database mode in Universal Startup Form"
End Select

DoCmd.OpenForm vNewForm
DoCmd.Close acForm, cStartupForm

End Sub
 
V

Van T. Dinh

That sounds fine.

I think the problem was the Module with all the constants hasn't been loaded
by the time your code is executed.

You may like to try the Current Event of the preliminary Start-up Form.
 
A

Adam

Yes, your suggestion about the module not being loaded seems to fit with all
the evidence. It certainly explains why the code works fine if I put it in
the form's timer event.

I tried putting it in the current event, but that doesn't work, as for some
reason trying to close the form in the current event generates an error. But
no matter. I think this problem is solved by putting it in the timer event.
I tried a half second delay instead of a 1 second delay, and that also works
fine. At some stage I might experiment to see just how short a delay I can
get away with.

Many thanks for all your help.

Adam
 
V

Van T. Dinh

Don't make it too short. Your database can be used on a slower machine and
the timing problem will come back.
 

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