Need help on Command Button

SandyLY
StarLounger
Posts: 79
Joined: 10 Apr 2011, 21:28

Need help on Command Button

Post by SandyLY »

I have a main form with 7 sub-forms all working great. I added a command button on the main form to call another form (Form B) and it works to a point - meaning when I am on a record in the main form and click the cmd button it take me to the current record in Form B however when I add a new record in the main form and click the cmd button Form B is called, displaying a blank record, but the new record ID,of the main form, does not populate the ID in Form B I have to type in the ID manually - is there a way to populate the ID of the new record in Form B automatically?
Thanks Sandy

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Need help on Command Button

Post by HansV »

Could you post the code (or macro) behind the command button on the main form? And the code (or macro) in the On Open and/or On Load event of Form B, if any?
Best wishes,
Hans

Mark L
3StarLounger
Posts: 331
Joined: 11 Feb 2010, 03:55
Location: Land O Lakes, FL

Re: Need help on Command Button

Post by Mark L »

I'm guessing your problem is that you haven't yet saved the record you just entered on the mainform. Prior to the line of code that opens Form B, insert this line of code: If Me.dirty = True then Runcommand accmdSaveRecord
Mark Liquorman
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.

SandyLY
StarLounger
Posts: 79
Joined: 10 Apr 2011, 21:28

Re: Need help on Command Button

Post by SandyLY »

Hi Hans
I built the cmd button with the Button Wizard which created a Macro in the On Click Event on the Main Form -
QuotCond, Form, , ="[QuoteID]=" & [QuoteID], , Normal

Nothing on Form B

Hi Mark L, thanks for responding.
My form opens via a Macro, however, to get it to do what I want it to do I may have to go to code, but I don't know a lot of code so I was trying to keep it simple.

Thanks Sandy

SandyLY
StarLounger
Posts: 79
Joined: 10 Apr 2011, 21:28

Re: Need help on Command Button

Post by SandyLY »

I am trying to get Form B to act like a sub-form without adding it to one of my tabs - which I did at one point and it worked - but Form B holds very different data from the other tabs on the Main Form so I thought putting as a "pop up form" on the Main form would be more user friendly.

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Need help on Command Button

Post by HansV »

Try this:

Open the main form in design view.
Select the command button.
Activate the Event tab of the Property Sheet.
Select [Event Procedure] from the On Click box.
Click the builder dots ... to the right of the dropdown arrow.
Make the code look like this:

Code: Select all

Private Sub Command0_Click()
    If Me.Dirty Then
        Me.Dirty = False
    End If
    DoCmd.OpenForm FormName:="QuoteCond", WhereCondition:="QuoteID=" & Me.QuoteID
End Sub
where Command0 is the name of the command button.
Best wishes,
Hans

SandyLY
StarLounger
Posts: 79
Joined: 10 Apr 2011, 21:28

Re: Need help on Command Button

Post by SandyLY »

I entered the code on the On Click Event - EventProcedure - and it works pretty much like the Macro did - I still have to enter the QuoteID into Form B, if I don't the new record saves but not to the QuoteID on the Main form. I tried Marks suggestion in your code
Private Sub Command0_Click()
If Me.Dirty Then
Me.Dirty = False (and I tried = True)
RunCommand = accmdSaveRecord
End If
DoCmd.OpenForm FormName:="QuotCond", WhereCondition:="QuoteID=" & Me.QuoteID (I changed QuoteCond to QuotCond which is the name of my form)
End Sub

Same response everything looks good except when I try to add a new record - QuoteID on Form B has to be entered manually.

You think it would be better to add to one of my tabs as a sub-form? It does what I want it to but I just don't think it belongs there....

Thanks Sandy
(I even understood the code this time!!)

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Need help on Command Button

Post by HansV »

The line

Me.Dirty = False

saves the current record. I fear I'll have to see the database. Could you create a copy of the database, remove features that are not relevant to the problem (such as most records, and all subforms - we only need the main form and Form B), then zip it and attach it to a reply? Thanks in advance.
Best wishes,
Hans

SandyLY
StarLounger
Posts: 79
Joined: 10 Apr 2011, 21:28

Re: Need help on Command Button

Post by SandyLY »

See attached
Thanks for your help.
Sandy
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78568
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Need help on Command Button

Post by HansV »

Thanks - that helps! You can solve the problem by setting the Default Value of the QuoteID text box on QuoteCond ("Form B").

- Open QuoteCond in design view.
- Select the QuoteID text box.
- Activate the Data tab of the Property Sheet.
- Enter [Forms]![Quote]![QuoteID] in the Default Value property.

You will currently get an error message if you click the button while the quote status hasn't been filled in yet. To avoid that, change the code for the command button on the Quote form as follows:

Code: Select all

Private Sub Command41_Click()
    If IsNull(Me.cboQuoteStatus) Then
        Me.cboQuoteStatus.SetFocus
        MsgBox "Please select a quote status", vbExclamation
        Exit Sub
    End If
    If Me.Dirty Then
        Me.Dirty = False
    End If
    DoCmd.OpenForm FormName:="QuotCond", WhereCondition:="QuoteID=" & Me.QuoteID
End Sub
Best wishes,
Hans

SandyLY
StarLounger
Posts: 79
Joined: 10 Apr 2011, 21:28

Re: Need help on Command Button

Post by SandyLY »

Excellent! Does exactly what I wanted.
Thanks Sandy