Solved

expression to string different text strings together while ignoring null fields

Posted on 2009-04-13
4
182 Views
Last Modified: 2012-05-06
ive got 5 fields of text and i want to create a query that runs an expression that strings all of the non-null fields (of that 5) together (intesperced with " - "). ive been creating a series of if statments which is ok for 3 fields, but gets crazy with any more than that.

can anyone help?

td
0
Comment
Question by:sci-aid
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 22

Expert Comment

by:Kelvin Sparks
ID: 24133836
Use a code module - for instances

Function StringJoin(Field1 as variant, Feild2 as variant, Field3 as variant, Field4 as variant, Field5 as variant) as string


if not isnull(Field1) then
     stringjoin = Field1
end if

If not IsNull(Field2) then
     If StringJoin <> "" Then
            StringJoin = StringJoin & "-" & Fiel2
     Else
            StringJoin = Field2
    End If
End If

Repeat for fields3 to 5

In Query call the function

Yourfield:StringJoin(Field1, Field2,......)




Kelvin


end function
0
 

Author Comment

by:sci-aid
ID: 24133882
thanks kelvin - ill give it a go. im a newbie with code so give me a while. td.
0
 

Author Comment

by:sci-aid
ID: 24149878
hi kelvin - as i said, bare with me as im a big time novice with the code side of access. ive had a play around with what you suggested but im not sure how to place a function within the query. i know its wrong, but here is what ive got so far:

SELECT frm_temp_protocol_01.Habitat, frm_temp_protocol_01.DataSource, frm_temp_protocol_01.Photoperiod24h, frm_temp_protocol_01.PhotoperiodObs

Function StringJoin(Habitat as variant, DataSource as variant, Photoperiod24h as variant, PhotoperiodObs as variant) as string
if not isnull(Habiat) then
     StringJoin = Habitat
end if

If not IsNull(DataSource) then
     If StringJoin <> "" Then
            StringJoin = StringJoin & ", " & DataSource
     Else
            StringJoin = DataSource
    End If
End If

If not IsNull(Photoperiod24h) then
     If StringJoin <> "" Then
            StringJoin = StringJoin & ", " & Photoperiod24h
     Else
            StringJoin = Photoperiod24h
    End If
End If

If not IsNull(PhotoperiodObs) then
     If StringJoin <> "" Then
            StringJoin = StringJoin & ", " & PhotoperiodObs
     Else
            StringJoin = PhotoperiodObs
    End If
End If

end function

FROM frm_temp_protocol_01;
0
 
LVL 22

Accepted Solution

by:
Kelvin Sparks earned 250 total points
ID: 24153281
The function is saved into the code modules.

The query is

SELECT StringJoin(frm_temp_protocol_01.Habitat, frm_temp_protocol_01.DataSource, frm_temp_protocol_01.Photoperiod24h, frm_temp_protocol_01.PhotoperiodObs) as NewString from frm_temp_protocol_01

My use of NewString in the query can be replaced with whatever you want to call the created column.


Kelvin
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question