Solved

expression to string different text strings together while ignoring null fields

Posted on 2009-04-13
4
181 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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