Solved

Update records through a subform

Posted on 2016-11-04
5
23 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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …

809 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