Results 1 to 8 of 8
  1. #1
    billk is offline Novice
    Windows 7 Access 2003
    Join Date
    Jun 2010
    Posts
    5

    VBA Update Table Field Based on Multi-Select Listbox

    I've been struggling with this for a bit now. I'm trying to create a Freeday program for our HR department. I have the code figured out how to use a multi-Select List box to add new earned freedays to the tblFreedays. However I need to also update the earned hours based on the multi-select listbox. I attached the database to this posting. Below the list box is an add button that I would like to have it update the tblFreedays date earned(which is working) and also increment the daysearned and daysavailable field in the tblEmp(this isn't working) I added a cmdbutton for Update Hours to practice writing a code to make this happen. Any help would greatly be accepted.

    Thanks in advanced

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Haven't looked at the sample, but within the standard multiselect loop you can run an update query:

    CurrentDb.Execute "UPDATE TableName SET FieldName = 123 WHERE KeyField = " & Me.ListboxName.ItemData(varItem)

    Presumably you'd want to concatenate in some value as well.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    billk is offline Novice
    Windows 7 Access 2003
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by pbaldy View Post
    Haven't looked at the sample, but within the standard multiselect loop you can run an update query:

    CurrentDb.Execute "UPDATE TableName SET FieldName = 123 WHERE KeyField = " & Me.ListboxName.ItemData(varItem)

    Presumably you'd want to concatenate in some value as well.
    Thanks, I got it figured out. I attached a new working rev of the database.

    Regards,

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Glad you got it working. Welcome to the site by the way! A lot of people are reluctant to download attachments, and a lot can't due to work restrictions, so it might be helpful to them if you post the relevant code.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  5. #5
    billk is offline Novice
    Windows 7 Access 2003
    Join Date
    Jun 2010
    Posts
    5
    Quote Originally Posted by pbaldy View Post
    Glad you got it working. Welcome to the site by the way! A lot of people are reluctant to download attachments, and a lot can't due to work restrictions, so it might be helpful to them if you post the relevant code.
    Private Sub cmdAdd_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim var As Variant
    Dim varAdd As Variant
    Set db = CurrentDb
    Set rs = db.OpenRecordset("tblFreeDay")

    If IsNull(Me.txtDateHolder) Then
    MsgBox "No Date selected...", vbExclamation
    Exit Sub
    End If

    For Each var In Me.lstEmployees.ItemsSelected
    With rs
    .AddNew
    .Fields("EmpName") = CLng(Me.lstEmployees.Column(0, var))
    txtDateHolder.SetFocus
    .Fields("DateEarned") = Me.txtDateHolder.Text
    .Update

    End With
    Next

    MsgBox "Dates Updated successfully...", vbInformation

    If IsNull(Me.txtDateHolder) Then
    MsgBox "No Date selected...", vbExclamation
    Exit Sub
    End If

    Set db = CurrentDb
    Set rsAdd = db.OpenRecordset("tblEmp")

    For Each varAdd In Me.lstEmployees.ItemsSelected
    db.Execute ("UPDATE tblEmp SET [DaysEarned] = [DaysEarned]+1 WHERE [tblEmp.EmpName] = '" & Me.lstEmployees.Column(1, varAdd) & "'")
    db.Execute ("UPDATE tblEmp SET [DaysAvailable] = [DaysAvailable]+1 WHERE [tblEmp.EmpName] = '" & Me.lstEmployees.Column(1, varAdd) & "'")

    Next
    Me.lstEmployees.Requery
    MsgBox "Hours Updated Successfully...", vbInformation
    Forms!frmEmp.Refresh


    rs.Close
    db.Close

    Set rs = Nothing
    Set db = Nothing

    End Sub

  6. #6
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    A couple of thoughts.

    You shouldn't need to set focus to your form control. Just drop the .Text from the reference (.Value is the default, and it's what you want anyway).

    You should be able to combine the two SQL statements:

    ...SET [DaysEarned] = [DaysEarned]+1, [DaysAvailable] = [DaysAvailable]+1 WHERE...

    As a general thought, I would probably not store the earned and available that way. It's sort of like inventory really. I'd have a transaction table where I have a record each time I earned a day, and a record when I used a day (or days). My days available is the net of the two, just as my inventory quantity on hand is the net of my purchases less sales. With your method, sooner or later somebody is going to complain that their balance is off, and you have no way to document it other than trying to go back and manually recalculate it. What if that recalculation doesn't jive with the balance in the table?
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  7. #7
    krjoshi is offline Novice
    Windows 8 Access 2010 64bit
    Join Date
    Nov 2014
    Posts
    13
    where I can find the attachment?

  8. #8
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    I believe the forum software automatically deletes attachments after a certain amount of time to save space. This thread being over 4 years old, that's likely what happened.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. Replies: 2
    Last Post: 03-14-2010, 07:27 PM
  2. Replies: 4
    Last Post: 09-03-2009, 02:01 PM
  3. Select a certain field of a record based on ID
    By cesarone82 in forum Access
    Replies: 3
    Last Post: 06-06-2009, 01:16 PM
  4. Replies: 1
    Last Post: 03-01-2009, 09:53 AM
  5. Multi-select listbox and update flag in table
    By Suresh in forum Programming
    Replies: 0
    Last Post: 12-19-2007, 01:04 AM

Tags for this Thread

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