Solved

# How to obtain the fist word from a given string

Posted on 2004-09-05
165 Views
How can I programmitcally get the first word from a string if the format is the following?
Genesis: Chapter: 001
Exodus: Chapter: 001

I only need the first word, for example: Genesis or Exodus etc.

Node.Text is as follows: Genesis: Chapter: 001

Here is the code I'm using, it doesn't work and I've been working with this problem for hours.
a% = Len(Trim(Node.Text))
b% = 1
Do Until a% = 0
If Left\$(Node.Text, b%) = ":" Then
GoTo ExitDo
Else
Book_ID = Left\$(Node.Text, b%)
End If
a% = a% - 1
b% = b% + 1
Loop

ExitDo:

Hint: I only need the string prior to the first colon.
0
Question by:mcdonald_g
• 4
• 2

LVL 18

Expert Comment

ID: 11986418
Private Sub Command1_Click()
Dim MyArr As Variant
Dim MyString As String
MyString = Node.Text
MyString = Replace(MyString, ":", " ")
MyArr = Split(MyString, " ")
MsgBox MyArr(0)
End Sub
0

LVL 8

Expert Comment

ID: 11986421
Use the split function

dim strData

strData = split ("Genesis: Chapter: 001",":")

msgbox "First word is: " strdata(0)

0

LVL 18

Accepted Solution

JR2003 earned 125 total points
ID: 11986429
If you know it's always a text in front of the first colon it's even more straigh forward:

Private Sub Command1_Click()
Dim MyArr As Variant
MyArr = Split(Node.Text, ":")
MsgBox MyArr(0)
End Sub
0

LVL 8

Expert Comment

ID: 11986460
Even easier than that....

a% = Len(Trim(Node.Text))
strData = split (Node.Text,":")
a% = strData(0)
0

LVL 8

Expert Comment

ID: 11986465
Sorry, goofed on the variable

a% = Len(Trim(Node.Text))
strData = split (a%,":")
a% = strData(0)
0

LVL 8

Expert Comment

ID: 11986573
hmmm...
0

## Featured Post

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…