Results 1 to 7 of 7
  1. #1
    GAJITCS is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2023
    Posts
    4

    SQL WHERE - No results when using WHERE

    DoCmd.RunSQL "INSERT INTO PartsChangeLog SELECT * FROM Parts "

    This command works and drops in every record from the table "Parts" into the table "PartsChangeLog", but is not what I want. What I need, is for the command to make use of a "WHERE" condition too.

    When the command is expanded to read:

    DoCmd.RunSQL "INSERT INTO PartsChangeLog SELECT * FROM Parts " & _
    "WHERE PartID=" & PartID

    the code still runs, but I am presented with a message that tells me that Zero records will be updated. I concluded that the WHERE condition is not operating as I need it to.

    The larger picture is that this code runs as an Event Procedure activated on the "Before Update" event of a form. The plan is that when a record on a form is changed, that the old values for the record are deposited in the PartsChangeLog table.
    The form data is based on a query. The query makes use of my own Record Navigation control which include a Search box. Data entered in the Search Box is used in the Criteria of the query as


    Like "*" & [forms]![Create / Edit Parts]![PartIDSearch] & "*" - Which is listed in the criteria of the PartID record and in the Or field against Item Description.

    The problem occurs whether the Search box is used or not and I have tried with a new form without these additional Navigation controls too with the same result.

    I am occasionally shown a message that reads "Run-time error '3075': Syntax Error (missing operator) in query expression "PartID=20-virt 8/25'.

    In that example, "20-virt 8/25" is the PartID of the record being amended.

    In table "Parts", PartID is a Primary Key, is Short Text, and Indexed=Yes (No duplicates)
    In table PartsChangeLog, PartID is not a Primary Key, is Short Text and Indexed=Yes (Duplicates OK)

    I think my WHERE condition is formatted incorrectly, but I can't work out how it should be.

    Can one of you good folk shine some light on this please.

  2. #2
    davegri's Avatar
    davegri is offline Excess Access
    Windows 11 Access 2019
    Join Date
    May 2012
    Location
    Denver
    Posts
    3,407
    try

    "Where PartsID='" & PartID & "'"

  3. #3
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    Debugging 101 - inspect the values after you have created them: Your problem is that your partID appears to be a string
    Use a variable to hold your SQL string.

    Code:
    Dim strSQl as String
    
    strSQL = "INSERT INTO PartsChangeLog SELECT * FROM Parts WHERE PartID = '" & Me.PartID & "'"
    
    Debug.Print strSQL 
    
    CurrentDb.Execute strSQL, DbSeeChanges
    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 ↓↓

  4. #4
    GAJITCS is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2023
    Posts
    4
    Quote Originally Posted by Minty View Post
    Debugging 101 - inspect the values after you have created them: Your problem is that your partID appears to be a string
    Use a variable to hold your SQL string.

    Code:
    Dim strSQl as String
    
    strSQL = "INSERT INTO PartsChangeLog SELECT * FROM Parts WHERE PartID = '" & Me.PartID & "'"
    
    Debug.Print strSQL 
    
    CurrentDb.Execute strSQL, DbSeeChanges
    Thank you Minty. I am new to this at the moment and haven't developed any sort of debugging stuff. Enclosing as you have '" & Me.PartID & "'" seems to be close to what Davegri went on to suggest too. I added your code to the Event procedure and nothing seemed to happen, but I fully expect I have put it in the wrong place.

  5. #5
    GAJITCS is offline Novice
    Windows 10 Office 365
    Join Date
    Mar 2023
    Posts
    4
    Quote Originally Posted by davegri View Post
    try

    "Where PartsID='" & PartID & "'"
    Thank you Davegri. Your code was inserted and worked.

    But I don't understand what made the difference or why?

    If you have time to explain, I would appreciate it.

  6. #6
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    4,914
    Quote Originally Posted by GAJITCS View Post
    Thank you Davegri. Your code was inserted and worked.

    But I don't understand what made the difference or why?

    If you have time to explain, I would appreciate it.
    Because your PartID is text and not numeric.
    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

  7. #7
    Minty is offline VIP
    Windows 10 Office 365
    Join Date
    Sep 2017
    Location
    UK - Wiltshire
    Posts
    3,003
    To compare or insert strings, a string value need to be escaped by a delimiter. That delimiter is normally a double quote " e.g.

    "Fred"

    However you are already in the middle of creating a string so you can't easily use another " mark so we use another acceptable delimiter the single quote ' e.g.

    'Fred'

    So your concatenated string becomes

    "INSERT INTO PartsChangeLog SELECT * FROM Parts WHERE PartID = 'Fred' "

    Dates need the # delimiter around them e.g. "Where MyDate = #2023-05-14#"

    Numbers don't need a delimiter.

    Does that help?
    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 ↓↓

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

Similar Threads

  1. Replies: 6
    Last Post: 08-21-2019, 07:17 AM
  2. Replies: 2
    Last Post: 04-15-2017, 11:09 AM
  3. Replies: 5
    Last Post: 05-21-2013, 02:21 PM
  4. Replies: 3
    Last Post: 05-02-2013, 10:36 AM
  5. Replies: 6
    Last Post: 05-14-2012, 07:24 AM

Tags for this Thread

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