Results 1 to 7 of 7
  1. #1
    btappan is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    43

    concatenate with + sign for both leading and trailing characters

    I have some data that I need to concatenate that each field requires both leading and trailing data for XML purposes.



    My query looks like this:

    "<NAME>" & [tABLE].Name & "</NAME><AGE>" & [tABLE].Age & "</AGE><GENDER>" & [tABLE].Gender & "</GENDER>


    Which outputs:

    "<NAME>Bob</NAME><AGE>31</AGE><GENDER>Male</GENDER>


    I've tried using the + operator instead of the & in various ways, but don't get a clean output when a field is blank so that no XML tags are present on a blank field. Any Help?

  2. #2
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    If you are building a query in VBA in a string it would look like this...
    Code:
    sqlSTR = "<NAME>" & [tABLE].Name & "</NAME>" & _
             "IIF(IsNull([tABLE].Age, '', '<AGE>' & [tABLE].Age & '</AGE>') & _
             "IIF(IsNull([tABLE].Gender, '', '<GENDER>' & [tABLE].Gender & '</GENDER>') ...
    Or if you are building a query in SQL view it would look like this
    Code:
    "<NAME>" & [tABLE].Name & "</NAME>" & IIF(IsNull([tABLE].Age, """", "<AGE>" & [tABLE].Age & "</AGE>") & IIF(IsNull([tABLE].Gender, """", "<GENDER>" & [tABLE].Gender & "</GENDER>") ...
    YOu'll have to figure out how many quotes go where...

  3. #3
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    when a field is blank so that no XML tags are present on a blank field.
    @Dal
    Shouldn't the code be??
    Code:
    sqlSTR = "<NAME>" & [tABLE].Name & "</NAME>" & _
             "IIF(IsNull([tABLE].Age, "", "<AGE>" & [tABLE].Age & "</AGE>") & _
             "IIF(IsNull([tABLE].Gender,"", "<GENDER>" & [tABLE].Gender & "</GENDER>") ...
    If the age is null, don't show the tags, if not null, show the tags.
    If the gender is null, don't show the tags, if not null, show the tags.

  4. #4
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Yup. I started out thinking of using NZ, and never mentally flipped the order. Also, I had a completely incorrect number of quotes for that to work - forgot to double all the internal double quotes.

    I've fixed it to use single quotes inside the doubles, but the code is still aircode.

  5. #5
    btappan is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    43
    Thanks guys, I'm using SQL view, is your code below correct then or not? I'm getting an Syntax error

    Full query copy and pasted from SQL view:

    SELECT "<FIRST_NAME>" & [Table].FirstName & "</FIRST_NAME>" & IIF(IsNull([Table].Age, """", "<AGE>" & [Table].Age & "</AGE>") & IIF(IsNull([Table].Gender, """", "<GENDER>" & [Table].Gender & "</GENDER>") AS Test From Table

    And yes, my testing table is named "Table" and my field names are FirstName, Age, and Gender



    Quote Originally Posted by Dal Jeanis View Post
    If you are building a query in VBA in a string it would look like this...

    Or if you are building a query in SQL view it would look like this
    Code:
    "<NAME>" & [tABLE].Name & "</NAME>" & IIF(IsNull([tABLE].Age, """", "<AGE>" & [tABLE].Age & "</AGE>") & IIF(IsNull([tABLE].Gender, """", "<GENDER>" & [tABLE].Gender & "</GENDER>") ...
    YOu'll have to figure out how many quotes go where...

  6. #6
    btappan is offline Advanced Beginner
    Windows 7 64bit Access 2010 64bit
    Join Date
    Oct 2013
    Posts
    43
    Never mind, figured it out, too many quotes and unbracketed FROM table. Thanks guys.

    SELECT IIf(IsNull([Table].[FirstName]),"","<FIRST_NAME>" & [tABLE].[FirstName] & "</FIRST_NAME>") & IIf(IsNull([Table].[Age]),"","<AGE>" & [tABLE].[Age] & "</AGE>") & IIf(IsNull([Table].[Gender]),"","<GENDER>" & [tABLE].[Age] & "</GENDER>") AS Testi
    FROM [Table];

  7. #7
    Dal Jeanis is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    May 2013
    Location
    Dallas TX
    Posts
    1,742
    Good job! Please mark thread solved. Top of page, under "thread tools".

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

Similar Threads

  1. Replies: 9
    Last Post: 02-11-2013, 03:09 PM
  2. Sign Out and Sign In Student Database
    By jamiers in forum Forms
    Replies: 6
    Last Post: 08-29-2012, 02:03 PM
  3. Replies: 35
    Last Post: 09-19-2011, 10:13 AM
  4. Removing Trailing Numbers
    By swagger18 in forum Programming
    Replies: 3
    Last Post: 01-28-2011, 02:39 AM
  5. Replies: 9
    Last Post: 07-16-2009, 09:13 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