Solved

Whitespace

Posted on 2001-08-03
24
1,235 Views
Last Modified: 2007-11-27
Is there any way to check a whitespace character in vb? Thanks..
0
Comment
Question by:veraps
  • 9
  • 5
  • 2
  • +8
24 Comments
 
LVL 3

Expert Comment

by:casassus
ID: 6348108
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
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6348111
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
 
LVL 4

Accepted Solution

by:
WolfgangKoenig earned 200 total points
ID: 6348116
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
 

Author Comment

by:veraps
ID: 6348117
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
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6348119
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
 

Author Comment

by:veraps
ID: 6348120
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
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6348121
See -> Miscellaneous Constants
 
WoK
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6348131
Also:

Dim Inputstr As String

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

(Eleminates Backspace)
WoK
0
 

Author Comment

by:veraps
ID: 6348138
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
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6348145
Practically you can check:

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

WoK
0
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6348150
Sorry i must go for lunch ;)

Good coding
WoK
0
 
LVL 2

Expert Comment

by:BozzoCage
ID: 6348152
Use some conversion function to check for valid input, like Val or CInt.
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:veraps
ID: 6348155
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
 

Author Comment

by:veraps
ID: 6348160
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
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6348193
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
 
LVL 3

Expert Comment

by:andysalih
ID: 6348274
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
 
LVL 2

Expert Comment

by:TrueDrake
ID: 6348421
Hi,

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

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

Enjoy
0
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 6348525
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
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6348542
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
 
LVL 4

Expert Comment

by:WolfgangKoenig
ID: 6348610
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
 
LVL 27

Expert Comment

by:Ark
ID: 6350982
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
 
LVL 12

Expert Comment

by:Preece
ID: 6361637
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
 
LVL 49

Expert Comment

by:DanRollins
ID: 7198441
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
 
LVL 5

Expert Comment

by:Netminder
ID: 7213222
Per recommendation, force-accepted.

Netminder
CS Moderator
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now