?
Solved

Saving and updating Records unbound form

Posted on 2014-08-11
9
Medium Priority
?
197 Views
Last Modified: 2014-11-12
Hi,

I have a list box on a form.  When a user clicks on a item in the listbox, three fields on my main form display data from the table associated with it.  See below example (i'm not explaining it very well - sorry):

Dim db As Database
Dim rst As DAO.Recordset
Dim SQL As String


Set db = CurrentDb()

   SQL = "SELECT * FROM qry_terms WHERE id=" & Me.lbTerms.Column(3) & "; "

   Set rst = db.OpenRecordset(SQL)

   If rst.EOF = False Then
      Me.txtService = rst("service_heading")
      Me.txtUnit = rst("unit")
      Me.txtPrice = rst("price")
      
   Else
     
   End If

   rst.Close
   Set rst = Nothing
End Sub

Open in new window


This allows the user to quickly update two fields associated with it without opening another form etc.  My question is how do I manage the updates when a user changes or adds data in the three fields?

Thanks
0
Comment
Question by:anthonytr
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40252857
Reverse it:
rst.Edit
rst("service_heading") = Me.txtService
rst("unit") = Me.txtUnit
rst("price") = Me.txtPrice
rst.Update

Open in new window


btw, is there a reason you're not using bound controls?
0
 

Author Comment

by:anthonytr
ID: 40252907
Thanks.

The form is already bound, but to another table.  The list box on the form displays data from a different table and I want the user to be able to click on an item form the list box and edit it.  Perhaps I could use a subform, but I thought this was neater.

Would the subform method be easier and safer?
0
 
LVL 28

Expert Comment

by:MacroShadow
ID: 40252946
Why not bind the form to both tables (using a query)? Of course that would only be possible it the database was designed properly.

Personally I stay away from subforms unless they are absolutely necessary, in design mode they are difficult to deal with especially if the subform is large.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 85
ID: 40253192
You might also try an UPDATE call:

Currentdb.Execute "UPDATE QuerySourceTable SET Service_Heading='" &  Me.txtService & "', Unit='" &  Me.txtUnit & "', Price=" & Me.txtPrice & " WHERE ID=" & Me.lblTerms.Column(3)
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 40253205
In some cases it would makes sense to use a query to join to the second table and bind the form to the query.  That would allow you to update both tables from the same form.  I don't like to do this for lookup tables because it becomes too easy for the user to accidentally change common data.

So, based on your description, I think it would make more sense to use a subform.  Subforms are not difficult to deal with although as MacroShadow mentioned, they can sometimes be annoying to update in situ.  In that case, simply open them alone so you are not constrained by the main form.  You can set the properties of the subform control to make the subform aspect invisible if that makes sense with the esthetic of the form.  If you want the separateness of the data to stand out, then by all means, leave the box and scroll bars visible in the subform.  You also need to decide if you want to allow adds and deletes as well and set the properties appropriately.
0
 

Author Comment

by:anthonytr
ID: 40263571
I can't create a query to bind the form to it as the tables are joined in a (one to many) relationship.  I will look more into using:

rst.Edit
rst("service_heading") = Me.txtService
rst("unit") = Me.txtUnit
rst("price") = Me.txtPrice
rst.Update

Open in new window



to allow the user to update/save changes to the record.  I need to look at how I check for changes so that I can invoke the update method when changes have been made, or allow the user to disregard the changes/do nothing if changes haven't been made.
0
 
LVL 40

Expert Comment

by:PatHartman
ID: 40263682
Did you try the subform technique?  This is especially relevant since you have now told us that your main form is bound to the 1-side table and the record you want to update is on the many-side of the relationship.  It only makes sense to include the additional table in the join if you are including a "lookup" table which is the 1-side of the relationship.
0
 

Author Comment

by:anthonytr
ID: 40263706
I have looked at the sub form method, however, the listbox which the user clicks is on the main form and not the sub form.
0
 
LVL 40

Accepted Solution

by:
PatHartman earned 2000 total points
ID: 40263928
Have the query of the new subform reference the listbox in its Where clause.  In the Click event of the listbox, Requery the new subform.  That will sync them.  Access will take care of the rest.
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Loops Section Overview

862 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