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

Whitespace

Is there any way to check a whitespace character in vb? Thanks..
0
veraps
Asked:
veraps
  • 9
  • 5
  • 2
  • +8
1 Solution
 
casassusCommented:
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.
0
 
WolfgangKoenigCommented:
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
0
 
WolfgangKoenigCommented:
Miscellaneous Constants
   

The following constants are defined in the Visual Basic for Applications type library and can be used anywhere in your code in place of the actual values:

Constant Equivalent Description
vbCrLf Chr(13) + Chr(10) Carriage return?linefeed combination
vbCr Chr(13) Carriage return character
vbLf Chr(10) Linefeed character
vbNewLine Chr(13) + Chr(10)  Platform-specific new line character; whichever is appropriate for current platform
vbNullChar Chr(0) Character having value 0
vbNullString String having value 0 Not the same as a zero-length string (""); used for calling external procedures
vbObjectError -2147221504 User-defined error numbers should be greater than this value. For example:
Err.Raise Number = vbObjectError + 1000
vbTab Chr(9) Tab character
vbBack Chr(8) Backspace character
vbFormFeed Chr(12) Not useful in Microsoft Windows
vbVerticalTab Chr(11) Not useful in Microsoft Windows

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
verapsAuthor Commented:
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 ..
0
 
WolfgangKoenigCommented:
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


0
 
verapsAuthor Commented:
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..
0
 
WolfgangKoenigCommented:
See -> Miscellaneous Constants
 
WoK
0
 
WolfgangKoenigCommented:
Also:

Dim Inputstr As String

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

(Eleminates Backspace)
WoK
0
 
verapsAuthor Commented:
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..
0
 
WolfgangKoenigCommented:
Practically you can check:

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

WoK
0
 
WolfgangKoenigCommented:
Sorry i must go for lunch ;)

Good coding
WoK
0
 
BozzoCageCommented:
Use some conversion function to check for valid input, like Val or CInt.
0
 
verapsAuthor Commented:
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..
0
 
verapsAuthor Commented:
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..
0
 
Valliappan ANSenior Tech ConsultantCommented:
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.
0
 
andysalihCommented:
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
0
 
TrueDrakeCommented:
Hi,

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

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

Enjoy
0
 
Valliappan ANSenior Tech ConsultantCommented:
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.
0
 
WolfgangKoenigCommented:
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
0
 
WolfgangKoenigCommented:
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
0
 
ArkCommented:
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
0
 
PreeceCommented:
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
0
 
DanRollinsCommented:
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
0
 
NetminderCommented:
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 9
  • 5
  • 2
  • +8
Tackle projects and never again get stuck behind a technical roadblock.
Join Now