Opening a form from within another one (1 Viewer)

XV1957

Registered User.
Local time
Today, 16:13
Joined
Aug 6, 2014
Messages
80
Hello Access experts,
I have a company form on which I put a command button that opens a purchase order form.
The idea is that the company form creates a purchase order heading and opens the form on the new purchase order record.
My problem is that the openform command ignores the where clause.
In a previous post, someone told me to open aform without a where clause, and to reopen it with the where clause. But this does not work either.
The Purchase Order form invariably opens on the first record in the Table, and no matter what I try, never on the right record.
Here is the code:
Code:
'Create the Purchase Heading record
        rstHead.AddNew
            rstHead!PUDate = Now
            rstHead!PUOrderNb = strRight
            rstHead!PUCompID = Me.ID
            rstHead!PUVat = defVat
            rstHead!PuSent = 0
        rstHead.Update
        rstHead.Bookmark = rstHead.LastModified
        MsgBox "Purchase Order Created"
        DoCmd.RunCommand acCmdSaveRecord
        strWhere = "[PUOrderNb] = " & strRight
        If CurrentProject.AllForms("frmPurchase").IsLoaded Then
            DoCmd.Close "frmPurchase"
        End If
        DoCmd.OpenForm "frmPurchase"
        DoCmd.OpenForm "frmPurchase", , strWhere
Could it be that the record created in thsi code is somehow not available when I open the Purchase Order form?
Thanks in advance for good advice.
 

Minty

AWF VIP
Local time
Today, 15:13
Joined
Jul 26, 2013
Messages
10,371
Add a Debug.Print strWhere after you set it's value and see what you get in the immediate window.
 

XV1957

Registered User.
Local time
Today, 16:13
Joined
Aug 6, 2014
Messages
80
Hi Minty,
Thanks for your quick answer.
I had already checked that the where string worked using a Msgbox.
The print debug gave the following string: "[PUOrderNb] = PO201700203.00" which for all I can see is correct.
 

Minty

AWF VIP
Local time
Today, 15:13
Joined
Jul 26, 2013
Messages
10,371
Try replacing your code with
Code:
 strWhere = "[PUOrderNb] = '" & strRight & "'"

This will put the criteria which is a string in quotes.
I'm surprised you didn't get a type mismatch error...
 

XV1957

Registered User.
Local time
Today, 16:13
Joined
Aug 6, 2014
Messages
80
Hi Minty,
Thanks again.
I tried your solution (copied it into the text so as not to make mistakes).
It does not work and I did not get any mismatch errors previously.
The immediate window shows this now, as it should:
[PUOrderNb] = 'PO201700204.00'
 

Minty

AWF VIP
Local time
Today, 15:13
Joined
Jul 26, 2013
Messages
10,371
Having checked I don't think your where clause is in the right place - add an extra comma;
Code:
 DoCmd.OpenForm "frmPurchase", , , strWhere
 

static

Registered User.
Local time
Today, 15:13
Joined
Nov 2, 2015
Messages
823
A form can only be opened once.

DoCmd.OpenForm "frmPurchase"
DoCmd.OpenForm "frmPurchase", , strWhere

The second openform will be ignored. That's why you have code in there to make sure it's not already open.
 

XV1957

Registered User.
Local time
Today, 16:13
Joined
Aug 6, 2014
Messages
80
Hi Minty,
Thanks for your insight.
I could have found out myself, I was so convinced I had let Access put the commas in place. Of course it now works. Once again you were a great help.
 

Users who are viewing this thread

Top Bottom