Solved

how can i get a part of an unfixed variable length

Posted on 2003-11-14
14
188 Views
Last Modified: 2010-05-01
Hi,

Dim strA as string
Dim strB as string

StrA = "Name   Toni2002"

This variable consists of 3 parts: 1) a word    2) space   3) a word  ,  these words have  not fixed length but the space which inbetween is fixed  in any value of strA

How can I put the 1st part (Name) or the 3rd part (toni2002)  alone in another variable
I mean  how can I let

strB = "Name"
OR
strB = "toni2002"

Thanx
0
Comment
Question by:toni2002
  • 5
  • 4
  • 3
  • +1
14 Comments
 
LVL 8

Assisted Solution

by:Rog
Rog earned 25 total points
ID: 9749331
What I would do is search for "Name"

iNameBeg = instr(1,strA,"Name")  'Gives you beginning of Name String.

Now you can then do another instr function to find the end of Toni2002

iName2Beg = instr(inameBeg,Stra,"&")

Now if you do a

sName = mid(stra,iNameBeg+4,iname2beg - inamebeg)  'This will give you Toni2002

Rog
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9749334

i think
dim lstart as long
dim lend as long

lstart=instr(1,strA," ",vbtextcompare)
lend=instr(lstart,strA,"&",vbtextcompare

msgbox mid$(1,strA,lstart) & mid$(lend,strA)
0
 
LVL 8

Expert Comment

by:Rog
ID: 9749348
Sorry some problem with my Browser I saw &...

sName = mid(stra,4,len(stra) - 4)  'This will give you Toni2002

sNmae = trim(sName)

This will get rid of spaces.

Rog
0
 
LVL 16

Accepted Solution

by:
Richie_Simonetti earned 50 total points
ID: 9749351
more accurate (;)
Sub test()
Dim strA As String
Dim strB As String

strA = "“Name   Toni2002&#8221"

Dim lstart As Long
Dim lend As Long

lstart = InStr(1, strA, " ", vbTextCompare) - 1
lend = InStr(lstart, strA, "&", vbTextCompare)

MsgBox Mid$(strA, 1, lstart) & Mid$(strA, lend)

End Sub
0
 
LVL 44

Assisted Solution

by:Arthur_Wood
Arthur_Wood earned 50 total points
ID: 9749378
using VB 6, you can use the Split Function:

Dim Array() as String
Dim Source as String
Source = "Name   Toni2002"
Array= Split(Source, " ")

Array(0) ===> "Name"
Array(1) ===> "Toni2002"

AW
0
 
LVL 8

Expert Comment

by:Rog
ID: 9749416
Last example would not work if the actual name was "Joe Schmo 2002"

You would then have to look at the uBound(Array) then.

Rog
0
 
LVL 44

Expert Comment

by:Arthur_Wood
ID: 9749569
and  "Joe Schmo 2002"
 is NOT in agreement with the original question, which stated:
1) a word    2) space   3) a word  

not 1) a phrase 2) space 3) a word

If the original statement was imprecise, then ALL of the examples suggested so far will fail, for the same reason.

AW
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 8

Expert Comment

by:Rog
ID: 9749607
Author,

I wasn't trying to start anything at all.  An all the suggestions would not fail if you looked closely.  Mine does not take the space into account in the Name "Joe Schmo 2002".

Sorry...

Your code was very good, I was just pointing out that sometimes peopole put spaces in names.

Rog
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9749660
Am i crazy or main Q was edited??
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9749695
The problem here is that you cannot simply search for the first space in the string.

Rog pointed out that you can have a name with spaces in it like "Joe Schmo 2002".

We don't know if this may also be true of the first part, the Field Name, which in this case happens to be "Name".  If one does, then any scheme searching for spaces will be futile.

I believe that Rog has had the best approach so far.  Although, we would have to start with a known list of Field Names to search for.

toni2002... Do you know all the possible values that the first word may contain, or whether they will ever contain spaces?

Idle_Mind
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9750327
To me, in any case several spaces should be a problem since you are searching for the first one only and stop on it, so Split should fails due to that ,but Instr function would works OK.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9750374
It's a moot point since toni2002 has accepted an answer.

I was simply pointing out that no combination of Split() and/or Instr() will work reliably if either your Field Name or your Field Value has spaces in it.

Without knowing whether spaces are even allowed in the Field Names/Values, discussion is useless.

Having a list of known field names would allow you to determine at what point in the string the field value begins without question.

Enough said,

Idle_Mind
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 9750567
"It's a moot point since toni2002 has accepted an answer."

I don't mind if answer was accepted or not (even if it would be my comment or not)
I like to give some clarification to what i think is useful to round an idea.
But, again, as long as stated,
1) a word    2) space   3) a word  

i don't care how many spaces would be in 3) a word, instr function will do the job.
Problem only will rise if 1) a word is other than Name as provided (like First Name, in example)
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 9750625
Exactly.  With the given values, your code executes flawlessly.  I don't in any way mean to imply that it wouldn't.

The way the original question is worded though, it leads me to believe that there are other possible values for the "word" in position 1 and toni2002 was just asking the question to get a generic answer to the problem.

I like to develop algorithms with the most generic case in mind, and was tyring to get more information on the problem from toni2002 to no avail.

Idle_Mind
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

911 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

17 Experts available now in Live!

Get 1:1 Help Now