Waiting for subroutine to finish

  • Thread starter Thread starter Dominic Vella
  • Start date Start date
D

Dominic Vella

I want to stop exectution of my code until another routine had finished
This is not the actual code I'm trying to create, but it is a sample of how
I want the code to halt.
Something like this:

Sub DoChanges
Dim intCount as integer
For intCount = 1 to 10000
Next intCount
DoCmd.Open Form "MyForm", , , ,acDialog
End Sub

Sub BigCalendar
DoChanges
' I'd like to halt here until timing has finished
MsgBox "Done"
End Sub


Is it possible to stop the code?
 
VBA is not multi-threaded, so your code will work as is.

When you call DoChanges, the MsgBox won't show until DoChanges completes.

When you open MyForm in dialog view, that pauses the rountine. So DoChanges
DoChanges won't complete until you close the dialog, and therefore it won't
pass control back to BigCalendar, and so the MsgBox won't dispaly until
after the dialog form closes.
 
As Allen has mentioned, your code will run as is. You do not need to wait
unless you are using an outside process. However, your wait code is
processor dependent. Faster processors will run it faster than slower
processors. Instead use the computer's clock which should tick off the same
time on any machine (give or take a few milliseconds a day). The following
code will delay as long as you want for an outside process. It will stop
your current code until the time is up:

http://www.datastrat.com/Code/Delay.txt
 
Well, I'm as stumped as you then, so I'll give a clearer picture of what I'm
doing.

'== In form1 I've got a routine which calls a module routine.
Private Sub cmdDetailEdit_Click()
EditSingle("Item Details", "tblItems", "[item_id]=" & Me.lstDetail,
"item_detail", "Item Heading")
Me.lstDetail.Requery
End Sub

'== In Module1 the routine designs a template form
Public Sub EditSingle(strFormCaption As String, strTable As String,
strCriteria As String, strTextField As String, strTextCaption As String)
DoCmd.OpenForm "fdlgTemplate", acNormal
With Forms("fdlgTemplate")
.Caption = strFormCaption
.RecordSource = "SELECT " & strTable & ".* FROM " & strTable & "
WHERE " & strCriteria & ";"
.lblHeading.Caption = strFormCaption
.txtField.ControlSource = strTextField
.lblField.Caption = strTextCaption
End With
DoCmd.OpenForm "fdlgTemplate", , , strCriteria, , acDialog
End Sub

The problem is the form1.lstDetail requery's before fdlgTemplate has
finished. Can anyone explain or help?

Thanks

Dominic
 
But fdlgTemplate is not opened in dialog mode.

Trying to open it in dialog mode when it's already open is not going to be
useful.
 
I would normally agree, however I need to open the form in normal mode so
that I can change the forms Record Source, change the Label caption and
change the text box Control Source.

I then after the form is closed I want the list to refresh.

Well, I figure my last option then is to pass the reference to the list to
the Dialog form too so that the Dialog form will do the refreshing when it
closes. Does that sound like the best idea?

By the way, thanks for your input. I appreciate it.

Dom

Allen Browne said:
But fdlgTemplate is not opened in dialog mode.

Trying to open it in dialog mode when it's already open is not going to be
useful.

--
Allen Browne - Microsoft MVP. Perth, Western Australia

Reply to group, rather than allenbrowne at mvps dot org.

Dominic Vella said:
Well, I'm as stumped as you then, so I'll give a clearer picture of what
I'm doing.

'== In form1 I've got a routine which calls a module routine.
Private Sub cmdDetailEdit_Click()
EditSingle("Item Details", "tblItems", "[item_id]=" & Me.lstDetail,
"item_detail", "Item Heading")
Me.lstDetail.Requery
End Sub

'== In Module1 the routine designs a template form
Public Sub EditSingle(strFormCaption As String, strTable As String,
strCriteria As String, strTextField As String, strTextCaption As String)
DoCmd.OpenForm "fdlgTemplate", acNormal
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

With Forms("fdlgTemplate")
.Caption = strFormCaption
.RecordSource = "SELECT " & strTable & ".* FROM " & strTable & "
WHERE " & strCriteria & ";"
.lblHeading.Caption = strFormCaption
.txtField.ControlSource = strTextField
.lblField.Caption = strTextCaption
End With
DoCmd.OpenForm "fdlgTemplate", , , strCriteria, , acDialog
End Sub

The problem is the form1.lstDetail requery's before fdlgTemplate has
finished. Can anyone explain or help?
 
Back
Top