Results 1 to 4 of 4
  1. #1
    john134 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jan 2014
    Posts
    42

    Using a WHERE clause in an SQL statement

    I have the following lines of code:

    Dim Temp AS String
    Temp = Forms("Form1").[Name1].Value 'in this case Temp refers to 'Don'
    SQL = "INSERT INTO [Table2](Name1) SELECT [Table1].[Name1] FROM [Table1] WHERE ([Table1].[Name1]=Temp)"
    DoCmd.RunSQL SQL

    The error message is to input the parameter "Temp".
    I have come to the conclusion after much experimenting that the WHERE clause


    will not accept a variable for the object of the clause. It will work when I use
    a string, such as 'Don' instead of Temp.

    Can someone verify that I am correct in this case? And, if so, is there some kind
    of work around for it?

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    You have to concatenate the variable into the string:

    SQL = "INSERT INTO [Table2](Name1) SELECT [Table1].[Name1] FROM [Table1] WHERE ([Table1].[Name1]='" & Temp & "')"
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    john134 is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jan 2014
    Posts
    42
    It worked. Many thanks.

  4. #4
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    Happy to help!
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

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

Similar Threads

  1. SQL WHERE clause with an AND in VBA
    By CharissaBelle in forum Forms
    Replies: 3
    Last Post: 04-14-2016, 04:29 PM
  2. Error FROM clause of your SQL statement
    By Stephanie53 in forum Forms
    Replies: 8
    Last Post: 05-15-2013, 04:04 PM
  3. Switch Statement in WHERE Clause
    By Gray in forum Queries
    Replies: 1
    Last Post: 06-02-2011, 06:50 AM
  4. Use of COLLATE statement in SELECT clause
    By zurek in forum Queries
    Replies: 7
    Last Post: 03-16-2011, 06:46 AM
  5. Help on WHERE clause
    By QBCM in forum Programming
    Replies: 1
    Last Post: 12-19-2005, 08:43 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