Results 1 to 4 of 4
  1. #1
    mdavid is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2017
    Posts
    160

    Syntax error in query expression

    Hi I have the following code:



    Code:
    Private Sub CopySlctd2FP_Click()
    Dim ctl As Control
    Dim strList As String
    Dim varSelected As Variant
    Dim strSQL As String
    
    
      Set ctl = Me!lstbMempComplaints
      If ctl.ItemsSelected.Count = 0 Then
        MsgBox "You haven't selected anything"
      Else
        For Each varSelected In ctl.ItemsSelected
          strList = strList & ctl.ItemData(varSelected) & ", "
          strSQL = "INSERT INTO FPSubComplaint(ComplaintID, Complaint, Preparation, Administration, PartsUsed, Healer) " & _
                   "VALUES ('" & ctl.ItemData(varSelected) & "', SELECT Complaint, Preparation, Administration, PartUsed, " & _
                   "Healer From MempComplaintTom Where ComplaintID = """ & ctl.ItemData(varSelected) & """)"
          Debug.Print strSQL
          DoCmd.RunSQL strSQL
        Next varSelected
        strList = Left$(strList, Len(strList) - 2)
        MsgBox "You selected the following items:" & vbCrLf & strList
      End If
    End Sub
    The Debug.print outputs:

    INSERT INTO FPSubComplaint(ComplaintID, Complaint, Preparation, Administration, PartsUsed, Healer) VALUES ('199', SELECT Complaint, Preparation, Administration, PartUsed, Healer From MempComplaintTom Where ComplaintID = "199")

    On DoCmd.RunSQL strSQL I get the error: "Syntax error in Query expression 'SELECT Complaint'."

    Can someone please tell me what's wrong with query

    Thanks
    David

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    strSQL = "INSERT INTO FPSubComplaint(ComplaintID, Complaint, Preparation, Administration, PartsUsed, Healer) " & _
    "SELECT " & ctl.ItemData(varSelected) & " AS CID, Complaint, Preparation, Administration, PartUsed, Healer " & _
    "FROM MempComplaintTom WHERE ComplaintID = " & ctl.ItemData(varSelected)

    Is ComplaintID a number type? If not then use apostrophe delimiters.


    Review https://www.w3schools.com/SQL/sql_in...nto_select.asp
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    mdavid is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    Oct 2017
    Posts
    160
    Thanks June,
    Drives me crazy trying to sort the quote from the quotes.
    Is there a way of building this type of a statement with a wizard?

    Thanks
    David

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Can use query builder to help with syntax but then transferring to VBA you will have to figure out the concatenation and use of quotes/apostrophes.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Syntax error in query expression
    By ittiekat in forum Queries
    Replies: 6
    Last Post: 02-11-2016, 05:39 PM
  2. Replies: 2
    Last Post: 09-10-2014, 11:30 AM
  3. Replies: 11
    Last Post: 05-01-2014, 11:56 AM
  4. Replies: 3
    Last Post: 03-05-2013, 11:17 AM
  5. Replies: 6
    Last Post: 05-30-2012, 12:32 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