Loops

xPaul

Registered User.
Local time
Today, 13:00
Joined
Jan 27, 2013
Messages
65
Hi all,

I have 7 textboxes on a form, 6 - for data manipulation and another to display how many of those 6 filled out.

The text boxes are called:
txt_name through to txt_name_5

And the display is called:
txt_count

What I am trying to do is create a loop that will count how many of those text boxes aren't null, and then return the value based on that to txt_count.

I have tried a few things like:

PHP:
count = 6
 
For CountRecords = Me.txt_name to Me.txt_name_5 
 
If Len(count) > 1 then
count = count - 1
End If
Next 
Me.txt_count = count

I'm probably going about creating this loop wrong. But any help would be appreciated.
 
So the names of the controls are.. txt_name_1 to txt_name_5 if that is the case, when do you wish for this calculation to be performed?
Code:
Public Function returnCount() As Integer
    Dim iCtr As Integer, nonNull As Integer
    For i = 1 To 5
        If Len(Me.Controls("txt_name_" & i) & vbNullString) <> 0 Then nonNull = nonNull+1
    Next
End Function
This should loop through the 5 controls and return the total Number of Non null controls.. To set this in the Unbound control, you could use..
Code:
= returnCount()
 
Well, they actually are:

txt_name
txt_name_1
txt_name_2
txt_name_3
txt_name_4
txt_name_5

I can see that the first text box will be a problem - but I'll fix that my end.

I will be performing the calculation in On Current, so that regardless of the record it will count how many textboxes are, or are not filled in. So I'll most probably call the sub. Or probably best, use your method and add it to the control source of the unbound text box.
 
So if this is the case,
txt_name
txt_name_1
txt_name_2
txt_name_3
txt_name_4
txt_name_5
you can change the code as..
Code:
Public Function returnCount() As Integer
    Dim iCtr As Integer, nonNull As Integer
    [B]If Len(Me.Controls("txt_name") & vbNullString) <> 0 Then nonNull = nonNull + 1[/B]
    For i = 1 To 5
        If Len(Me.Controls("txt_name_" & i) & vbNullString) <> 0 Then nonNull = nonNull + 1
    Next
    [B]returnCount = nonNull[/B]
End Function
 

Users who are viewing this thread

Back
Top Bottom