Results 1 to 7 of 7
  1. #1
    Bazsl is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2016
    Posts
    47

    What is wrong with this UPDATE statement?

    UPDATE Costume AS Cc SET Cc.[Photo File] = (SELECT Cp.Photo.FileName AS FileName


    FROM Costume AS Cp WHERE Cc.[Costume Number] = Cp.[Costume Number]);

    When I click Run I get a result set with a single column headed Photo File. What I am trying to do is set the ShortTest field Photo File to the value of the FileName field in the attachement field named Photo. Thanks.

  2. #2
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    Pretty sure what you are trying to do here is reference a property - although it can be displayed in a query, it can't be assigned to another field

    I suspect you will need to use vba to reference the property

    part of the problem is [photo file] is one field/one record, but you might have many attachments for that one record. You may know you only have one per record, but access can't assume that.

    In vba try something like

    Code:
    dim rst as dao.recordset
    
    set rst=currentdb.openrecordset("SELECT [Costume Number], Photo.FileName as FName FROM Costume")
    While not rst.eof
        currentdb.execute ("UPDATE Costume SET [Photo File]='" & nz(rst!FName) & "' WHERE [Costume Number]=" & rst![Costume Number])
        rst.movenext
    wend
    if there is more than one attachment, only one of them will populate the photo file field. If you have more, then to code becomes more complex.

    note having spaces in field and table names is a bad idea, recommend you lose them

  3. #3
    Bazsl is offline Advanced Beginner
    Windows 10 Access 2013 64bit
    Join Date
    Jun 2016
    Posts
    47
    Thanks Ajax. What does the nz(...) function do. I am very new to vba and have not seen that construct before.

  4. #4
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,931
    learn to google! a tip is to precede your search with 'access vba' i.e. access vba nz function

    what it does is if the first value is null, it uses the second value (which if left blank it uses either a zero length string for text or 0 for numbers depending on the first value datatype). It is a standard way of protecting against nulls - the alternative in this situation would be

    iif(isnull(rst!FName),"",rst!FName)

    I used it here because if there is no attachment, a null would be returned and the code could fail.

  5. #5
    AAAndy is offline Advanced Beginner
    Windows 7 64bit Access 2013 32bit
    Join Date
    Aug 2016
    Posts
    68
    Quote Originally Posted by Ajax View Post
    Pretty sure what you are trying to do here is reference a property - although it can be displayed in a query, it can't be assigned to another field

    I suspect you will need to use vba to reference the property

    part of the problem is [photo file] is one field/one record, but you might have many attachments for that one record. You may know you only have one per record, but access can't assume that.

    In vba try something like

    Code:
    dim rst as dao.recordset
    
    set rst=currentdb.openrecordset("SELECT [Costume Number], Photo.FileName as FName FROM Costume")
    While not rst.eof
        currentdb.execute ("UPDATE Costume SET [Photo File]='" & nz(rst!FName) & "' WHERE [Costume Number]=" & rst![Costume Number])
        rst.movenext
    wend
    if there is more than one attachment, only one of them will populate the photo file field. If you have more, then to code becomes more complex.

    note having spaces in field and table names is a bad idea, recommend you lose them




    I modified the above code to use in my project but it gives an error.
    the error is somewhere in this line
    CurrentDb.Execute ("UPDATE dbo_tbl_Custom_Subscription_Subscriptions SET [varExtensionSettingValueList]='" & Nz(rst!LStringEmail) & "' WHERE [varReportTitle]=" & rst![varReportTitle])

    Does anyone know what the problem is?


    Query is:
    Dim rst As dao.Recordset

    Set rst = CurrentDb.OpenRecordset("SELECT [varReportTitle] FROM dbo_tbl_Custom_Subscription_Subscriptions")

    While Not rst.EOF
    CurrentDb.Execute ("UPDATE dbo_tbl_Custom_Subscription_Subscriptions SET [varExtensionSettingValueList]='" & Nz(rst!LStringEmail) & "' WHERE [varReportTitle]=" & rst![varReportTitle])
    rst.MoveNext
    Wend


    Error is:
    Run-time error '3075':
    Syntax error (missing operator) in query expression '[varReportTitle]=Weekly Tuesdays 5pm'.

  6. #6
    orange's Avatar
    orange is offline Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Currentdb.execute does not need brackets around arguments.

    Create a string variable for your SQL

    eg Dim mySql as string
    mySQL = "UPDATE dbo_tbl_Custom_Subscription_Subscriptions SET [varExtensionSettingValueList]='" & Nz(rst!LStringEmail) & "' WHERE [varReportTitle]=" & rst![varReportTitle])
    Then do
    Debug.print mySQL 'to see how it is rendered
    Always test the sql before trying to execute.

  7. #7
    ssanfu is offline Master of Nothing
    Windows XP Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    @AAAndy,

    By posting your question in Bazsl's post, you have done what is termed "Hi-Jacking" a thread. You should have started your own thread and referenced this thread.
    You would probably get more reads if you had your own thread.


    However, there were several things wrong with your code.
    Compare this to your version:
    Code:
        Dim rst As dao.Recordset
        Dim sSQL As String
    
        
        sSQL = "SELECT [varReportTitle], LStringEmail FROM dbo_tbl_Custom_Subscription_Subscriptions"
        Set rst = CurrentDb.OpenRecordset(sSQL)
        '   MsgBox rst![varReportTitle]
        While Not rst.EOF
    
            sSQL = "UPDATE dbo_tbl_Custom_Subscription_Subscriptions"
            sSQL = sSQL & " SET [varExtensionSettingValueList] ='" & Nz(rst!LStringEmail) & "' WHERE [varReportTitle] = '" & rst![varReportTitle] & "';"
            Debug.Print sSQL
    
            CurrentDb.Execute sSQL, dbFailOnError
            rst.MoveNext
        Wend
    
        'clean up
        rst.Close
        Set rst = Nothing

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

Similar Threads

  1. Replies: 2
    Last Post: 11-24-2014, 05:53 PM
  2. Replies: 2
    Last Post: 09-26-2013, 09:13 PM
  3. UPDATE TO IIF Statement
    By anilytics in forum Queries
    Replies: 5
    Last Post: 03-09-2012, 03:45 AM
  4. SQL Update statement help
    By kalltim in forum Access
    Replies: 6
    Last Post: 01-18-2012, 07:30 AM
  5. What is wrong with my SQL Select statement?
    By John2810 in forum Programming
    Replies: 2
    Last Post: 04-01-2010, 10:30 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