Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to hold the vbacode until form in function is closed 1

Status
Not open for further replies.

QbLulu

Programmer
Feb 24, 2009
6
NL
I have a form with a button on it.
the code under "onclick" event is like

sub buttononclick
DoFuction
Msgbox("Hello world")
end sub

Function DoFunction
Dim form1 as form
Set frm = New Form_f_form2
clnIdentificationForms.Add Item:=frm, Key:=CStr(frm.Hwnd)
Set frm = Nothing
end funct

form2 has a button bttnQuit which closes the form. If I run this situation now the messagebox pops up as soon as the form in the doFunction appears in staed of after the closing of that form. How can I hold the code until form2 is closed?
 
I believe that in the Letwin and Getz Access Developers Handbook they provide an example of doing this. I have not done it myself. With a standard form you can open it in dialog and stop code execution, but not with a form instance. Just because a form opens does not mean any code stops running. I will take a look at the example when I get home but I think it uses a loop that continues to validate that the form is loaded.

Pseudo code.

sub buttononclick
dim hndl as variant
hndl = DoFuction
do until notLoadedInstance(hndl)
Msgbox("Hello world")
loop
end sub

Function DoFunction as variant
Dim form1 as form
Set frm = New Form_f_form2
clnIdentificationForms.Add Item:=frm, Key:=CStr(frm.Hwnd)
doFunction = frm.Hwnd
Set frm = Nothing
end func

function notLoadedInstance(hndl as variant)as boolean
some code
end function
 
I had to puzzle a bit on it but I got it working

Now I got on the main form:

Private Sub bttn_Click()
Dim hndl As Variant
hndl = OpenForm(<arguments>)
Do Until Not FormOpen(hndl)
DoEvents
Loop
<code to do after from has been closed
End Sub

Function OpenFrom
Dim form1 as form
Set frm = New Form_f_form2
clnIdentificationForms.Add Item:=frm, Key:=CStr(frm.Hwnd)
OpenForm = frm.Hwnd
Set frm = Nothing
end funct

Public Function FormOpen(hndl As Variant)As Boolean
On Error Resume Next
Dim objForm As Form
For Each objForm In Forms
If objForm.Hwnd = hndl Then
FormOpen = True
Exit Function
End If
Next
FormOpen = False
End Function


The latter is loosely based on a function in formhelper.bas to be found at
OK, thanks
 
One thing I forgot was the doevents which is key because you need to give control back to the processor. Good catch. The other thing is that continuously checking for an open form is processor intensive. So you want to go into your loop, but only check maybe every 1000 times through the loop. Something like
(pseudo code)

constant adhcInterval = 1000
dim loopCounter as long
dim frmOpen as boolean
Dim hndl As Variant
hndl = OpenForm(<arguments>)
frmOpen = true
Do
DoEvents
if loopCounter mod adhcInterval = 0 then
frmOpen = FormOpen(hndl)
end if
loupCounter = loopCounter + 1
Loop until not frmOpen

This way only calling FormOpen every 1000 times.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top