Solved calculate textbox report (1 Viewer)

Akai90

Member
Local time
Today, 16:27
Joined
Feb 8, 2022
Messages
65
hi,

i have report need to calculate how many room have. total room have 18 field,

report jumla bilik.png

below is the code i use

Code:
=IIf(IsNull([JENISBILIK1]),"0",IIf(IsNull([JENISBILIK2]),"1",IIf(IsNull([JENISBILIK3]),"2",IIf(IsNull([JENISBILIK4]),"3",IIf(IsNull([JENISBILIK5]),"4",IIf(IsNull([JENISBILIK6]),"5",IIf(IsNull([JENISBILIK7]),"6",IIf(IsNull([JENISBILIK8]),"7",IIf(IsNull([JENISBILIK9]),"8",IIf(IsNull([JENISBILIK10]),"9",IIf(IsNull([JENISBILIK11]),"10",IIf(IsNull([JENISBILIK12]),"11",IIf(IsNull([JENISBILIK13]),"12",IIf(IsNull([JENISBILIK14]),"13","14"))))))))))))))

it can only put until no 14, if i put ,IIf(IsNull([JENISBILIK15]),"14" it will got error

the expression is to complex

any idea ?

i need to put 4 more untul i get 18
 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 01:27
Joined
Oct 29, 2018
Messages
21,447
You could use your own custom function.
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:27
Joined
Jan 23, 2006
Messages
15,380
What exactly are you trying to achieve? Please provide a clear statement, preferably with an example.
If English is not your mother tongue, then please be precise in your own language, then use Google translate(or similar) and post.
 

GPGeorge

Grover Park George
Local time
Today, 01:27
Joined
Nov 25, 2004
Messages
1,813
hi,

i have report need to calculate how many room have. total room have 18 field,

View attachment 98115
below is the code i use

Code:
=IIf(IsNull([JENISBILIK1]),"0",IIf(IsNull([JENISBILIK2]),"1",IIf(IsNull([JENISBILIK3]),"2",IIf(IsNull([JENISBILIK4]),"3",IIf(IsNull([JENISBILIK5]),"4",IIf(IsNull([JENISBILIK6]),"5",IIf(IsNull([JENISBILIK7]),"6",IIf(IsNull([JENISBILIK8]),"7",IIf(IsNull([JENISBILIK9]),"8",IIf(IsNull([JENISBILIK10]),"9",IIf(IsNull([JENISBILIK11]),"10",IIf(IsNull([JENISBILIK12]),"11",IIf(IsNull([JENISBILIK13]),"12",IIf(IsNull([JENISBILIK14]),"13","14"))))))))))))))

it can only put until no 14, if i put ,IIf(IsNull([JENISBILIK15]),"14" it will got error

the expression is to complex

any idea ?

i need to put 4 more untul i get 18
This has all the earmarks of a "Spreadsheet Style" table, not a relational database application table used in Access. You have, for example, JENISBILIK1, JENISBILIK2, JENISBILIK3, etc. If those are fields in a table, then that is a "spreadsheet" not a properly designed table. In such designs, queries are a real problem.

Here are some articles explaining the problem created by this table design and explaining how to correct it.
 

June7

AWF VIP
Local time
Today, 00:27
Joined
Mar 9, 2014
Messages
5,463
Could use less characters with Switch() function, however, doubt will be enough to allow 4 more terms.

Really looks like a non-normalized data structure and that may be root of problem.

Build a VBA custom function that uses Switch() or SELECT CASE. Will probably have to open a recordset object.

Provide sample of table. If you want to provide db for analysis, follow instructions at bottom of post.
 

Akai90

Member
Local time
Today, 16:27
Joined
Feb 8, 2022
Messages
65
here my example,
i can't share the real access due has very secret data
 

Attachments

  • New Microsoft Access Database.accdb
    1.1 MB · Views: 269

oleronesoftwares

Passionate Learner
Local time
Today, 01:27
Joined
Sep 22, 2014
Messages
1,159
Hi,
Its better you normalise your database
Fields like JENISBILIK1,JENISBILIK2,JENISBILIK3 etc can be a single field, then in your report it can be easier to perform a calculation on it.
 

jdraw

Super Moderator
Staff member
Local time
Today, 04:27
Joined
Jan 23, 2006
Messages
15,380
???? What is the purpose of this database? You should review and apply some database principles if you plan to use Access.
 

June7

AWF VIP
Local time
Today, 00:27
Joined
Mar 9, 2014
Messages
5,463
Function like:
Code:
Function CountRooms(intID)
Dim rs As DAO.Recordset, x As Integer, c As Integer
Set rs = CurrentDb.OpenRecordset("SELECT * FROM butirips WHERE ID=" & intID)
For x = 19 To rs.Fields.Count - 1
    c = c + IIf(Not IsNull(rs(x)), 1, 0)
    Debug.Print rs(x).Name
Next
CountRooms = c
End Function
Call from textbox:
=CountRooms([ID])
 

Akai90

Member
Local time
Today, 16:27
Joined
Feb 8, 2022
Messages
65
Function like:
Code:
Function CountRooms(intID)
Dim rs As DAO.Recordset, x As Integer, c As Integer
Set rs = CurrentDb.OpenRecordset("SELECT * FROM butirips WHERE ID=" & intID)
For x = 19 To rs.Fields.Count - 1
    c = c + IIf(Not IsNull(rs(x)), 1, 0)
    Debug.Print rs(x).Name
Next
CountRooms = c
End Function
Call from textbox:
=CountRooms([ID])
thanks sir.. it working, but if have add new field it will have error.
 
Last edited:

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 16:27
Joined
May 7, 2009
Messages
19,226
see also Query2 and your form.
 

Attachments

  • New Microsoft Access Database.accdb
    1.1 MB · Views: 280

Minty

AWF VIP
Local time
Today, 09:27
Joined
Jul 26, 2013
Messages
10,367
thanks sir.. it working, but if have add new field it will have error.
This is because your table design is wrong, as per posts #4 and #5

Fix that and your problems will be easily solved.
 

Users who are viewing this thread

Top Bottom