Solved

expression to string different text strings together while ignoring null fields

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

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…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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