Is a textbox empty?

M

Mark King

Hi all,

Hope somebody can help me with my dilemma!

I have 12 textboxes on a userform and when these are completed and the user
clicks on a button, the information is then transfered into the word
document in the relevant places.

What I would like to, is when the user clicks the button I want it to check
to see if any of the boxes are empty before transfereing all the data into
the document, and if they are then I would like to display a message saying
please complete all textboxes before clicking the submit button.

So basically just a short simply routine that checks to see if any textboxes
are empty when a command button is clicked.

Many thanks in advance

Mark
 
M

Malcolm Smith

Mark

This is one way of doing this. In my test example I have the following:
some code in the This_Document object and a form called frmTest.

The form has a number of text boxes. The number is not important and the
names are not important (well, apart from good coding styles, etc):


The code in the This_Document which creates the dialog box when a document
is created on the template is as follows:


Option Explicit

Private Sub Document_New()

Dim ofrm As frmTest

Set ofrm = New frmTest
ofrm.Show vbModal
'---------------
Unload ofrm
Set ofrm = Nothing

End Sub



All straightforward stuff. The code which does what you want is behind
the Click event of the cmdOK button. This is the code in its interety
behind the form class:


Option Explicit

Private Sub cmdCancel_Click()

Me.Hide

End Sub


Private Sub cmdOK_Click()

Dim oControl As Control
Dim oTextBox As TextBox
Dim bIsInvalidData As Boolean


bIsInvalidData = False

For Each oControl In Me.Controls
If TypeName(oControl) = "TextBox" Then
Set oTextBox = oControl
If Len(oTextBox.Text) = 0 Then
If Not bIsInvalidData Then
oControl.SetFocus
bIsInvalidData = True
End If
End If
End If
Next oControl

If Not bIsInvalidData Then
Me.Hide
End If

End Sub



How this works is as follows. I loop through each of the controls in the
Me collection. Me is a pointer which points to the dialog box object. So
, in this case it will rattle around all of the text boxes, labels and
buttons.

Now I check to see if the oControl object is a TextBox. This is what the
TypeName() function does.

If the object is a TextBox then I set a pointer, of type TextBox, pointing
to the oControl. I prefer to do it this way because I know at this stage
that it's a TextBox control rather than a generic Control object which I
am dealing with.

All then I do is to make sure that the length of the text property (one
could always do:

If Len(Trim$(oTextBox.Text)) = 0 Then

to stop smart alecs putting in a space to pass the test.

And I think that the rest is rather straightforward.


Does this help?
Malc
www.dragondrop.com
 
J

Jonathan West

Hi Mark

You can include this at the start of the code for the Click event of the
submit button

Dim oControl as Control
For Each oControl in Me.Controls
If TypeOf oControl Is MsForms.TextBox Then
If Len(oControl.Text) = 0 Then
MsgBox "You haven't entered text in all the textboxes." & vbCr &
_
"Go back and finish the job."
Exit Sub
End If
End If
Next oControl

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top