IsText?

G

Guest

Hi! I am working on a form and am a little stuck. I would like it if when the
form was opened an input box appears asking my user to enter a PHCO Class.
The PHCO Classes are letters, A, B, C. I have done this sort of code before
to enter quarters. I know that the "IsNumeric" is holding me up because I am
trying to enter letters. Is there an "IsText" to only allow the user to enter
text? Or do I need a new code? Help would be appreciated!

Private Sub Form_Open(Cancel As Integer)
Dim strDefault As String
Dim fGotDefault As Boolean
Do
strDefault = InputBox("Enter the new PHCO Class:")
If Len(strDefault) = 0 Then
fGotDefault = True
Else
If IsNumeric(strDefault) Then
fGotDefault = True
Else
MsgBox "That's not a valid class!"
End If
End If
Loop Until fGotDefault

If Len(strDefault) > 0 Then
Me!PHCOClass.DefaultValue = strDefault
End If
End Sub
 
D

Dirk Goldgar

Tandy said:
Hi! I am working on a form and am a little stuck. I would like it if
when the form was opened an input box appears asking my user to enter
a PHCO Class. The PHCO Classes are letters, A, B, C. I have done this
sort of code before to enter quarters. I know that the "IsNumeric" is
holding me up because I am trying to enter letters. Is there an
"IsText" to only allow the user to enter text? Or do I need a new
code? Help would be appreciated!

Private Sub Form_Open(Cancel As Integer)
Dim strDefault As String
Dim fGotDefault As Boolean
Do
strDefault = InputBox("Enter the new PHCO Class:")
If Len(strDefault) = 0 Then
fGotDefault = True
Else
If IsNumeric(strDefault) Then
fGotDefault = True
Else
MsgBox "That's not a valid class!"
End If
End If
Loop Until fGotDefault

If Len(strDefault) > 0 Then
Me!PHCOClass.DefaultValue = strDefault
End If
End Sub

See if this does what you want. I made two changes, one replacing the
IsNumeric() test, and one assigning the default value.

'----- start of revised code -----
Private Sub Form_Open(Cancel As Integer)

Dim strDefault As String
Dim fGotDefault As Boolean

Do
strDefault = InputBox("Enter the new PHCO Class:")
If Len(strDefault) = 0 Then
fGotDefault = True
Else
If strDefault Like "[A-Z]" Then
fGotDefault = True
Else
MsgBox "That's not a valid class!"
End If
End If
Loop Until fGotDefault

If Len(strDefault) > 0 Then
Me!PHCOClass.DefaultValue = Chr(34) & strDefault & Chr(34)
End If

End Sub
'----- end of revised code -----

If you want the value entered to be forced to upper case, you might
change the DefaultValue assignment to

Me!PHCOClass.DefaultValue = _
Chr(34) & UCase(strDefault) & Chr(34)
 
G

Guest

Private Sub Form_Open(Cancel As Integer)
Dim strDefault As String

Do While True
strDefault = InputBox("Enter the new PHCO Class:")
If (Len(strDefault) = 1 And strDefault >= "A" And _
strDefault <= "C") Or (Len(strDefault) = 0) Then
Exit Do
End If
MsgBox "That's not a valid class!"
Loop
If Len(strDefault) > 0 Then
Me!PHCOClass.DefaultValue = strDefault
End If
End Sub
 
P

Paul Overway

TOP:

strDefault = UCase(InputBox("Enter the new PHCO Class:"))

If Instr("ABC",strDefault) = 0 Then
MsgBox "That's not a valid class!"
Goto TOP
End if
 
P

Paul Overway

Better yet...

TOP:

strDefault = UCase(InputBox("Enter the new PHCO Class:"))

If Instr("ABC",strDefault) = 0 Or Len(strDefault) <> 1 Then
MsgBox "That's not a valid class!"
Goto TOP
End if
 
G

Guest

Good approach, Paul. Two problems If you look at her original code, she
allows a zero length to be entered.
If Len(strDefault) > 0 Then
Me!PHCOClass.DefaultValue = strDefault
End If
Don't know what she does if nothing was entered.
The other is the use of the Go To.
Go To = Bad
Loop = Good
 
P

Paul Overway

I have no problem with judicous use of Goto....but if it is used to write
spaghetti that is a different story. It is the context that matters. In
this case its use is very direct, easily understood, and not buried in 20
nested loops. I could have just as easily used err.raise or a loop....but
the result is the same and neither is less readable or understandable. I'm
fairly picky about coding practices, but wouldn't make a blanket statement
that Goto is always bad.

As for allowing the zero length, I'm not sure of her intent. Actually,
there are other issues with her original code beyond that, i.e., does case
matter? How would one cancel out of the loop altogether?, etc. I'm
assuming she'll figure that out.
 
G

Guest

I will agree with you on the problems in her code. I also agree in
philosophy with your statement on the judicous use of Goto. It did get it's
reputation from being abused. There are times when it is the cleanest and
easiest to read. "Easiest To Read" is very important to me.
I will also grant that coding is a matter of style and preference. My
personal rules on the Goto are quite simple:
1. Never Go Up
2. Only use it to go to an Exit label when skipping the code between the
Goto and the Exit label would create undue nesting or hard to read code.

Perhaps my earlier statement was rigid, but giving an example with a Goto to
a novice might lead to bad habits.

Again, it is personal preference (or shop standards), but I would always use
a Do Loop rather than a Goto as you did in your example. As you say, there
needs to be a way out.
If programming were not a creative exercise, I probably would not do it.

Enjoy seeing your posts, you always have good ideas.
 

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

Similar Threads


Top