Solved

Update records through a subform

Posted on 2016-11-04
5
18 Views
Last Modified: 2016-11-23
I have a subform whose record source is a table (MyTable). The subform’s data is displayed as continuous forms.

In the detail section of the subform I have a command button. To the right of the button are textboxes which are used to display values from the various fields in MyTable.

So, when the subform is in Form View, the records from MyTable are displayed (one row for each record) and there is a command button next to each record

What I’m trying to accomplish is to run an update query that will update MyTable but only update the record next to the particular command button I click.  Right now, it seems to be updating ALL records in MyTable.  

The update I’m trying to perform is to just set at YES/NO field (Field1) from No to Yes but only for the selected record.
Here is the update query as it is now (that is affecting the entire table, not just the target record):

UPDATE MyTable SET Field1 = -1;

I also tried this but it doesn't seem to be working either (ID is the primary key):

UPDATE MyTable SET Field1 = -1
WHERE MyTable.ID=Forms!Form1!Subform1!txt_ID;
0
Comment
Question by:dbfromnewjersey
  • 3
  • 2
5 Comments
 
LVL 49

Accepted Solution

by:
Gustav Brock earned 500 total points (awarded by participants)
ID: 41874773
You are making it way too hard for yourself.

Just update the form from the click event of the button:

    Me!YourField.Value = True

No query, no updating, done.

/gustav
0
 

Author Comment

by:dbfromnewjersey
ID: 41874808
Thanks but that's now producing a 'Return Without GoSub' error message.

Here is the code under the subform's command button on click event:

Private Sub cmd_Select_Click()
On Error GoTo Err_cmd_Select_Click

'DoCmd.OpenQuery "qry_Append_Temporary_Table" <---- used to populate a different subform. Tried  commenting out to get rid of the error but no success.

Me!Transaction_Entered_Marker.Value = True

Me.Parent.frm_Subform2.Form.Requery

Exit_cmd_Select_Click:
    Exit Sub

Err_cmd_Select_Click:
    MsgBox Err.Description
    Resume Exit_cmd_Select_Click
   
End Sub
0
 

Author Comment

by:dbfromnewjersey
ID: 41874852
I must be doing something wrong because that method doesn't update the selected record.

As an example, I have 2 records in my table. Therefore, the subform displays 2 records with a command button next to each. If I click the command button next to the first record, nothing happens.  If I click the second record, the table does get updated.
0
 
LVL 49

Assisted Solution

by:Gustav Brock
Gustav Brock earned 500 total points (awarded by participants)
ID: 41874863
No query, no updating, done.

Private Sub cmd_Select_Click()

    Me!Transaction_Entered_Marker.Value = True

    ' And if you wish the record saved at once:
    Me.Dirty = False

End Sub 

Open in new window

/gustav
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41898879
Solved.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now