Update Query in VBA in Access

I'm trying to run an update query for all the values from my multiselect listbox but it doesn't seem to be working correctly. Can anyone tell me if I have the syntax wrong with my code?

  'Add selected value(s) to table
  Set ctl = Me.WCList
    For Each varItem In ctl.ItemsSelected
    strSQL = "UPDATE tblBusinessHoursTemp SET tblBusinessHoursTemp.EndDate =" & Me.cboEndingWeek & _
          " WHERE tblBusinessHoursTemp.ShiftID =" & varItem
    DoCmd.RunSQL strSQL
    Next varItem

Open in new window

LVL 1
Lawrence SalvucciInformation Technology ManagerAsked:
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.

Jeffrey CoachmanMIS LiasonCommented:
1. To be sure, add this:
   Dim varItem as Variant

2. Always "view" the SQL before you execute it, ...this is so you can see the SQL and visually check for correct syntax:
    msgbox strSQL

3. Use Currentdb.Execute instead of Docmd.RunSQL
    CurrentDB.Execute strSQL, dbfailonerror

Im sure an expert well versed in SQL can spot the syntax error...

JeffCoachman
0
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
I already have all that in my code. I only cut the section I thought I would need to post here. Here is the full code:

  Dim strSQL        As String
  Dim db            As DAO.Database
  Dim rs            As DAO.Recordset
  Dim ctl           As Control
  Dim varItem       As Variant

  On Error GoTo ErrorHandler

  Set db = CurrentDb()
  Set rs = db.OpenRecordset("tblBusinessHoursTemp", dbOpenDynaset, dbAppendOnly)

  'make sure a selection has been made
  If Me.WCList.ItemsSelected.Count = 0 Then
    MsgBox "Must select at least 1 Employee"
    Exit Sub
  End If

  'Add selected value(s) to table
  Set ctl = Me.WCList
    For Each varItem In ctl.ItemsSelected
    strSQL = "UPDATE tblBusinessHoursTemp SET tblBusinessHoursTemp.EndDate =" & Me.cboEndingWeek & _
          " WHERE tblBusinessHoursTemp.ShiftID =" & varItem
    DoCmd.RunSQL strSQL
    Next varItem

ExitHandler:
  Set rs = Nothing
  Set db = Nothing
  Exit Sub

ErrorHandler:
  Select Case err
    Case Else
      MsgBox err.Description
      DoCmd.Hourglass False
      Resume ExitHandler
  End Select

Open in new window

0
Jeffrey CoachmanMIS LiasonCommented:
Can you clearly explain what:
     "doesn't seem to be working correctly"
...means?

Tell us exactly what is, ...or is not happening when you run the code...

FWIW, this works fine for me:

Dim ctl As Control
Dim varItem As Variant
Dim strSQL As String
 Set ctl = Me.WCList
    For Each varItem In ctl.ItemsSelected
    strSQL = "UPDATE tblBusinessHoursTemp SET tblBusinessHoursTemp.EndDate =" & Me.cboEndingweek & _
          " WHERE tblBusinessHoursTemp.ShiftID =" & varItem
    DoCmd.RunSQL strSQL
    Next varItem
0
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

Lawrence SalvucciInformation Technology ManagerAuthor Commented:
When I execute the code it's not updating the records in the tblBusinessHoursTemp with the date from the "Me.cboEndingWeek" control.

I select the employees from my WCList and then enter a date in the cboEndingWeek control and click the command button and then I look in the table and there was nothing updated.
0
Lawrence SalvucciInformation Technology ManagerAuthor Commented:
ok it's working now. I was pulling the wrong value into my varItem variant. But now the problem is this....I select a date of 01/14/13 and it updates the table with 12/30/1899. Why would it do that?
0
Jeffrey CoachmanMIS LiasonCommented:
Chang your SQL to this:

    strSQL = "UPDATE tblBusinessHoursTemp SET tblBusinessHoursTemp.EndDate =" & "#" & Me.cboEndingweek & "#" & _
          " WHERE tblBusinessHoursTemp.ShiftID =" & varItem
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
Jeffrey CoachmanMIS LiasonCommented:
;-)
0
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.