Fill a field using another field from another form

I have a data entry form.  One of the fields on the form needs to be selected from a predefined list.   I tried using a combobox to provide this but the list is too big.

Instead I created a search form that opens when the user clicks a command button next to the field on the data entry form.

When this new search form opens the user has the ability to search and select the record they want.   I need the ID selected to be passed back to the Data Entry form.

I have tried instantiating the search form as an oject using code below that is executed from the Data Entry form.

Dim f As New Form_Search

f.Modal = True
f.Visible = True
If f.SelectedItem > 0 Then
'use  f.SelectedItem
End If

Set f = Nothing
The problem is that this code continues to run after f.visible = true.   What I really want is to hold the code until it is hidden so that I reference and use  f.selected item.

Any ideas?
Who is Participating?
NatchiketConnect With a Mentor Commented:
hi lee

An effective technique is to use the OpenForm method with the WindowMode set to acDialogue, this will suspend the calling code from running.

In the seach form once the user has selected an ID, run Me.Visible = False .. this will allow the calling code to to carry on

You then test for the presence of the search form in the calling code using syscmd e,g,

If syscmd(acSysCmdGetObjectState,acForm,"Search")=acObjStateOpen Then
    'if we go here we know that search form is open so retrieve ID
    ID = Forms!Search!Whateverfield
    DoCmd.Close, acForm, "Search"
     ' if we go here the form was already closed by the user without specifying anything...
End IF
Rey Obrero (Capricorn1)Connect With a Mentor Commented:
to halt the codes after opening a form, open the form in dialog or modal mode

docmd.openform "formx",,,,, acdialog

the code will halt and continue when you close the form formx
lee_jdAuthor Commented:
Search form
Rey Obrero (Capricorn1)Commented:
just wondering, how you can run codes from anywhere, when a form was open in dialog mode.
if the dialog form sets its own visible propery to false it is no longer 'dialog' and the calling code will resume, at whch point it can retrieve stuff from the hidden form
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.