Results 1 to 9 of 9
  1. #1
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496

    Clearing a field with conditions

    I have a form which displays areas along with a checkbox for each area



    Click image for larger version. 

Name:	Capture.JPG 
Views:	13 
Size:	77.1 KB 
ID:	14293

    I have a dlookup that lists the number of schools that fall in that area that have the fields filled.

    I would like to clear the date field in the table for the schools, for those within that area.

    I don't have an idea on how to do this. Any suggestions and I don't want to clear date fields for all the schools, just the ones in the area (linked tables)

    The concept is that you check an area, click the clear dates button and it clears the date field for each school within that area.

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Use WHERE clause in an UPDATE sql action.
    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
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    so UPDATE tblAreas INNER JOIN tblSchools ON tblAreas.AreasID = tblSchools.AreaID SET tblSchools.CatalogueLabelPrinted = NullWHERE (((tblAreas.CheckToClear)=True));

    but in vba?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    I don't see a date field in that image. Which table has the date field that you want to clear?
    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
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by June7 View Post
    I don't see a date field in that image. Which table has the date field that you want to clear?
    CatalogueLabelPrinted

    That is the date field

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Does the UPDATE query work? What happens?

    You probably also want to run another query to set the checkboxes all back to unchecked and refresh the form.
    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
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Quote Originally Posted by June7 View Post
    Does the UPDATE query work? What happens?

    You probably also want to run another query to set the checkboxes all back to unchecked and refresh the form.
    I'm fairly certain the update query will work - I've never attempted it through vba (partly because if I get it wrong). I will try it on an "offline" copy of the database of course before I attempt it just because I don't want to while any valuable data.

    So on click of button, uncheck after the vba for the sql is done? I didn't think of that (if the user is lazy and doesn't uncheck the area on the form).

    I'll post a result soon (it's 5 am here I just happened to be awake).

  8. #8
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    I used

    Code:
    Dim strSql As StringstrSql = "UPDATE tblAreas INNER JOIN tblSchools ON tblAreas.AreasID = tblSchools.AreaID SET tblSchools.CatalogueLabelPrinted = Null " & vbCrLf & _
    "WHERE (((tblAreas.CheckToClear)=True));"
    
    
       Const cstrPrompt As String = _
            "Are you sure you want clear the records? Yes/No"
        If MsgBox(cstrPrompt, vbQuestion + vbYesNo) = vbNo Then
            Cancel = True
            ElseIf vbYes Then
          DoCmd.RunSQL strSql
           Me.Refresh
        End If
    I even put a refresh when you click the checkboxes.

    It works for the first time however press it again and it asks for the wrong numbers to clear. Instead of say 20 it will clear 40.

    I think it is doing all schools in area and not by whether catalogue date is clear (not that it matters as they all need to be cleared anyway but I like to have less confusion for the user.)

  9. #9
    Ruegen's Avatar
    Ruegen is offline VIP
    Windows 7 64bit Access 2010 64bit
    Join Date
    Jul 2013
    Location
    Australia
    Posts
    1,496
    Turns out the boss wanted a different date field - no matter - works for that too

    ended up with

    Code:
    Private Sub Command63_Click()Dim strSql As String
    Dim strSqlCheck As String
    strSql = "UPDATE tblAreas INNER JOIN tblSchools ON tblAreas.AreasID = tblSchools.AreaID SET tblSchools.CallBackDate = Null " & vbCrLf & _
    "WHERE (((tblAreas.CheckToClear)=True));"
    
    
    strSqlCheck = "UPDATE tblAreas SET tblAreas.CheckToClear = False " & vbCrLf & _
    "WHERE (((tblAreas.CheckToClear)=True));"
    
    
       Const cstrPrompt As String = _
            "Are you sure you want clear the records? Yes/No"
        If MsgBox(cstrPrompt, vbQuestion + vbYesNo) = vbNo Then
            Cancel = True
            ElseIf vbYes Then
          DoCmd.RunSQL strSql
          DoCmd.RunSQL strSqlCheck
           Me.Refresh
        End If
    End Sub
    thanks for the help!

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

Similar Threads

  1. Replies: 7
    Last Post: 07-07-2013, 11:11 AM
  2. Caculated field with if then conditions
    By tmaleshafske in forum Access
    Replies: 2
    Last Post: 09-11-2012, 01:26 PM
  3. Clearing a Date field
    By chris.williams in forum Programming
    Replies: 8
    Last Post: 08-09-2012, 06:04 PM
  4. Replies: 3
    Last Post: 08-01-2012, 10:56 AM
  5. Query result based upon two field conditions
    By diane802 in forum Access
    Replies: 35
    Last Post: 01-08-2010, 06:31 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