How to find the following string..?

sdesar
sdesar used Ask the Experts™
on
In VB I have a string like this-

"dn  bob"
I want the result to have all the data upto the space ie-
"dn"

How can I get this?
At present I am using Trim.. and it gets only 2 chars... I want to make it more felixble and have to parse everyting upto the space...
Left(LCase(Trim("" & Me.Controls("Col" & I))), 2)

Any suggestions..
Thanks
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
Try using:
Left(LCase(Trim("" & Me.Controls("Col" & I))), InStr(1,Trim(Me.Controls("Col" & I)," "))
Commented:
hi sdesar

you can use the "instr function" to do it as follows

private sub Command1_click()
dim x as integer
dim findstring as string
dim resString as string

findstring = "dn bob"
x = instr(findstring," ") 'Finding the position of " " in the string

resstring= left(findstring,x)

end sub

Author

Commented:
PreStaul ..
I tried this -
Select Case Left(LCase(Trim("" & Me.Controls("Col" & I))), InStr(1, Trim(Me.Controls("Col" & I), " ")))

At first I got an syntax error missing ) .. I added ) at the end .
Then I got an error - invalid number of arguments...

Any suggestions
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
BTW.. I like vb buff's suggestion!
Thanks

Commented:
(copied from duplicate question):

Also, if you're trying to remove duplicate spacing, you can try the Replace command that was added in VB6:

const vbSpace as string = " "
strSomething = replace(strSomething, vbSpace & vbSpace, vbSpace)

Commented:
try this

Dim X, FindStr As String
Dim Pos As Long

X = "dn  bob"
Pos = InStr(1, X, " ")
FindStr = Mid(X, 1, Pos - 1)
MsgBox FindStr

Commented:
Here's another way...

MsgBox Split("dn bob", " ")(0)

That will create an array out of the string using the space as the delimiter, adding the (0) to the end will return the first item in the array.  So for this example you would get a messagebox that said "dn".

For your current needs it would go something like this...

    Select Case Split(Trim("" & Me.Controls("Col" & I)), " ")


Take care,
Shannon

Commented:
Sorry, forgot to add the (0) :)

   Select Case Split(Trim("" & Me.Controls("Col" & I)), " ")(0)

Author

Commented:
Hi vbBuff...
I implemented your suggestion .. but when I run the code, I always get the else case .. ie RED in my report...
What wrong with the logic..

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

  On Error GoTo done
  Dim LNG_COLOR As Long
  Dim I As Long

Dim findstring As String
  Dim resString As String

 
  'Me.Section(acDetail).BackColor = vbWhite
  Me.Controls("PTS_X_SHOT_STATUS_STR_VALUE").BackColor = vbWhite
  For I = It - 1 To 0 Step -1
    Me.Controls("Col" & I).BorderWidth = 0
    findstring = Me.Controls("Col" & I)
    x = InStr(findstring, " ") 'Finding the position of " " in the string
    resString = Left(findstring, x)
  '  Select Case LCase(Trim("" & Me.Controls("Col" & I)))
  '  Select Case Left(LCase(Trim("" & Me.Controls("Col" & I))), 2)
    Select Case resString
    Case resString = "d", "n/a"
      Me.Controls("Col" & I).BackColor.bkGreen
    Case resString = "f"
        Me.Controls("Col" & I).BackColor.bkPink
    Case resString = "ip"
        'Me.ForeColor = bkBlue
        'Me.Controls("Col" & I).BackColor = bkBlue
         Me.Controls("Col" & I).BackColor.bkBlue
    Case Else
      Me.Controls("Col" & I).BackColor = bkRed
     
    End Select
    'Me.Controls("Col" & I).BackColor = LNG_COLOR
  Next I
  'if ON HOLD Grey it
  If Me.PK_X_SHOT_STATUS = "E034080020C905F8" Then
   Me.Controls("PTS_X_SHOT_STATUS_STR_VALUE").BackColor = lng_shading
   '  Me.Section(acDetail).BackColor = lng_shading
  End If

Author

Commented:
Hi vbBuff...
I implemented your suggestion .. but when I run the code, I always get the else case .. ie RED in my report...
What wrong with the logic..

Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)

  On Error GoTo done
  Dim LNG_COLOR As Long
  Dim I As Long

Dim findstring As String
  Dim resString As String

 
  'Me.Section(acDetail).BackColor = vbWhite
  Me.Controls("PTS_X_SHOT_STATUS_STR_VALUE").BackColor = vbWhite
  For I = It - 1 To 0 Step -1
    Me.Controls("Col" & I).BorderWidth = 0
    findstring = Me.Controls("Col" & I)
    x = InStr(findstring, " ") 'Finding the position of " " in the string
    resString = Left(findstring, x)
  '  Select Case LCase(Trim("" & Me.Controls("Col" & I)))
  '  Select Case Left(LCase(Trim("" & Me.Controls("Col" & I))), 2)
    Select Case resString
    Case resString = "d", "n/a"
      Me.Controls("Col" & I).BackColor.bkGreen
    Case resString = "f"
        Me.Controls("Col" & I).BackColor.bkPink
    Case resString = "ip"
        'Me.ForeColor = bkBlue
        'Me.Controls("Col" & I).BackColor = bkBlue
         Me.Controls("Col" & I).BackColor.bkBlue
    Case Else
      Me.Controls("Col" & I).BackColor = bkRed
     
    End Select
    'Me.Controls("Col" & I).BackColor = LNG_COLOR
  Next I
  'if ON HOLD Grey it
  If Me.PK_X_SHOT_STATUS = "E034080020C905F8" Then
   Me.Controls("PTS_X_SHOT_STATUS_STR_VALUE").BackColor = lng_shading
   '  Me.Section(acDetail).BackColor = lng_shading
  End If

Commented:
Try this to make sure there are no leading spaces:


   findstring = Trim(Me.Controls("Col" & I))
   x = InStr(1,findstring, " ") 'Finding the position of " " in the string
   resString = Left(findstring, x)

   Select Case resString
It's not a leading space but a trailing one


findstring = "dn  bob"

x = InStr(1,findstring, " ") 'Finding the position of " " in the string
'' This will = 3

  resString = Left(findstring, x)

'' resString wil = "dn " ' with a trailing space

try this

    resString = Trim(Left(findstring, x))

or

    resString = Trim(Left(findstring, x - 1))

in case you are not looking for a space(chr(32))

Commented:
Hornet has it right there with x-1.

Author

Commented:
This is the code that I currently have.
It seems to get to case "ip"- green and the case Else - blue

The following is a list of possible strings that I have in the table that are not being recognized--
ip davids
ns davids
d davids

ONLY  'ip' case is working and Else case...

HELP PLEASE!!!

'Me.Section(acDetail).BackColor = vbWhite
  Me.Controls("PTS_X_SHOT_STATUS_STR_VALUE").BackColor = vbWhite
  For I = It - 1 To 0 Step -1
    Me.Controls("Col" & I).BorderWidth = 0
    findstring = Me.Controls("Col" & I)
    x = InStr(findstring, " ") 'Finding the position of " " in the string
    resString = Left(findstring, x - 1)
    'Left(InStr(Me.Controls("Col" & I), " "), x - 1) -- I tried to use this code instead of resString.. does not work
    Select Case resString
    Case "d", "n/a"
      Me.Controls("Col" & I).BackColor = bkPink
    Case "ns"
      Me.Controls("Col" & I).BackColor = bkRed
    Case "ip"
      Me.Controls("Col" & I).BackColor = bkGreen
    Case Else
     Me.Controls("Col" & I).BackColor = bkBlue
     
    End Select
   
  Next I
  'if ON HOLD Grey it
  If Me.PK_X_SHOT_STATUS = "75998CCB17881ED5E034080020C905F8" Then
   Me.Controls("PTS_X_SHOT_STATUS_STR_VALUE").BackColor = lng_shading
   '  Me.Section(acDetail).BackColor = lng_shading
  End If
   
 
done:
    Exit Sub
I copy your code and modified it to work without the controls as follows

ReDim tmp(2)
tmp(0) = "ip davids"
tmp(1) = "ns davids"
tmp(2) = "d davids"

 For I = 2 To 0 Step -1
   findstring = tmp(I)
   x = InStr(findstring, " ") 'Finding the position of " " in the string
   resString = Left(findstring, x - 1)
   Select Case resString
   Case "d", "n/a"
     jl = 0
   Case "ns"
     jl = 1
   Case "ip"
     jl = 2
   Case Else
     jl = 3
   End Select
 
 Next I


then I stepped through it and everything worked fine
Have you stepped through?
What is the value of resString?
dim sUntilSpace as string
sUntilSpace = split(LCase(Trim("" & Me.Controls("Col" & I))),space(1))(0)
msgbox sUntilSpace

Author

Commented:
Thanks for all the help from everyone.
my code works now.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial