Results 1 to 13 of 13
  1. #1
    Imloyd76 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    12

    Code to use a Command Button to update a field in a record.

    I have a form with a "Find" command button that pulls the information from a record in my database. One of those fields is a "Location" field. I have a command button on the form that I want to "Update" that "Location" field. So for example if I use my find button to pull a record and that items "Location" states that it's in row 45...I want to change the text in the box to whatever the new location is and hit the "Update Record" button and it only update that "Location" field in that record. Thanks in advance for any help.

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Where does the location value come from?

    As long as that record has focus, maybe simple as: Me!Location = value
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Imloyd76 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by June7 View Post
    Where does the location value come from?

    As long as that record has focus, maybe simple as: Me!Location = value


    I was hoping that would work, however, when I use that it clears out the value in that field completely rather than updating it with the new information.

  4. #4
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Show your code. How is the value derived?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Imloyd76 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by June7 View Post
    Show your code. How is the value derived?
    Here's my code:

    Option Compare Database

    Private Sub cmdUpdateLocation_Click()
    Me!Location = Value

    End Sub
    Private Sub cmdFindBox_Click()
    Form.SetFocus
    Form.Filter = "BoxID=" & "'" & Text25 & "'"
    Form.FilterOn = True
    If Location = "Shipped" Then
    MsgBox ("This Item has already been Shipped")
    Form.Filter = "BoxID='0'"
    Form.FilterOn = True
    Text25.SetFocus
    Else: Location.SetFocus
    End If

    End Sub


    Private Sub Form_Load()
    Form.Filter = "BoxID='0'"
    Form.FilterOn = True
    End Sub

  6. #6
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    Nothing in that code actually changes value of Location field.

    Include code to modify Location field value. Step debug.

    If BoxID is a number datatype field, don't use apostrophe delimiters in the Filter expression.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    Imloyd76 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by June7 View Post
    Nothing in that code actually changes value of Location field.

    Include code to modify Location field value. Step debug.

    If BoxID is a number datatype field, don't use apostrophe delimiters in the Filter expression.
    The Filter expression works fine as the BOXID's are Alpha Numeric. I'm only having problems with the Update of the Location field.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,898
    There is no code in that procedure that modifies the Location field. Perhaps you need something like:

    Else
    Me.Location.SetFocus
    Me.Location = "some value here"
    End If
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  9. #9
    Imloyd76 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by June7 View Post
    There is no code in that procedure that modifies the Location field. Perhaps you need something like:

    Else
    Me.Location.SetFocus
    Me.Location = "some value here"
    End If
    You've been very helpfull. I'm still running into problems though. I want the value of "Location" to change if I hit the cmdUpdate button. And more importantly I need it to update with whatever I type in that "Location" field on the form. So far all it's doing is removing the contents of that field instead of replacing the contents with the new data.

  10. #10
    R_Badger is offline Knows a few tricks
    Windows XP Access 2003
    Join Date
    Feb 2012
    Location
    Suffolk, UK
    Posts
    262
    To clarify:

    You have a form, which utilises your code to filter to the required record.

    You then want to change that records location field, by inputting new data into it, and then hitting a command button.

    I have a question; Can you currently type into the Location field?

    It may be the property is set to locked, in which case, you don't require a command button necessarily, you just need to open the form in design view, right click the control, click properties, then click data, locked=no.

    If it wont allow you to alter the data, there may be data validation applied to the field by the form control.

    If neither of these help, it would be helpful if you cost attach a copy of your database. (Strip out any data you wouldn't write on the back of a postcard) and instead insert some dummy data so we have an idea of what we are looking at, as without looking at your form with this, we are a little blind.

    If you can't post it, I have a couple of other thoughts

  11. #11
    Imloyd76 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    12
    Quote Originally Posted by R_Badger View Post
    To clarify:

    You have a form, which utilises your code to filter to the required record.

    You then want to change that records location field, by inputting new data into it, and then hitting a command button.

    I have a question; Can you currently type into the Location field?

    It may be the property is set to locked, in which case, you don't require a command button necessarily, you just need to open the form in design view, right click the control, click properties, then click data, locked=no.

    If it wont allow you to alter the data, there may be data validation applied to the field by the form control.

    If neither of these help, it would be helpful if you cost attach a copy of your database. (Strip out any data you wouldn't write on the back of a postcard) and instead insert some dummy data so we have an idea of what we are looking at, as without looking at your form with this, we are a little blind.

    If you can't post it, I have a couple of other thoughts
    You are exactly correct in your description of my needs. As far as the "Location" field goes that is not locked. Everything else that populates the filter is locked but as I am using this form to only change the location field it is not. I will try and attach a copy of my database. Thanks again for the help.

  12. #12
    R_Badger is offline Knows a few tricks
    Windows XP Access 2003
    Join Date
    Feb 2012
    Location
    Suffolk, UK
    Posts
    262
    If the field isnt locked then it should be a simple case of changing the field, if that's not working you can try adding a commandbutton with the onclick event with a simple entry:
    Code:
    Private Sub Command0_Click()
    RunCommand acCmdSaveRecord
    End Sub
    If I recall correctly then that should work

  13. #13
    Imloyd76 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Mar 2012
    Posts
    12
    That's exactly what I needed....Thank you so much.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 3
    Last Post: 12-17-2011, 02:32 PM
  2. Replies: 4
    Last Post: 07-22-2011, 12:52 PM
  3. Command button code to clear form
    By windwardmi in forum Forms
    Replies: 15
    Last Post: 11-21-2010, 03:21 PM
  4. Form/report command button code
    By max3 in forum Forms
    Replies: 1
    Last Post: 08-26-2009, 02:18 AM
  5. Command button code
    By lfolger in forum Forms
    Replies: 3
    Last Post: 03-25-2008, 04:26 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums