Results 1 to 12 of 12

Thread: Can't quite describe this one...

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    3

    Angry 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

  2. #2
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    Try hard-coding it straight in. It hasn't failed for me!

    VB Code:
    1. Sub CreateString()
    2.  
    3.    Dim message, name As String
    4.  
    5.    name=InputBox ("Enter Your Name:", , "Welcome!") ' second quotes is just the title
    6.  
    7.    message="Hello " & name & " has selected " & name & " to review them."
    8.  
    9.    lblMessage=message
    10.  
    11. End Sub

    It should accomplish the same thing that you are trying to do without all that "Replace" crap...

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930
    str = Replace(str, "<Name>", Name)

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    3
    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

  5. #5
    Fanatic Member ahara's Avatar
    Join Date
    Nov 2003
    Location
    Toronto
    Posts
    531
    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

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,930
    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.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,041
    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.

  8. #8

    Thread Starter
    New Member
    Join Date
    Apr 2004
    Posts
    3
    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

  9. #9
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263
    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:
    1. y = InStr(strPara, "{")
    2. Do While y <> 0
    3.     z = InStr(y, strPara, "}")
    4.     s3 = Mid(strPara, y + 1, z - y - 1)
    5.     If s3="NAME" Then
    6.         s3 = strName
    7.     End If
    8.     strPara = Left(strPara, y - 1) + s3 + Mid(strPara, z + 1)
    9.     y = InStr(y, strPara, "{")
    10. Loop

  10. #10
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    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:
    1. Private Sub Command1_Click()
    2. Dim Result As String
    3.  
    4. ScriptControl1.AddCode (Text1.Text)
    5. Result = ScriptControl1.Run("UserDefinedFunction", "KayJay", 25)
    6.  
    7. MsgBox Result
    8.    
    9.  
    10. End Sub
    11.  
    12. Private Sub Form_Load()
    13. Text1.Text = ("Function UserDefinedFunction(UserName, Age)" & vbCrLf & vbCrLf & _
    14.              "UserDefinedFunction = UserName & vbcrlf & Age" & vbCrLf & vbCrLf & _
    15.              "End Function")
    16. End Sub

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  11. #11
    The picture isn't missing BuggyProgrammer's Avatar
    Join Date
    Oct 2000
    Location
    Vancouver, Canada
    Posts
    5,217
    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 .

  12. #12
    Banned timeshifter's Avatar
    Join Date
    Mar 2004
    Location
    at my desk
    Posts
    2,465
    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:
    1. Private Sub cmdNewUser_Click()
    2.  
    3.    Dim UserItem as String ' just to check what they want to change
    4.    Dim UserPref as String ' to change what user wanted to change
    5.  
    6.    UserItem=InputBox ("Valid Items: name, age, sname", , "Choose Field:") ' second quotes is title, first quotes is where you tell them whats useable
    7.  
    8.    If lcase(UserItem)="name" Then
    9.       ' what to change if user wants to change name
    10.    If lcase(UserItem)="age" Then
    11.       ' what to change if user wants to change age
    12.    ' etc, etc...
    13.    EndIf
    14.  
    15.    UserPref="User's name is " & name & ", user is " & age & " years old, and lives in " & location "." ' or something like that..
    16.  
    17. 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
  •  



Click Here to Expand Forum to Full Width