Results 1 to 9 of 9
  1. #1
    sandman is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Oct 2021
    Location
    Tasmania
    Posts
    14

    Sql update query string converted to db.execute not working

    The following query string works perfectly when run from a command button.




    Dim sqlSTR As String
    sqlSTR = "UPDATE Table1 SET Table1.[attachment] = [Forms]![Form1]![attach] WHERE Table1.[ref]= [Forms]![Form1]![ref];"
    DoCmd.RunSQL sqlSTR


    I have tried with many variations of cocatenate on the reference fields (Form1.attach) and (Form1.ref) in the string SqlSTR which I used in the reference of strSQL in db.execute strSQL,dbFailOnError
    but they all continually fail.

    I get runtime error 424 Object required.


    Some help is much appreciated
    Last edited by sandman; 10-22-2021 at 06:51 AM. Reason: spelling

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    No, I reckon you need to concatenate.

    Put it ALL into a string variable and debug.print that variable.

    If you still cannot get it to work, post back with the output of the debug.print and what type of value the form control holds, number, string or date.

    Is it actually an attachment as well?

    Edit: If actually an attachment, that apparently is not the way to do it.

    I have Google on this laptop and a quick search brought up https://stackoverflow.com/questions/...o-access-table
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    sandman is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Oct 2021
    Location
    Tasmania
    Posts
    14
    Thanks for your help,

    This is the result of the debug.print. its exactly the same as the original sqlSTR

    UPDATE Table1 SET Table1.[attachment] = [Forms]![Form1]![attach] WHERE Table1.[ref]= [Forms]![Form1]![ref];

    No it is not an attachment, all the field references are set as text as are the table fields

  4. #4
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    If the form is open the simply concatenate the values.

    sqlSTR = "UPDATE Table1 SET Table1.[attachment] = '" & Me.[attach] & "' WHERE Table1.[ref]= '" & Me.[ref] & "'"

    Debug.Print sqlSTR

    I have assumed both are strings.

    The VBA compiler doesn't understand the form reference in the shape of [Forms]![Form1]![attach].
    Saved
    queries do and DoCmd.RunSQL is invoking the same interpreted engine.
    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  5. #5
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    I might not be expressing this with the proper terminology but I believe the first works because RunSql uses the query 'engine' where such form references work. You can prove that by using form references in a test query. The latter method does not work the same way. .Execute is a method of Application.CurrentDb which uses ACE (formerly JET?) whereas RunSql is a method of the DoCmd object. If anyone has the exact skinny on this, please post it.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #6
    sandman is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Oct 2021
    Location
    Tasmania
    Posts
    14
    Minty your answer is what appears to be almost there, it throws a wobbler when I use your exact string so

    I modified it to
    sqlSTR = "UPDATE Table1 SET Table1.[attachment] = " & Me.[attach] & " WHERE Table1.[ref]= " & Me.[ref] & ";"

    When i run it the debug.print shows as

    UPDATE Table1 SET Table1.[attachment] = testing WHERE Table1.[ref]= a;

    This is exactly what is in the text boxes on the form.

    However the table doesn't update and the Vb error message is "3061 to few parameters. Expected 2"

    Any ideas??

    and debug refers to CurrentDb.Execute sqlSTR, dbFailOnError

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    You need to include the string separators I put in.

    sqlSTR = "UPDATE Table1 SET Table1.[attachment] = '" & Me.[attach] & "' WHERE Table1.[ref]= '" & Me.[ref] & "'"

    Which should generate

    UPDATE Table1 SET Table1.[attachment] = 'testing' WHERE Table1.[ref]= 'a'

    You don't need the semi colon on the end.

    DLookup Syntax and others http://access.mvps.org/access/general/gen0018.htm
    Please use the star below the post to say thanks if we have helped !
    ↓↓ It's down here ↓↓

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,927
    Can you see them now?
    Code:
    sqlSTR = "UPDATE Table1 SET Table1.[attachment] = '" & Me.[attach] & "' WHERE Table1.[ref]= '" & Me.[ref] & "'"
    

    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  9. #9
    sandman is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Oct 2021
    Location
    Tasmania
    Posts
    14
    Yep it works like a charm. Thanks to you both for the assist. it was the delimiters that were missing or out of position. "Lesson learned". Sorry I didn't answer sooner, it was 3am here when I last posted.

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

Similar Threads

  1. Replies: 38
    Last Post: 09-21-2019, 06:10 PM
  2. Replies: 5
    Last Post: 02-20-2018, 07:25 PM
  3. Replies: 2
    Last Post: 01-23-2018, 09:11 AM
  4. Replies: 9
    Last Post: 01-14-2015, 05:10 PM
  5. Converted macro to VBA not working
    By gemadan96 in forum Forms
    Replies: 5
    Last Post: 06-04-2014, 10:36 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