Form code runs too quickly

S

Sandy

I have the following code attached to my form - works fine other than when
the form is first opened. If the third "if" is true, the code runs before
the form is even open!

Any way to have the form open first before the message box displays?

Just one more small point - what syntax do I use to have the message in the
message box show on two lines and also have double quotes around "Job
Complete" (as I have just typed)?

Private Sub Form_Current()
If IsNull(txtDateCollected) Then
Me.lblJobComplete.Visible = False
Me.chkJobComplete.Visible = False
ElseIf Not IsNull(txtDateCollected) And Me.chkJobComplete = True Then
Exit Sub
ElseIf Not IsNull(txtDateCollected) And _
Not IsNull(Forms![Finalise
Jobs]![FinaliseJobDetailsSubform].Form.cboRepairer1) Then
Me.lblJobComplete.Visible = True
Me.chkJobComplete.Visible = True
MsgBox "If this job is now complete check the Job Complete check
box", vbOKCancel
Me.chkJobComplete.SetFocus
End If
End Sub

Thanks
Sandy
 
J

Joe D

Sandy: Someone else may have a slicker way to deal with this, but when I
want to slow down the action I use a Pause Function (adapted from another
forum suggestion). Place this in a module:

Public Function Pause(PauseSeconds As Integer)
Dim Start
Start = Timer
Do While Timer < Start + PauseSeconds
DoEvents
Loop
End Function

Then place Pause 1, for example, in your code to pause for a second. Change
the pause amount to your liking.

To get the double quotes in the message, do this:


MsgBox "If this job is now complete check the " & Chr(34) & "Job Complete" &
Chr(34) & " check box", vbOKCancel

HTH

JoeD
 
J

Jeanette Cunningham

Sandy,
when you put some code in the current event for the form, that code will run
while the form is open and loading and will have finished running by the
time the form has finished opening. You can't change this behaviour of
Access. If you want the user to only see the message after the form opens,
you will need to remove the message box from the current event. One
suggestion is to use a label instead of a message box. Make the label
visible or hide it depending on the values.
Another suggestion is to use a form to replace the message box.

Jeanette Cunningham
 
L

Linq Adams via AccessMonster.com

Nice solution, Jeanette! I've seen this question come up a number of times
over the years without a successful resolution. And mimicing the messagebox
with a popup form also has the advantage of allowing much more in the way of
formatting than a real messagebox does!

Linq
 
S

Sandy

Hi Joe D, Jeanette, Linq,

Firstly, sorry for delay in coming back to you all, but I had problems with
my computer - fixed now though!

Sadly Joe, the pause function only delays the code from running and still
does not allow the form to open first.

However if I include the following at the top of my code-

Dim strFrmName As String
strFrmName = "Finalise Jobs"

DoCmd.OpenForm strFrmName

the form and the message box appear simultaneously which is a much better
result! Don't fully understand why it works (even when the form is already
open and a different record is selected) but it does.

Thanks for all your responses and I thought you might be interested in my
resolution.
Sandy


Jeanette Cunningham said:
Sandy,
when you put some code in the current event for the form, that code will
run while the form is open and loading and will have finished running by
the time the form has finished opening. You can't change this behaviour of
Access. If you want the user to only see the message after the form opens,
you will need to remove the message box from the current event. One
suggestion is to use a label instead of a message box. Make the label
visible or hide it depending on the values.
Another suggestion is to use a form to replace the message box.

Jeanette Cunningham

Sandy said:
I have the following code attached to my form - works fine other than when
the form is first opened. If the third "if" is true, the code runs before
the form is even open!

Any way to have the form open first before the message box displays?

Just one more small point - what syntax do I use to have the message in
the message box show on two lines and also have double quotes around "Job
Complete" (as I have just typed)?

Private Sub Form_Current()
If IsNull(txtDateCollected) Then
Me.lblJobComplete.Visible = False
Me.chkJobComplete.Visible = False
ElseIf Not IsNull(txtDateCollected) And Me.chkJobComplete = True Then
Exit Sub
ElseIf Not IsNull(txtDateCollected) And _
Not IsNull(Forms![Finalise
Jobs]![FinaliseJobDetailsSubform].Form.cboRepairer1) Then
Me.lblJobComplete.Visible = True
Me.chkJobComplete.Visible = True
MsgBox "If this job is now complete check the Job Complete check
box", vbOKCancel
Me.chkJobComplete.SetFocus
End If
End Sub

Thanks
Sandy
 

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