Saving and updating Records unbound form

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
LVL 1
anthonytrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MacroShadowCommented:
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
anthonytrAuthor Commented:
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
MacroShadowCommented:
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
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
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
PatHartmanCommented:
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
anthonytrAuthor Commented:
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
PatHartmanCommented:
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
anthonytrAuthor Commented:
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
PatHartmanCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.