-
Apr 16th, 2004, 10:26 AM
#1
Thread Starter
New Member
Can't quite describe this one...
Hi,
Using VB6 and having a bit of an annoying problem, the following code: -
Sub strFunction()
Dim str As String
Dim Name As String
Name = "Mark"
str = "Hello <Name> has selected <Name> to review them"
str = Replace(str, "<", """ & ")
str = Replace(str, ">", " & """)
str = """" & str & """"
Debug.Print str
End Sub
Produces the following output : -
"Hello " & Name & " has selected " & Name & " to review them"
This is correct except that & Name & should be Mark and it shoudl look like this : -
"Hello Mark has selected Mark to review them"
When you hard code "Hello " & Name & " has selected " & Name & " to review them" in it works perfectly, any ideas?
Kind regards,
Homer
-
Apr 16th, 2004, 10:32 AM
#2
Try hard-coding it straight in. It hasn't failed for me!
VB Code:
Sub CreateString()
Dim message, name As String
name=InputBox ("Enter Your Name:", , "Welcome!") ' second quotes is just the title
message="Hello " & name & " has selected " & name & " to review them."
lblMessage=message
End Sub
It should accomplish the same thing that you are trying to do without all that "Replace" crap...
-
Apr 16th, 2004, 10:34 AM
#3
str = Replace(str, "<Name>", Name)
-
Apr 16th, 2004, 10:49 AM
#4
Thread Starter
New Member
Yep, you are both correct but it's not what we are looking for, the reason for all the replace options on the " <> " signs is so that the user can put a number of different things in the middle, like: -
<Forename>
<Surname>
<Age>
etc etc etc,
Too many to hard code directly, therefore by replacing just the <> signs with the " & it would overcome this problem, but it just doesn't seem to like it!
Any help is much appreciated, surely someone must have come across this before!
Thanks in advance,
Homer J
-
Apr 16th, 2004, 10:58 AM
#5
Fanatic Member
I don't know if this will help but, you could use a function:
Code:
Private Function getVar(p As String) As String
Select Case p
Case "<Name>"
getVar = theName
'as many cases as you need
End Select
End Function
theName = "Mark"
str = "Hello <Name> has selected <Name> to review them"
str = Replace(str, "<Name>", getVar("<Name>"))
variables would have to be form level - I know it's not exactly what you want but I don't think it is possible to convert literals to variables. Hope that helps
"Knowledge is gained when different people look at the same information in different ways"
- Louis Pasteur
-
Apr 16th, 2004, 11:42 AM
#6
There is no alternative to hard coding, just different methods of doing it (using an array may well be an option).
No matter what characters you add to a string it will always be just a string, you cannot convert it into code.
-
Apr 16th, 2004, 01:53 PM
#7
Yeah, what is your ultimate objective? If you hardcode with a variable, you can have that variable show up, but if you are thinking that you can cause a different behavior after compilation by altering the string, you won't get it. What you wrote was a waste of time, but it wasn't clear what you ultimately wanted to have happen.
-
Apr 17th, 2004, 10:32 AM
#8
Thread Starter
New Member
What we are looking for is quite simple.
A text box where a user can type in text and in addition they can put a code in that represents a number of fields within a database. Such as: -
<Name>
<Forename>
Now, what we want to programme is that anywhere in that text box a < or > appears we want to replace it with " & so that it will produce what I said above, quite simple really.
Are you all saying that this is not possible?
Homer
-
Apr 17th, 2004, 11:06 AM
#9
We do something similar in our report writer - we have paragraphs that we store that have "{token}" embedded within.
We like {} over <> because they are less likely to occur in "real text".
I've ripped this out of a production program - and cleaned it up a bit to fit your example better. Please note that we declare i,j,k,x,y,z as longs in all our routines and s1,s2,s3,s4 and s5 as string - just to have handy access to temporary variables...
VB Code:
y = InStr(strPara, "{")
Do While y <> 0
z = InStr(y, strPara, "}")
s3 = Mid(strPara, y + 1, z - y - 1)
If s3="NAME" Then
s3 = strName
End If
strPara = Left(strPara, y - 1) + s3 + Mid(strPara, z + 1)
y = InStr(y, strPara, "{")
Loop
-
Apr 17th, 2004, 12:07 PM
#10
Frenzied Member
For user defined variable and/or function processing checlk out the MS Script Contol. It can run arbitary VBScript code entered during runtime.
VB Code:
Private Sub Command1_Click()
Dim Result As String
ScriptControl1.AddCode (Text1.Text)
Result = ScriptControl1.Run("UserDefinedFunction", "KayJay", 25)
MsgBox Result
End Sub
Private Sub Form_Load()
Text1.Text = ("Function UserDefinedFunction(UserName, Age)" & vbCrLf & vbCrLf & _
"UserDefinedFunction = UserName & vbcrlf & Age" & vbCrLf & vbCrLf & _
"End Function")
End Sub
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Apr 17th, 2004, 12:57 PM
#11
The picture isn't missing
You could also have it call an object in your program. Just add a Class module, and put your (public) functions in there.
Dim cScript as class1
set cscript=new class1
msscript.addobject class1, "ObjectName"
then in the msscript you could access it like an object:
ObjectName.SayThis "hello"
Remember, if someone's post was not helpful, you can always rate their post negatively .
-
Apr 19th, 2004, 09:21 AM
#12
You could always try something like this... Please forgive me if it's a little clumsy, I'm just giving you the code I know how to use.
VB Code:
Private Sub cmdNewUser_Click()
Dim UserItem as String ' just to check what they want to change
Dim UserPref as String ' to change what user wanted to change
UserItem=InputBox ("Valid Items: name, age, sname", , "Choose Field:") ' second quotes is title, first quotes is where you tell them whats useable
If lcase(UserItem)="name" Then
' what to change if user wants to change name
If lcase(UserItem)="age" Then
' what to change if user wants to change age
' etc, etc...
EndIf
UserPref="User's name is " & name & ", user is " & age & " years old, and lives in " & location "." ' or something like that..
End Sub
In this case, just use UserPref as the full string to compile all the information at the end.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|