Link to home
Start Free TrialLog in
Avatar of veraps
veraps

asked on

Whitespace

Is there any way to check a whitespace character in vb? Thanks..
Avatar of casassus
casassus

When you add your data in your collection, you can put a key.
With this idea, you can find quickly your data, if it does not exist, you have an error.
Here is a complete example:

Private Sub Form_Load()
Dim TestItem As Variant
Dim C As Collection
Dim A As Integer

A = 3
Set C = New Collection
C.Add 1
C.Add 2
C.Add 3
C.Add 45


For Each TestItem In C
If TestItem = A Then MsgBox ("Item found !")
Next TestItem

End Sub
ASKER CERTIFIED SOLUTION
Avatar of WolfgangKoenig
WolfgangKoenig

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of veraps

ASKER

Hm, actually i have this case:
I use a DHTML application, I need to make a validation for an input of a field. This field has to be numeric. I put the validation in the OnKeypress method, but I don't want it to show an error message when I press a white space (escape, backspace, carriagereturn etc.)
Thanks ..
Another is only:

Dim A as String

if A ="" Then do somthing

or

if A =" " Then do somthing

sometime this is needed:

if A ="'" Then do somthing

WoK


Avatar of veraps

ASKER

is there a way that I can check them practically, not enumerating them like:

if a = vbBack and a = vbVerticalTab and a = FormFeed ....

Is there a function so I just make this code:

If Not IsWhiteSpace(a)

thanks..
See -> Miscellaneous Constants
 
WoK
Also:

Dim Inputstr As String

if Mid(Inputstr,1,1) = vbBack  Then
  Inputstr = Mid(Inputtstr,2)
Endif

(Eleminates Backspace)
WoK
Avatar of veraps

ASKER

is there a way that I can check them practically, not enumerating them like:

if a = vbBack and a = vbVerticalTab and a = FormFeed ....

Is there a function so I just make this code:

If Not IsWhiteSpace(a)

thanks..
Practically you can check:

If Inputschar < Chr(32) Then
  MsgBox("Whitechar")
End if

WoK
Sorry i must go for lunch ;)

Good coding
WoK
Use some conversion function to check for valid input, like Val or CInt.
Avatar of veraps

ASKER

is there a way that I can check them practically, not enumerating them like:

if a = vbBack and a = vbVerticalTab and a = FormFeed ....

Is there a function so I just make this code:

If Not IsWhiteSpace(a)

thanks..
Avatar of veraps

ASKER

is there a way that I can check them practically, not enumerating them like:

if a = vbBack and a = vbVerticalTab and a = FormFeed ....

Is there a function so I just make this code:

If Not IsWhiteSpace(a)

thanks..
How about this:
If Val(InputStr & Chr(keypressed)) <> InputStr & Chr(keypressed) Then
    'disallow the key pressed
End If

'This will disallow the alpha-character keys or other character pressed.

I am not sure, if you could do this in DHTML Application.
If Inputschar < Chr(32) Then
 MsgBox("Whitechar")
End if

is not quite correct, check ascii 255 is a whitechar character to check against, instead of <space> ascii 32
Hi,

You'll find the IsNumeric function well adapted for validation:

If IsNumeric(Text1.Text) Then ....

Enjoy
TrueDrake, you are right. But IsNumeric I hope, cannot be used in KeyPress event. If used, then again may need to check for backspace, Delete etc.
Dim Inputchar As String

Inputchar ="a"

If Inputschar < Chr(32) Then
 MsgBox("This is a whitespace")
Else
 MsgBox("This is not a whitespace")
End if

This is the best solution to filter out white spaces
WoK
Numbers from 0 ? 31 are the same as standard, nonprintable ASCII codes. For example, Chr(10) returns a linefeed character. The normal range for charcode is 0 ? 255. However, on DBCS systems, the actual range for charcode is -32768 to 65535.

Here is your function:
---------------------------------------------------------

Function IsWhiteSpace(Charcode As Long) As Boolean
  If Charcode < 32 Then
    IsWhiteSpace = True
  Else
    IsWhiteSpace = False
  End If
End Function

Private Sub Form_Load()
Dim Test As String

Test = "a"
If IsWhiteSpace(Asc(Test)) Then
   MsgBox ("Yeah!")
Else
   MsgBox ("No!")
End If

Test = vbLf
If IsWhiteSpace(Asc(Test)) Then
   MsgBox ("Yeah!")
Else
   MsgBox ("No!")
End If

End Sub


This was your solution
Best regards
WoK
Avatar of Ark
Hi
No such ability in vb, only enumeration :(. Not all WhiteKeys ASCII < 32 (Ins,Del,F1-12 keys,Print Screen etc). IMHO more better to use not IF, but Select Case statement:

Here is my routine to get keyname from virtual key:

Private Function KeyName(ByVal key As Long) As String
  Select Case key
     Case vbKeyA To vbKeyZ, vbKey0 To vbKey9
          KeyName = Chr(key) 'Not WhiteChar
     Case vbKeyF1 To vbKeyF16
          KeyName = "F" & CStr(key - vbKeyF1 + 1)
     Case vbKeyCancel:   KeyName = "CANCEL"
     Case vbKeyBack:     KeyName = "BACKSPACE"
     Case vbKeyTab:      KeyName = "TAB"
     Case vbKeyClear:    KeyName = "CLEAR"
     Case vbKeyReturn:   KeyName = "ENTER"
     Case vbKeyShift:    KeyName = "SHIFT"
     Case vbKeyControl:  KeyName = "CTRL"
     Case vbKeyMenu:     KeyName = "MENU"
     Case vbKeyPause:    KeyName = "PAUSE"
     Case vbKeyCapital:  KeyName = "CAPS LOCK"
     Case vbKeyEscape:   KeyName = "ESC"
     Case vbKeySpace:    KeyName = "SPACEBAR"
     Case vbKeyPageUp:   KeyName = "PAGE UP"
     Case vbKeyPageDown: KeyName = "PAGE DOWN"
     Case vbKeyEnd:      KeyName = "END"
     Case vbKeyHome:     KeyName = "HOME"
     Case vbKeyLeft:     KeyName = "LEFT ARROW"
     Case vbKeyUp:       KeyName = "UP ARROW"
     Case vbKeyRight:    KeyName = "RIGHT ARROW"
     Case vbKeyDown:     KeyName = "DOWN ARROW"
     Case vbKeySelect:   KeyName = "SELECT"
     Case vbKeyPrint:    KeyName = "PRINT SCREEN"
     Case vbKeyExecute:  KeyName = "EXECUTE"
     Case vbKeySnapshot: KeyName = "SNAPSHOT"
     Case vbKeyInsert:   KeyName = "INS"
     Case vbKeyDelete:   KeyName = "DEL"
     Case vbKeyHelp:     KeyName = "HELP"
     Case vbKeyNumlock:  KeyName = "NUM LOCK"
     Case Else:          KeyName = "Virtual Key " & CStr(key)
  End Select
End Function

You can easy modify it for your purpose:

Private Function IsWhiteChar(ByVal key As Long) As Boolean
  Select Case key
     Case vbKey0 To vbKey9, vbKeyF1 To vbKeyF16, vbKeyCancel, vbKeyBack, vbKeyTab, vbKeyClear,vbKeyReturn, vbKeyShift, vbKeyControl,vbKeyMenu, vbKeyCapital 'etc
          'Allowable keys, do your staff (or return false)
     Case vbKeyA To vbKeyZ,vbKeySpace, etc
          'Disallowable keys, return true
  End Select
End Function

Note that KeyPress event return ASCII code, while you need virtual key. You have to use KeyDown/KeyUp events for this

Cheers
Maybe this is too simplistic, or I'm not understanding the question, but would this help:


If Not trim(a) = "" then


or


if InStr(1, sTemp, [character you are seeking]) > 0 then



Preece
Hi veraps,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept WolfgangKoenig's comment(s) as an answer.

veraps, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept this comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer
Per recommendation, force-accepted.

Netminder
CS Moderator