• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 827
  • Last Modified:

VBA - Count how many textboxes aren't blank

I've created a form in Word and on it I have 8 text boxes for user input. However not all the text boxes will be used all the time so how do I count how many have been used?

Any coding examples would be very much appreciated.

Sorry but 95 points is all I have.
Sorry tag should be Word 2007
0
daleoran
Asked:
daleoran
  • 3
  • 3
  • 2
2 Solutions
 
XconeCommented:
Can you specify wether you use formfields within a document. Or do you have a form (as in Dialog)?

You can access formfields collection with VBA "ActiveDocument.FormFields"
You can access controls on a form with VBA "<FormName>.Controls"

Just use a For Each loop on those, and perform additional checks if you're dealing with textboxes. Then perform the check if it contains text.

I can give you a code example once you specify the type of editbox you're talking about.
0
 
daleoranAuthor Commented:
Sorry it's a form (as in dialog) called from a macro using InputForm.Show vbModal
0
 
GrahamSkanCommented:
This code will run from a button on the UserForm
Private Sub CommandButton1_Click()
    Dim t As Integer
    Dim ctrl As Control
    
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is TextBox Then
            If ctrl.Text = "" Then
                t = t + 1
            End If
        End If
    Next ctrl
    MsgBox "There " & IIf(t = 1, "is ", "are ") & t & " blank text box" & IIf(t = 1, "", "es") & " on this form"
End Sub

Open in new window

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
daleoranAuthor Commented:
Works a treat Graham but I have worded the question wrong - points are yours but hope you can help just a wee bit more.

What should have said was that I have a group of text boxes on a form which has around 20 text boxes in total. How do I count the unused boxes from the group of 8.
0
 
XconeCommented:
If they're grouped using a groupbox or other container, you could replace the line:

For Each ctrl In Me.Controls

with

For Each ctrl in NameOfGroupBox.Controls
0
 
GrahamSkanCommented:
How are they grouped? I don't know what the gropubox that XCone refers to is, but if they are in a Frame, the syntax is the same:

For Each ctrl in Me.Frame1.Controls
0
 
XconeCommented:
I meant Frame, sorry about that. GroupBox and Frame are the same thing. I use them a lot in another environment where it's called a GroupBox.
0
 
daleoranAuthor Commented:
Hope you don't mind the split with Xcone. He did suggest the code first even if the name was slightly wrong.

Many thanks for all the help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now