Page 1 of 2 12 LastLast
Results 1 to 15 of 20
  1. #1
    plackowski is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    15

    Quotation Chaos

    I'm trying to build a SQL statement so I can create a recordset from my data. I'm pretty new to this, so I started by working from an existing module. I managed to get this string to work:

    Code:
    strSql = "SELECT CableNumF FROM CableRoute WHERE " & strWhere & " ORDER BY [CableNumF], [Segment]"
    ...if I set strWhere to "[CondNameF] = """ & [CondName] & """" as an argument in my recordsource.

    However, I'm trying to build the function completely in VBA. I tried following the instructions here http://www.baldyweb.com/buildsql.htm and came up with the following:

    Code:
    strSql = "SELECT CableNumF FROM CableRoute WHERE CondNameF = " & Chr$(39) & CondName & ​Chr$(39) & " ORDER BY [CableNumF], [Segment]"
    I've tried every variation of quotes and apostrophes I can think of, but when I output the string to my report to test it, I can never get CondName to convert to its contents. All of the fields are text.

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    If CondNameF is a text field, then it will be
    ....CondFNameF='" & CondName & "' ORDER BY....

  3. #3
    plackowski is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    15
    That just returns
    Code:
    SELECT CableNumF FROM CableRoute WHERE CondNameF = '' ORDER BY [CableNumF], [Segment]
    when it should return
    Code:
    SELECT CableNumF FROM CableRoute WHERE [CondNameF] = "C002" ORDER BY [CableNumF], [Segment]

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    WHERE CondNameF = single quote double quote & CondName & double quote single quote

  5. #5
    plackowski is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    15
    That's what I used. I copied and pasted.

  6. #6
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Then the CondName must be empty, as it is showing. Put a VBA stop on that line and see what the value is in that field. Is it coming from a form?

  7. #7
    plackowski is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    15
    CondName is a field that exists in the table and report I am calling the function from. It is not a field in the CableRoute table. It does come up empty when I stop the code at that line as you suggested.

  8. #8
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Then that is your problem, not quotations - they are working correctly as coded. All you have posted is the SQL string, no idea where CondName is coming from.

  9. #9
    plackowski is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    15
    So there's no way for me to hard code my function to work with a particular table? In other words, I can't retrieve fields from the table the function is running in, unless I pass them in with an argument?

  10. #10
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Remove the WHERE condition, then you will get all records - not sure I understand what you are trying to do. In your first post you were truing to pass a parameter?

  11. #11
    plackowski is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    15
    I've got three tables, Conduit, Cable, and CableRoute. Each cable can pass through multiple conduit, and each conduit can contain multiple cable. To keep track of these relationships, I created the CableRoute table, which uses CableNumF, Segment, and CondNameF as fields. The F is for foreign key, since CableNum and CondName are the primary keys in the Cable and Conduit tables, respectively. The CableRoute table uses CableNumF and the Segment as a dual primary key. So each cable is now split into multiple segments (1,2,3, etc) and each segment is assigned to a conduit.

    Now I'm trying to create a conduit report. And for each conduit, I need to use concatenation to form a list of the cables they contain. So I'm trying to create a SQL query that will grab all CableNumF's from the CableRoute table where the current CondName (in the conduit table) matches the CondNameF (in the CableRoute table).

  12. #12
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    The query will handle the linking. The record source for your report will be a query that will have all conduits and each cable that belongs to it, you do that by linking the two tables together. Before you start creating a report, have the query ready, containing all the fields that you want on your report. Then use the report wizard to create the report, use the grouping options to separate the report into conduits showing all the cables which belong to it.

  13. #13
    plackowski is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    15
    The problem with that is that each conduit would then take up an extra row for each cable it contains. But I'm trying to mimic an existing table set up in excel, and if I can't summarize all the data for each conduit on one line, the final report size will balloon from 30 pages of conduits to 300.

    Once I've got the query built that lists the cables in each conduit, I'm using a while loop to concatenate the query's contents. I'm sure that part works since I borrowed it from another project online. It's just odd to me that I can't build the query completely in VBA and that I have to pass fields into the query using the strWhere variable. If I can pass it in, why can't I hard code it?

  14. #14
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    It's odd to me that you have to use VBA for this! There are many ways to handle the multiple-records issue, either you can use a grouped query or you can let the report do it for you. It seems to me that you want a heading for the conduit and a detail line for each cable - that is what reports do, without any intervention. If you don't want to show the cables in the detail section then the summaries can be handled in the group footer, one line on the report per conduit - again, the report will do all of this for you.

  15. #15
    plackowski is offline Novice
    Windows 7 64bit Access 2016
    Join Date
    Feb 2017
    Posts
    15
    Please, enlighten me! I would definitely prefer to avoid VBA if possible, I just haven't been able to find an example similar to this.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. String Literacy with Quotation Issue
    By excellenthelp in forum Programming
    Replies: 5
    Last Post: 07-31-2014, 09:05 AM
  2. InStr() and quotation marks
    By Puebles in forum Programming
    Replies: 3
    Last Post: 11-22-2013, 07:36 AM
  3. Quotation Mark Hell
    By shurstgbr in forum Programming
    Replies: 4
    Last Post: 06-13-2011, 06:17 AM
  4. Concatenate Quotation Marks
    By millerdav99 in forum Programming
    Replies: 3
    Last Post: 04-11-2011, 09:36 AM
  5. Transferring text within quotation marks
    By Lynette in forum Access
    Replies: 6
    Last Post: 11-24-2010, 11:58 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