Link to home
Start Free TrialLog in
Avatar of progismaps
progismapsFlag for United States of America

asked on

Access 2013 - assign the value of an unbounded object to a table value

Is there a way to take the value of an unbounded text box and assign it the value in a table?
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I think you might need a recordset to save the value to a table.  Also, you need to have some way of identifying which record to update.  This code assumes that the ID is selected from a combo box and the value to assign to the table from a textbox:

Private Sub cmdSaveCity_Click()
'Created by Helen Feddema 27-Feb-2017
'Last modified by Helen Feddema 27-Feb-2017

On Error GoTo ErrorHandler


   Dim strCity As String
   Dim lngID As Long
   Dim rst As DAO.Recordset
   Dim strSearch As String
   
   strCity = Nz(Me![txtCity].Value)
   
   If strCity = "" Then
      Me![txtCity].SetFocus
      GoTo ErrorHandlerExit
   Else
      lngID = Nz(Me![cboID].Value)
      If lngID = 0 Then
         Me![cboID].SetFocus
         Me![cboID].Dropdown
         GoTo ErrorHandlerExit
      End If
   End If
   
   Set rst = CurrentDb.OpenRecordset("tblContacts", dbOpenDynaset)
   strSearch = "[ContactID] = " & lngID
   rst.FindFirst strSearch
   If rst.NoMatch = False Then
      rst.Edit
      rst![City] = strCity
      rst.Update
      rst.Close
   End If
   
ErrorHandlerExit:
   Exit Sub

ErrorHandler:
   MsgBox "Error No: " & Err.Number _
      & " in " & Me.ActiveControl.Name & " procedure; " _
      & "Description: " & Err.Description
   Resume ErrorHandlerExit

End Sub

Open in new window

Typically, Access forms are bound and therefore you would not need extra code to save data.  Why is the form unbound if you want to save the data?
Pat has a good question.  One possible reason to have an unbound form is that you need to do extensive error-trapping before saving or updating a record.  Is that the case here?
Unbound forms are used for capturing parameters to run reports or for entering criteria for a search form.  In rare situations, you might add unbound controls to a main form to use to insert rows to a bound subform that is not updateable or which you don't want the user to update directly.  As long as you understand the Access event model, bound forms can be made to save or not save based on your validation rules.  Unbound forms are sometimes used when the BE is SQL Server (or other relational database) to which you connect remotely over a WAN.  Access can in some cases be sluggish and using an unbound form can sometimes help.
Avatar of progismaps

ASKER

I was having issue where users were partially filling in a form that had bounded objects.  I then decided to change objects from bounded to unbounded, but that presented an issue with assigning a value from an unbounded object to table entry.  Finally, abandoned that idea and just wrote VBA code for "Before Update" that checked that certain mandatory fields were filled in and then changed the save to check for Me.Dirty before saving.
That was the correct solution.  It is amazing the control you have once you understand the form's event model well enough to use the events for their intended purpose.

As I said, there are valid reasons for using unbound forms but controlling when and what to save isn't one of them.  Using the correct events for validation will give you all the control you need.
That was the correct solution.  It is amazing the control you have once you understand the form's event model well enough to use the events for their intended purpose.
I take it back.  Based on the answer you selected, you are not using the correct form events that would allow Access to control the process.  You are still doing it manually which is certainly your choice but a lot more work and not the "Access way".