Solved

expression to string different text strings together while ignoring null fields

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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