Consulting

Results 1 to 5 of 5

Thread: concat a variable with a string

  1. #1

    concat a variable with a string

    I'm trying to make a message box appear that says

    title "Wrong Magic Words" and message "<words> are not the magic words." where <words> is actually what the user typed in. (Hint: You'll need to concatenate a variable with a string.)

    I defined what the user typed in as the magic words as the following:

    Const strMAGICCODE = "Trick Or Treat"
    Dim strMAGICDODE As String
    Dim strCODESTORAGE As String
    strCODESTORAGE = InputBox("Enter the magic words", "User Validation")
    If strMAGICCODE = strCODESTORAGE Then
    ToggleButtons True

    Else

    MsgBox = Concat("strCODESTORAGE" & "Are not the magic words"), vbOKOnly, "Wrong Magic Words"

    End If

    ..I get an error though in the Concat section saying that a sub or function isn't defined..
    am I supposed to use strMAGICCODE..or strCODESTORAGE? Or am I supposed to set another variable or maybe a cell to hold the value that the user types in?

    please help

  2. #2
    VBAX Contributor
    Joined
    Jul 2005
    Posts
    169
    Location
    Hi
    Just try
    MsgBox (strCODESTORAGE & "Are not the magic words", vbOKOnly, "Wrong Magic Words")

  3. #3
    Administrator
    Chat VP
    VBAX Guru johnske's Avatar
    Joined
    Jul 2004
    Location
    Townsville, Australia
    Posts
    2,872
    Location
    [vba]
    Sub trythis()

    Const strMAGICCODE As String = "Trick Or Treat"
    Dim strCODESTORAGE As String

    strCODESTORAGE = InputBox("Enter the magic words", "User Validation")

    If strCODESTORAGE = strMAGICCODE Then
    ToggleButtons True
    Else
    MsgBox (strCODESTORAGE & " Are not the magic words"), vbOKOnly, "Wrong Magic Words"
    End If

    End Sub
    [/vba]
    You know you're really in trouble when the light at the end of the tunnel turns out to be the headlight of a train hurtling towards you

    The major part of getting the right answer lies in asking the right question...


    Made your code more readable, use VBA tags (this automatically inserts [vba] at the start of your code, and [/vba ] at the end of your code) | Help those helping you by marking your thread solved when it is.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by lexluther88

    ...

    MsgBox = Concat("strCODESTORAGE" & "Are not the magic words"), vbOKOnly, "Wrong Magic Words"

    ...


    ..I get an error though in the Concat section saying that a sub or function isn't defined..
    am I supposed to use strMAGICCODE..or strCODESTORAGE? Or am I supposed to set another variable or maybe a cell to hold the value that the user types in?
    CONCAT is a (superfluous) worksheet function. In VBA, just use the & operator.

    And you don't use =, that again is worksheet stuff.

    [vba]

    MsgBox "strCODESTORAGE" & "Are not the magic words", vbOKOnly, "Wrong Magic Words"
    [/vba]

  5. #5
    Thanks!! For both taking the time to help and for the code working as well. Johnske's code was perfect, it even had the space between what the user typed and "Are not the magic words"
    Thanks again.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •