String ignoring null (1 Viewer)

fat controller

Slightly round the bend..
Local time
Today, 20:11
Joined
Apr 14, 2011
Messages
758
Is there a way to create a text string that ignores nulls?

I have the following fields

Zone1
Zone2
Zone3
Zone4
Zone5
Zone6

These are set to return a number as text (so 1, 2, 3 etc), or Null as appropriate.

What I am trying to achieve is that the text stored is concatenated into a string (TextOut), so if:

Zone1 = 1
Zone2 = Null
Zone3 = 3
Zone4 = 4
Zone5 = Null
Zone6 = Null

TextOut to show 1 3 4 and ignore the nulls. How would I achieve this?

EDIT - Would IIf(Is NotNull(zone1 & zone2 [etc]) work?
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:11
Joined
May 7, 2009
Messages
19,246
EDIT - Would IIf(Is NotNull(zone1 & zone2 [etc]) work?


IIf( IsNull( zone1 & zone3 & ....), ...., )
 

CJ_London

Super Moderator
Staff member
Local time
Today, 20:11
Joined
Feb 19, 2013
Messages
16,674
this code will do what you want but will have a comma at the end which you will need to remove - how you do this depends on whether this is in query, form or code

iif(not isnull(zone1),zone1 & ", ","") & iif(not isnull(zone2),zone2 & ", ","") & ....
 

MarkK

bit cruncher
Local time
Today, 12:11
Joined
Mar 17, 2004
Messages
8,187
Or in VBA, a loop . . .
Code:
dim i as integer
dim tmp as string
dim fld as dao.field

for i = 1 to 6
   set fld = rst.fields("Zone" & i)
   if not isnull(fld) then tmp = tmp & ", " & fld
next

if len(tmp) > 0 then tmp = Mid(tmp, 3)
. . . assuming these are fields in a recordset.
 

stopher

AWF VIP
Local time
Today, 20:11
Joined
Feb 1, 2006
Messages
2,395
Not sure I fully understand the requirements particular when I see other contributors interpretations. I don't see and commas in your output so I'd go for this (untested):

Trim( (Zone1+" ")&(Zone2+" ") & (Zone3+" ") & (Zone4+" ") & (Zone5+" ") & (Zone6+" ") )

Take a look here at the nuances between & and +
 

Accessna

Registered User.
Local time
Today, 12:11
Joined
Oct 4, 2015
Messages
15
Code:
Dim i as Byte
Dim tmp as string
 
tmp = ""
For i = 1 to 6
  tmp = tmp & Nz(fields("Zone" & i),"") & _
           IIf(Nz(fields("Zone" & I),"")<>"" and i<6,", ","")
Next i
 
Last edited:

fat controller

Slightly round the bend..
Local time
Today, 20:11
Joined
Apr 14, 2011
Messages
758
Not sure I fully understand the requirements particular when I see other contributors interpretations. I don't see and commas in your output so I'd go for this (untested):

Trim( (Zone1+" ")&(Zone2+" ") & (Zone3+" ") & (Zone4+" ") & (Zone5+" ") & (Zone6+" ") )

Take a look here at the nuances between & and +

Sorry for the late response to this - broadband issues along with a few other things, and of course work, have all slowed me down somewhat.

Thanks to all for the input - stopher's solution above worked a treat :)
 

Users who are viewing this thread

Top Bottom