Results 1 to 8 of 8
  1. #1
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123

    Update Query With SubQuery

    I am just trying to get the hang of writing SQL statments



    below is an SQL Statement I am trying to work on in VBA but no luck

    Code:
    Dim dbs As Database
    Dim varItm As Variant
    Set dbs = CurrentDb
    Dim sSQL As String
    
    sSQL = "Update tblEmpDetails " & _
    "Set InVisible = True " & _
    "WHERE (SELECT ed.IsVisible From tblEmpDetails ed " & _
    "JOIN tblPickExemptions pe " & _
    "ON ed.PermFctn = pe.PermFctn " & _
    "WHERE ed.PermFctn = pe.PermFctn); "
    
    dbs.Execute ("sSQL ")
    It doesnt have to be in a string but I am trying different ways just to get the hang of it

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    Use Access query builder to help get the correct sql. Then copy/paste from SQL View to VBA.

    Try:

    "UPDATE tblEmpDetails " & _
    "JOIN tblPickExemptions " & _
    "ON tblEmpDetails.PermFctn = tblPickExemptions.PermFctn " & _
    "SET InVisible = True " & _
    "WHERE tblEmpDetails.PermFctn = tblPickExemptions.PermFctn
    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
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    your code as follows

    Dim dbs As Database
    Dim varItm As Variant
    Set dbs = CurrentDb
    Dim sSQL As String
    sSQL = "Update tblEmpDetails " & _
    "JOIN tblPickExemptions " & _
    "ON tblEmpDetails.PermFctn = tblPickExemptions.PermFctn " & _
    "Set InVisible = True " & _
    "WHERE tblEmpDetails.IsVisible " & _
    dbs.Execute ("sSQL ")

    This is the error msg Click image for larger version. 

Name:	debug.png 
Views:	8 
Size:	18.1 KB 
ID:	14807

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    Do not enclose variables in " marks.

    dbs.Execute sSQL
    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
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    I am Now getting this error with my SQL

    Click image for larger version. 

Name:	debug.png 
Views:	6 
Size:	35.0 KB 
ID:	14810

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    Ooops. Probably need INNER JOIN.

    Why don't you build and test the sql with an Access query object? Use a copy of the tables until you are sure of the results.
    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
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    I am not sure how to make an Update Query with the query builder

    because there is no "FROM"

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,965
    Open query builder, select tables, join them, click UPDATE from ribbon Design tab, drag fields to the grid.

    Or copy/paste the suggested sql into query builder SQL View. Edit it there to remove all the VBA stuff (add the INNER keyword) then switch to Design View to see the structure there.
    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.

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

Similar Threads

  1. Replies: 11
    Last Post: 11-08-2013, 06:14 PM
  2. Query with maybe subquery
    By AndycompanyZ in forum Queries
    Replies: 4
    Last Post: 06-28-2011, 11:45 AM
  3. Query and subquery wont work with combo box
    By jbg8931 in forum Queries
    Replies: 0
    Last Post: 05-09-2010, 10:24 AM
  4. Update from Subquery
    By JoshS in forum Queries
    Replies: 0
    Last Post: 04-28-2010, 07:45 AM
  5. Replies: 2
    Last Post: 05-27-2009, 08:47 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