Solved

A little variable question :)

Posted on 1998-09-07
11
226 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month8 days, 11 hours left to enroll

617 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