[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

VBA - Count how many textboxes aren't blank

Posted on 2009-05-20
8
Medium Priority
?
807 Views
Last Modified: 2012-05-07
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
Comment
Question by:daleoran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 4

Expert Comment

by:Xcone
ID: 24431342
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
 
LVL 10

Author Comment

by:daleoran
ID: 24431400
Sorry it's a form (as in dialog) called from a macro using InputForm.Show vbModal
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 400 total points
ID: 24433545
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
On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

 
LVL 10

Author Comment

by:daleoran
ID: 24472093
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
 
LVL 4

Assisted Solution

by:Xcone
Xcone earned 100 total points
ID: 24475222
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
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 24475605
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
 
LVL 4

Expert Comment

by:Xcone
ID: 24475938
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
 
LVL 10

Author Comment

by:daleoran
ID: 24484937
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

649 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