Results 1 to 8 of 8
  1. #1
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284

    Help with Query Criteria

    Hello...

    I am trying to run an update query based on a selected record in a ListBox. However I am having difficulty getting Access to like what I am trying to put into the Criteria field in the design mode

    This is what I am trying to get to work.


    Code:
    [Forms]![frmTempTimeTest]![LstTempTimeView].Column(0)
    What I get back is the error: Undefined function'[Forms]![frmTempTimeTest]![LstTempTimeView].Column' in expression

    I have reviewed other snipets of code and it looks similar to this but obviously I am doing something wrong..

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Can't reference Column(x) in query. It's not needed in this case anyway since you just want the value of the control. Just remove it.
    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
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Okay, but without the column reference will the query know I want to perform the update on the selected record in the ListBox?

    Thanks June...

  4. #4
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Below is the SQL statement without the column references... I do not get errors but I also do not get results.

    As I'm sure you will see I am trying to update the HoursWorked field based on the employee's ID and the date of the entry

    I'm not getting any errors so I 'think' I'm close...but I'm still the newbie...

    Code:
    StrSQL = "UPDATE tblDailyHoursWorked SET tblDailyHoursWorked.HoursWorked = [Forms]![frmTempTimeTest]![Text6] " & _
              "WHERE (((tblDailyHoursWorked.DB_ID)=[Forms]![frmTempTimeTest]![LstTempTimeView]) AND ((tblDailyHoursWorked.Entered)=[Forms]![frmTempTimeTest]![LstTempTimeView]));"

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    Concatenate the variable references. Put a space in front of WHERE so string does not run together.

    StrSQL = "UPDATE tblDailyHoursWorked SET tblDailyHoursWorked.HoursWorked = " & [Forms]![frmTempTimeTest]![Text6] & _
    " WHERE tblDailyHoursWorked.DB_ID=" & [Forms]![frmTempTimeTest]![LstTempTimeView] & " AND tblDailyHoursWorked.Entered=" & [Forms]![frmTempTimeTest]![LstTempTimeView]

    If code is behind frmTempTimeTest can use Me instead.

    StrSQL = "UPDATE tblDailyHoursWorked SET tblDailyHoursWorked.HoursWorked = " & Me.Text6 & _
    " WHERE tblDailyHoursWorked.DB_ID=" & Me.LstTempTimeView & " AND tblDailyHoursWorked.Entered=" & Me.LstTempTimeView

    However, it makes no sense for two fields to reference the same listbox value as criteria. Why would you need to use Entered as criteria when you have the DB_ID criteria?
    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.

  6. #6
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Hey June ~

    Always appreciate your help...

    The reason for needing two references (ID & Entered (which is short for date entered)) is because the query results will almost always populate the ListBox with more than one record for the same DB_ID (which is an employee's database ID as opposed to a unique record ID).
    Thus the need for a second identifier to be sure the correct record is being edited. This is the reason I was trying to incorporate the Column() reference in the criteria so the Update would know I want to change the hours for this DB_ID on this Entered (date) from... to...
    I hope that makes sense, but if that still doesn't make sense and you can offer a better way to accomplish this... I'm always open to learning...

    As for the code, it is still not pulling any results. When I view the InteliSense it is showing the same variable for both criteria which is the DB_ID. In my mind, when I hover over the "...DB_ID=" & [Forms]..." it should show the DB_ID
    But when I hover over the "...Entered=" &[Forms]..." it should show the date.

    I tried changing the Bound column... no difference.

    At the end of the day all I want to do is offer the end-user the ability to select an employee, enter a date range, run a query and populate a ListBox with the results. Then, allow the end-user to simply select a record in that ListBox and change the hours an employee input on their timecard. Seems incredibly straight forward and simple, and I've done this exact same thing in Excel but now I am wondering if this can be done in Access.

    If you (or for that matter anyone) have a better way of doing this, as always, I am open to learning.

    Thanks June

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,644
    It doesn't pull results because the criteria is looking for the same value in both fields.

    I originally thought you were using dynamic parameterized query object, not building SQL string in VBA.

    So, if the two values you want are in separate listbox (or combobox) columns, then pull them by referencing the column index. If value is in column 2, its index is 1. Since Entered is a date/time field, use # delimiters on the parameter. As long as DB_ID is a number type, no delimiters.

    StrSQL = "UPDATE tblDailyHoursWorked SET HoursWorked = " & Me.Text6 & _
    " WHERE DB_ID=" & Me.LstTempTimeView.Column(0) & " AND Entered=#" & Me.LstTempTimeView.Column(1) & "#"

    Is HoursWorked an aggregate value? Saving aggregate data is usually not necessary and not advised. Calculate when needed.
    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.

  8. #8
    RunTime91 is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Dec 2014
    Posts
    284
    Works Great, June ~ Thanks...

    No, HoursWorked is not an aggregate value, but I understand what you are saying...

    Now onto the next step of incorporating this into a stored procedure.

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

Similar Threads

  1. Replies: 2
    Last Post: 04-02-2015, 12:45 PM
  2. Replies: 1
    Last Post: 11-13-2014, 11:34 PM
  3. Replies: 4
    Last Post: 08-30-2014, 10:23 AM
  4. Replies: 5
    Last Post: 09-20-2012, 03:27 PM
  5. Replies: 5
    Last Post: 08-02-2012, 09:44 AM

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