Results 1 to 11 of 11
  1. #1
    onlylonely is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    110

    Question Insert Into .... Where .....

    G'day All,

    I'm facing with SQL code that not allowed me to insert value into table.

    CurrentDb.Execute "INSERT INTO IssuesTbl (Complete) Where PartNumber = " & Me.PartNumber & " AND IssueID = " & Me.cmbID & "" & _
    "VALUES('CLOSE')"
    CloseSQL = "SELECT Complete FROM IssuesTbl Where PartNumber ='" & Me.PartNumber & "' AND ID = '" & Me.cmbID & "'"


    CurrentDb.Execute "INSERT INTO CloseSQL(Complete)" & _
    "VALUES('Close')"

    Your advice is highly appreciated.



    ID PartNumber Issues Link Complete
    3 123 Good year 123
    Attached Thumbnails Attached Thumbnails TABLE.JPG  

  2. #2
    alansidman's Avatar
    alansidman is offline Indifferent
    Windows 10 Access 2016
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,536
    Look at this tutorial to ensure that your syntax is correct.

    https://www.techonthenet.com/sql/insert.php

  3. #3
    onlylonely is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    110
    Quote Originally Posted by alansidman View Post
    Look at this tutorial to ensure that your syntax is correct.

    https://www.techonthenet.com/sql/insert.php
    Hi Alansidman,

    Thank for your reply. Ya, i know the syntax is wrong. But i can't find a syntax which using Insert Into .... Where...
    usually they use insert into ...select ....

    CurrentDb.Execute "INSERT INTO IssuesTbl(Complete)" & _
    "VALUES('CLOSE')" & _
    "Where [IssuesTbl].[PartNumber] = " & Me.PartNumber & " And [IssuesTbl].[ID] = " & Me.cmbID & ""

    I've tried this code but still not able to ADD the "CLOSE" into Complete column. Any advice?

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Get the query working first in design view, then copy and paste the SQL.

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    You also don't appear to have a field called 'IssueID'.

    if using currentdb.execute, also add dbfailonerror as the second parameter - that way you will get the error reported as if running the query directly.

    Also worthwhile building the sgl string as per your closesql, then debug.print it. You can then see if it makes sense and can copy and paste from the immediate window into the query sql window and try to run it.
    Last edited by CJ_London; 06-16-2017 at 10:54 AM. Reason: mistook a space for no space

  6. #6
    onlylonely is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    110
    Quote Originally Posted by Ajax View Post
    You also don't appear to have a field called 'IssueID'.

    if using currentdb.execute, also add dbfailonerror as the second parameter - that way you will get the error reported as if running the query directly.

    Also worthwhile building the sgl string as per your closesql, then debug.print it. You can then see if it makes sense and can copy and paste from the immediate window into the query sql window and try to run it.
    Hi Ajax,

    Thanks for your reply. I'm only able to understand 50percents of your comment.
    It's too profound to me. You are right, i notice that I've a typo on "IssuesID"
    My table name has no bracket, may i know why do you think that i've a bracket for my table name?

  7. #7
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,845
    I edited my post to correct - it looked like there was no space between IssuesTbl and (Complete)

    I'm suggesting your code should be something like

    Code:
    dim sqlstr as string
    
    sqlstr="INSERT.....
    debug.print sqlstr 'see immediate window for the calculated string, copy and paste in the sql window into the query builder
    currentdb.execute sqlstr, dbfailonerror

  8. #8
    onlylonely is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    110
    Quote Originally Posted by Ajax View Post
    I edited my post to correct - it looked like there was no space between IssuesTbl and (Complete)

    I'm suggesting your code should be something like

    Code:
    dim sqlstr as string
    
    sqlstr="INSERT.....
    debug.print sqlstr 'see immediate window for the calculated string, copy and paste in the sql window into the query builder
    currentdb.execute sqlstr, dbfailonerror
    Hi AJax,

    Thank You for your reply. I think i've figured out something new. Which is using

    Function Update()
    Dim Complete As String


    Update IssuesTbl
    Set Complete = "A"
    WHERE PartNumber = " & Me.PartNumber & "


    End Function

    But it pops out "Object required". Any advice?

  9. #9
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Code:
    Function Update()
    Code:
    Dim sSQL As String
    
    'update statement where part number is a numeric field (i.e. the primary key of the 'parts' table)
    'sSQL = "Update IssuesTbl Set Complete = 'A' WHERE PartNumber = " & Me.PartNumber 
    'update statement where part number is a text field (i.e. the primary key of the 'parts' table)
    'sSQL = "Update IssuesTbl Set Complete = 'A' WHERE PartNumber = '" & Me.PartNumber  & "'"
    
    currentdb.execute(Complete)
    
    End Function


    uncomment the sql statement builder that applies to you

  10. #10
    onlylonely is offline Competent Performer
    Windows 7 64bit Access 2013 64bit
    Join Date
    May 2017
    Posts
    110
    Quote Originally Posted by rpeare View Post
    Code:
    Function Update()
    Code:
    Dim sSQL As String
    
    'update statement where part number is a numeric field (i.e. the primary key of the 'parts' table)
    'sSQL = "Update IssuesTbl Set Complete = 'A' WHERE PartNumber = " & Me.PartNumber 
    'update statement where part number is a text field (i.e. the primary key of the 'parts' table)
    'sSQL = "Update IssuesTbl Set Complete = 'A' WHERE PartNumber = '" & Me.PartNumber  & "'"
    
    currentdb.execute(Complete)
    
    End Function


    uncomment the sql statement builder that applies to you
    Hi Rpeare,

    Thank You for your reply. But I'm still getting error when i using the code.
    I've checked multiple times, all the location and names are correct.

    Click image for larger version. 

Name:	issuestbl.JPG 
Views:	10 
Size:	31.6 KB 
ID:	29142
    Attached Thumbnails Attached Thumbnails TABLE.JPG  
    Last edited by onlylonely; 06-16-2017 at 01:00 PM. Reason: Wrong img file

  11. #11
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    can't tell from what you posted. If you modified the code I gave you I'd need to see that, you still have everything commented out so there's no way of telling what you're doing or what you changed, nor can I see the event procedure that's calling the update, nor can I see whether or not (still) the partnumber is text or number, I think it make be text just because of it's alignment but I don't really know.

    Finally I made an error in my initial post

    it should be currentdb.execute(ssql)

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

Similar Threads

  1. Replies: 1
    Last Post: 08-29-2016, 07:50 PM
  2. Help with INSERT INTO
    By tcox in forum Programming
    Replies: 6
    Last Post: 10-12-2015, 12:20 PM
  3. Replies: 3
    Last Post: 05-27-2015, 01:04 PM
  4. INSERT query: insert new data only
    By drh in forum Access
    Replies: 2
    Last Post: 04-04-2014, 05:31 PM
  5. New insert row
    By khparhami in forum Access
    Replies: 5
    Last Post: 09-06-2010, 10:37 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