Results 1 to 8 of 8
  1. #1
    vicsaccess's Avatar
    vicsaccess is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    451

    need help with single and double quotes


    I have a SQL from a recordsource that I am trying to put to code behind a command button so that when the user clicks on the command button the recordsource changes. I have the string together but am having problems understanding when to use single and double quotes. I understand that I need to use single if the variable is a text string but I haven't done this with a trim before and am pulling out my hair. can someone please help me understand where and why to use single quotes in this string

    Code:
    Private Sub cmdcraft_Click()
        Dim strnewrecord As String
        strnewrecord = "SELECT CraftT.Craft, CraftT.Specialty, JobCraftT.Hours,  " _
            & "Trim([department] "& "  , " & [system] & " , " & [asset] & " , " & [component]") AS Task, " _
            & "WorkQueT.SchInd, JobT.JobID, WorkQueT.Summary, WorkT.WorkType, WorkQueT.WorkQueID, JobT.SafeWorkPermit, " _
            & "JobT.HoursPlanned, JobT.ContractorsNeeded, JobT.PartsStatus, JobT.schedule, WorkQueT.ScheduledDate, " _
            & "WorkQueT.ScheduleTime, [6AssetT].Asset" _
            & "FROM (JobCraftT RIGHT JOIN ((((((WorkQueT INNER JOIN 4DepartmentT ON WorkQueT.DeptID = [4DepartmentT].DeptID) " _
            & "INNER JOIN 5SystemT ON WorkQueT.SystemID = [5SystemT].SystemID) INNER JOIN 6AssetT ON WorkQueT.AssetID = [6AssetT].AssetID) " _
            & "INNER JOIN 7ComponentT ON WorkQueT.ComponentID = [7ComponentT].ComponentID) INNER JOIN WorkT ON WorkQueT.WorkID = WorkT.WorkID) " _
            & "INNER JOIN JobT ON WorkQueT.WorkQueID = JobT.WorkQueID) ON JobCraftT.JobID = JobT.JobID) " _
            & "LEFT JOIN CraftT ON JobCraftT.CraftID = CraftT.CraftID " _
            & "WHERE (((JobT.schedule)=True))ORDER BY WorkQueT.SchInd DESC; " _
            Debug.Print (strnewrecord)
        'Me.RecordSource = strnewrecord
        
    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    An apostrophe or doubled quote marks are usually used to delimit text parameters in filter criteria. You don't have any text fields in the WHERE clause.

    However, you are trying to build concatenation of fields which I think requires escaping the & character so the SQL statement will build with the literal character and delimiting the commas with apostrophes.

    Try:

    "Trim([department] && ', ' && [system] && ', ' && [asset] && ', ' && [component]) AS Task, "

    Alternatively, don't concatenate in query, concatenate in textbox.

    Why do you need to programmatically change object RecordSource?

    Why are you using Trim()?
    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
    vicsaccess's Avatar
    vicsaccess is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    451
    thanks June7, that's getting me closer, now I just have to find my syntax error. the trim is for the user to view only one text box rather than 4. is there a better way?

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Alternatively, don't concatenate in query, concatenate in textbox.

    Why do you need to programmatically change object RecordSource?

    Why are you using Trim()? How does Trim() help with concatenation?
    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.

  5. #5
    vicsaccess's Avatar
    vicsaccess is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    451
    hadn't thought of using the text box. that would be easier. as for using trim(), because up till now that's how I learned to concatenate. i'll change it to the text box and keep it simple. if you were to concatenate programmatically how should it be done? I'm trying to use the command button to change record source from a simple grouped by overview to a more detailed view.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    Missing a space at end of line:

    & "WorkQueT.ScheduleTime, [6AssetT].Asset "

    Didn't I already show the programmatic concatenation?
    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.

  7. #7
    vicsaccess's Avatar
    vicsaccess is offline Competent Performer
    Windows 8 Access 2013
    Join Date
    Apr 2015
    Posts
    451
    yes, thank you. I miss understood. thought maybe there was a better way to do the concatenation. will move it to the text box and simplify it.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,919
    You could build a query object and then simply reference the query object name when setting the RecordSource.
    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. Replies: 2
    Last Post: 05-29-2014, 09:30 PM
  2. Single and double quotes in a INSERT statement
    By dccjr in forum Programming
    Replies: 16
    Last Post: 03-14-2013, 09:50 PM
  3. Deleting double quotes out of my table?
    By kawi6rr in forum Queries
    Replies: 3
    Last Post: 02-19-2013, 04:32 PM
  4. Putting double quotes around text
    By weg220 in forum Queries
    Replies: 5
    Last Post: 12-21-2012, 10:16 AM
  5. Replace double quotes
    By Kay in forum Programming
    Replies: 27
    Last Post: 12-10-2012, 10:04 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