Solved

A little variable question :)

Posted on 1998-09-07
11
224 Views
Last Modified: 2012-05-04
Hi!  I am using VB5.

I need to know how to make my program take a look at a variable, and.... if that variable has a cirtain phrase somewhere in it (even in the middle or something), such as Hello! then it'll do something.

Also... I'd like another question answered that has to do with that.  The cirtain catch phrase may be something like (Hello, my name is ????!), and I want it to detect that phrase, and also save to a separate variable the ????.  If you can help, I'd be much apprieciative!  

Thanks a lot!

Both answers are required to get the points, but if you only know one, I'd appriciate if you'd just comment it in, and then  I might give you points separately in another 'fake' question :)
0
Comment
Question by:eversoslightly
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +2
11 Comments
 

Expert Comment

by:DaveD072798
ID: 1433481
Can you accomplish this with setting properties? If you code/program is stored in a Class module or an ActiveX DLL then rather than treat it as a "variable" you can treat it as an "object" with specific user-defined properties. Then, if a property is "Hello" or whatever, you can take appropriate actions from any program that recognizes COM objects, including itself.
0
 
LVL 1

Author Comment

by:eversoslightly
ID: 1433482
I'm really not familiar with ActiveX, and someother things you commented about here.

Could you please explain?

I'd feel more comfortable (with my knowledge, which is at a beginner to intermediate level :) ) if there was a way to search through a variable for the phrase.... and thus to a if-then statement.

If the way you are trying to explain is the only way, which sounds complicated, I'd appriciate if you'd explain it better, at which I would then raise the points, because it would seem like a harder question :)

Thank's a lot.

-EverSo
0
 
LVL 2

Expert Comment

by:cantrell
ID: 1433483
Use the INSTR string function for answer one,

For example;
1)
Dim mypos As Integer
Dim SearchString As String
Dim NextVariable As String

'Define what you want to search for.
SearchString = "hello"

'Look for the search string and make it case INsenstive.
mypos = InStr(UCase(Text1), UCase(SearchString))

'We found the search string.
If mypos Then MsgBox "Position of Hello is: " & mypos

2) This one is a little vague given the criteria you gave. Is what you want to search for always going to be after certain words, at the end of a string/sentence, etc? Give me a little more detail on this one and I will tell you how to do it.


0
Independent Software Vendors: 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!

 

Expert Comment

by:DaveD072798
ID: 1433484
Cantrell's method works perfectly well as long as you're doing everything within a single program. I'd recommend it if that's what you're attempting. I probably read more into your question. If you're trying to talk between applications or objects, you'd probably need to use properties, or at least methods (Public Functions and Public Subs). If all you're doing is working within a SINGLE app that try Cantrell's answer. Sorry for the waste of bandwidth!
0
 
LVL 12

Expert Comment

by:fulscher
ID: 1433485
Question 2: Just to make sure that I got the question right:

You would like to read a sentence into a variable and detect certain keywords (or sentences). If that keyword, or sentence, is present, you would like to extract certain parts of the sentence and put these parts into a separate variable.

That's an interesting problem which is not quite that easy to solve. Basically, you would need to write a price of software called "parser" that extracts the elements of a sentence and finds the certain variables.

For an easy start, try the following (the following is not verified with VB, so there might be a few syntax errors):

Function FindName(sInputLine as String, sOutName as String) as Boolean

Const cSSBegin = "Hello! My name is "
Const cSSEnd = "!"

Dim iCurSearchPos as Integer
Dim iPrevSearchPos as Integer
Dim iKeyStart As Integer
Dim iKeyEnd as Integer

' default result
FindName = False

' first, try to find cSSBegin:
iCurSearchPos = InStr(UCase(sInputLine), UCase(cSSBegin))
if iCurSearchPos = 0 then ' not found
  Exit Sub
Endif

iKeyStart = iCurSearchPos + Len(cSSBegin)

' Find the end of the sentence:
' Search the LAST occurence of cSSEnd in the string
iCurSearchPos = 0
iPrevSearchPos = 0
While iCurSearchPos <> 0
  iPrevSearchPos = iCurSearchPos
  iCurSearchPos = InStr(iPrevSearchPos + 1, UCase(sInputLine), UCase(cSSEnd))
Wend

If iPrevSearchPos = 0 Then  'end not found; assume rest of string
  iKeyEnd = Len(sInputLine)
Else ' end found
  iKeyEnd = iPrevSearchPos
endif

' not extract the result; may require more checking
sOutName = Mid$(sInputLine, iKeyStart, iKeyEnd-iKeyStart)
FindName = True
End Function

To use:
  Dim sName as String
  If FindName(Text1.Text, sName) Then
    MsgBox "Hello " & sName & "!"
  Endif


0
 
LVL 1

Author Comment

by:eversoslightly
ID: 1433486
Thanks for your help!

I don't have time to check these out right now... I'll check em out tonight sometime....

but...

(I was in kind of a hurry when writing it out, soo.... to clarify)


I've got a variable.

It may be something like "Whatever whatever talking garble yada yada Hello!  My name is Bob.  How are you? yada yayda whatever"

Now.... I want some sort of an if then statement....

If, somewhere in this variable is "Hello!  My name is ?????.  How are you?"  Then do..... whatever....

Also, then, as in question 2, I'd want it to save 'Bob', or whatever the ????? might happen to be, to a separate variable.

Hope this clarifys it.... feel free to ask more questions... I'll try these out tonight :0

-Ever-So
0
 
LVL 12

Expert Comment

by:fulscher
ID: 1433487
You might want to check for Eliza-Source Code. There is a basic version around, I believe. Eliza (Elisa?) is an Pseudo-AI that recognises certain keywords and reacts accordingly.
0
 

Accepted Solution

by:
joe_s earned 50 total points
ID: 1433488
If I understand you correctly this will do it. The variable sName contains the name 'Fred' (and anything after it). If you only want 'Fred' then you just have to do another instr on sName to find character (' ',!?) etc. and then take the left$ side of sName until the special character.

Dim sLookingFor As String
Dim iFoundPosition As Integer
Dim sLookingInString As String
Dim sName As String

sLookingInString = "abcdefg Hello, my name is Fred"
sLookingFor = "Hello, my name is"

iFoundPosition = InStr(1, sLookingInString, sLookingFor)
sName = Right$(sLookingInString, Len(sLookingInString) - iFoundPosition - Len(sLookingFor))

MsgBox "Name is " & sName

0
 
LVL 1

Author Comment

by:eversoslightly
ID: 1433489
Ok... the results of my testing :)

fulscher had the right idea, yet it didn't work.

cantrell's thing for question was right on the numbers.  I'd like to give him points, which I will in a separate question... keep and eye out for that cantrell! :)

And joe_s - regarding question 2, with what you put, it works... I'd like it if you could write out that what you said with the left$.  Then I'll give you the points.

Thanks!
0
 

Expert Comment

by:joe_s
ID: 1433490
This will do it if you know what the character after the name is. The second example will work for any non-numeric character following the name. Good Luck.

Dim sLookingFor As String
Dim iFoundPosition As Integer
Dim sLookingInString As String
Dim sName As String

sLookingInString = "abcdefg Hello, my name is Fred. What is your name?"
sLookingFor = "Hello, my name is"

iFoundPosition = InStr(1, sLookingInString, sLookingFor)
sName = Right$(sLookingInString, Len(sLookingInString) - iFoundPosition - Len(sLookingFor))

MsgBox "Right side of string is :" & sName & ":"

iFoundPosition = InStr(1, sName, ".")
sName = Left$(sName, iFoundPosition)

MsgBox "Name is " & sName

---------------------------------------------
This code will find "Fred" as long as the character following 'd' is not alpha:

Dim sLookingFor As String
Dim iFoundPosition As Integer
Dim sLookingInString As String
Dim sName As String
Dim sChar As String
Dim iIndex As Integer

sLookingInString = "abcdefg Hello, my name is Fred What is your name?"
sLookingFor = "Hello, my name is"

iFoundPosition = InStr(1, sLookingInString, sLookingFor)
sName = Right$(sLookingInString, Len(sLookingInString) - iFoundPosition - Len(sLookingFor))

MsgBox "Right side of string is :" & sName & ":"
iIndex = 0
Do
  iIndex = iIndex + 1
  sChar = UCase$(Mid$(sName, iIndex, 1))
Loop While (iIndex < Len(sName) And Asc(sChar) > 64 And Asc(sChar) < 91)

sName = Left$(sName, iIndex - 1)

MsgBox "Name is " & sName


0
 
LVL 1

Author Comment

by:eversoslightly
ID: 1433491
Thanks!

It worked great.
0

Featured Post

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!

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…

763 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