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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lock Moadal/Popup form to mainform 1

Status
Not open for further replies.

ZOR

Technical User
Jan 30, 2002
2,963
GB
I have a mainform with a button to open a popup/modal form. Both recordsorces are on the same tables, however if my base form is set to a record, how do I make the popup/modal forms ID1 the same. ID1 is on both forms, being the primary key of the same table. Thanks
 
How are ya ZOR . . .

What ever code used will first need to know if the popup is open. If you don't already have your own, the following function can be used. Put it in a module in the modules window:
Code:
[blue]Function IsOpenForm(frmName As String) As Boolean
   Dim cp As CurrentProject, Frms As Object
   
   Set cp = CurrentProject()
   Set Frms = cp.AllForms
   
   If Frms.Item(frmName).IsLoaded Then
      If Forms(frmName).CurrentView > 0 Then IsOpenForm = True
   End If
   
   Set Frms = Nothing
   Set cp = Nothing

End Function[/blue]
The in the [blue]On Current[/blue] event of the mainform you'll have:
Code:
[blue]   Dim frm As Form, Cri As String
   
   If IsOpenForm("[purple][b]PopupFormName[/b][/purple]") Then
      Set frm = Forms![[purple][b]PopupFormName[/b][/purple]]
      
      Cri = "[ID1] = " & Me!ID1
      frm.Recordset.FindFirst Cri
      
      Set frm = Nothing
   End If[/blue]
The code assumes ID1 is [blue]numeric[/blue]. I believe you know what to do if ID1 is text.

[blue]Thats it! . . .[/blue]

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Perfect, thanks a lot. Regards
 
Oh dear, does not work for me. It looked okay, however the popup form named DocsForm gets opened after the mainform named MainForm1 is set to a selected record. I tried putting the code in the onopen event of the popup but that didn't work either. I did have the code below on a button on the mainform, it seemed to work until I used Me.Requery on the popup form and it lost its ID1 Value.

Forms!DocsForm.Form.ID1 = Forms!MainForm1.Form.ID1

I also put it in again after the Me.Requery on the popup form but I was not sure if that was reliable.

Regards

 
ZOR . . .

I fully tested before posting . . . no problemo.

post your code for opening the popup and the event used . . .

Also post the code as you have it . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Many thanks.

The code for the module was pasted in okay.

The code for the button which opens the popup form is:

Dim stDocName As String
Dim stLinkCriteria As String
stDocName = "DocsForm"
DoCmd.OpenForm stDocName, , , stLinkCriteria

The code for the Mainforms oncurrent event is:

Private Sub Form_Current()
Dim frm As Form, Cri As String

If IsOpenForm("DocsForm") Then
Set frm = Forms![DocsForm]

Cri = "[ID1] = " & Me!ID1
frm.Recordset.FindFirst Cri

Set frm = Nothing
End If

End Sub


Am I right in thinking If IsOpenForm("DocsForm") Then, means if the popup for is open then do something. If so how does the event get fired?

Thanks again

 
Just read an old post, used the wizard which for once did something I wanted.

thread702-1345256

However thanks again for help. Regards
 
Howdy ZOR . . .

Sorry to get back so late!

ZOR said:
[blue]Am I right in thinking If IsOpenForm("DocsForm") Then, means if the popup for is open then do something. [purple]If so how does the event get fired?[purple][/blue]

The forms [blue]On Current[/blue] event triggers when you set focus to a different record other than the current. Wether by mouse or keyboard.

The event also occurs (by default) when you first open the form. However the popup isn't open at this time. To you this means when you 1st open the popup it isn't synchronized. Perhaps this is what leads you to believe it isn't working.

I'll work up a little code to get it working on open. In the meantime, open the popup and select a different record on the mainform and let me know if the popup synchronizes . . .

Calvin.gif
See Ya! . . . . . .

Be sure to see thread181-473997
 
Don't worry about the time getting back, only too greatful for help. The wizard produced this:

Dim stDocName As String
Dim stLinkCriteria As String

stDocName = "DocsForm"

stLinkCriteria = "[ID1]=" & Me![ID1]
DoCmd.OpenForm stDocName, , , stLinkCriteria

When the popup opens, if there is no record in the related table, the foreign key is a zero. That changes when I add a record, it fills in with the correct foreign key value. If I open up the popup when there is a related record/s, the foreign key ID1 is correct.

I guess therefore its how it should be?, eems to work.

Thanks again
 
Yup, that should work fine. Just make sure the pop-up is opened as dialog, and add a line to refresh the main form (since the operate on the same table)

You will also need to add a refresh before the form open, as again, if you edited the main form first, you may get a record lock on the popup, depending on the record locking options.


me.refresh
DoCmd.OpenForm stDocName, , , stLinkCriteria, acdialog
me.refresh
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top