Results 1 to 13 of 13

Thread: Saving quotes in variables

  1. #1

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262

    "Quotations" in variables - Need Master-Gurus of VB help plese

    Here is the problem...

    I need to save quotes in a variable. How in the world do you do that? I have declared the following variables as strings SearchField and SearchString. I need to have these variables in quotes per the appropriate syntax, unless there is a better way to do this.

    Code:
    Dim SearchField As String
    Dim SearchString As String
    
    Private Sub cmdFindNext_Click()
    frmMain.Data1.Recordset.FindNext SearchField Like SearchString
    MsgBox frmMain.Data1.Recordset.Fields(searchfield)
    End Sub
    Without the variables, the code might look like this:
    (ie-with the quotes)

    Code:
    Private Sub cmdFindNext_Click()
    frmMain.Data1.Recordset.FindNext "answer Like '*20*'"
    MsgBox frmMain.Data1.Recordset.Fields("answer")
    End Sub
    Again, the question is how do you place data in a variable, which would ordinarily require quotes. If this is confusing, let me go at it another way. How would you have a textbox display a variable with quotes? For example you want the textbox to say quote Hello World unquote or another example/question is: This is my "butcher" knife
    How would you have the word butcher show in quotes, especially if that whole sentence is the result of a variable(string)?????

    Thanks,
    Sal

  2. #2
    Guest
    Try using the Chr(34) which is a ".

    Code:
    Dim MyVar As String
    MyVar = Chr(34) & Text1 & Chr(34)

  3. #3

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    Gonna try it...thanks

    Will let you know how it works.

    Sal

  4. #4

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262

    Unhappy

    Didn't work. Search button comes up with undesirable results. Any other Ideas????

    Sal

    [Edited by Sal on 05-29-2000 at 06:11 PM]

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Arrow Another way.

    You can also use a quotation mark twice in a string to let the compiler know, "Don't end the string here or something... This is a quotation mark!"

    For example:
    Code:
    MsgBox "This is ""my"" code!"
    MsgBox """Quoted Text"""
    MsgBox """" ' Or MsgBox Chr(34)
    Would give the messages:

    This is "my" code!
    "Quoted Text"
    "

  6. #6
    Guest
    Can you please repost that? You accidently put a \ when ending the code instead of a /.

  7. #7

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    There seems to be some kind of problem which I cannot identify. I am trying to setup a simple search routine for a database which contains multiple fields. I would like for the user to be able to search the field of choice, with the text of choice.

    If I setup the code to search for what I want it to look for, it works great. The example of that is:

    Code:
    Private Sub cmdFindNext_Click()
    frmMain.Data1.Recordset.FindNext "question like '*aircraft*'"
    MsgBox frmMain.Data1.Recordset.Fields("question")
    End Sub
    >>>question is the field in my database and aircraft is the text which is being searched for in that field (question).<<<<

    The above code uses the search information that I put in the code.

    Now...
    If I setup variables, it doesn't work. I thought maybe there was a problem with the lack of quotations, as seen above. But, I'm not sure that the quotes are the problem. Maybe it's something else. Hmmmm.

    Here is the code with quotes ( using chr(34) ):

    Code:
    Dim SearchField As String
    Dim SearchString As String
    
    Private Sub cmdFindNext_Click()
    SearchString = "'*" & txtFind & "*'"
    frmMain.Data1.Recordset.FindNext chr(34) & SearchField Like SearchString & chr(34)
    MsgBox frmMain.Data1.Recordset.Fields(chr(34) & searchfield & chr(34))
    End Sub

  8. #8
    Guest
    Hmmm...Try storing chr(34) & searchfield & chr(34) in 1 variable and make VB look for that.

    For example.

    Code:
    Dim SearchThisString
    SearchThisString = chr(34) & searchfield & chr(34)
    Now you line should look like this

    Code:
    MsgBox frmMain.Data1.Recordset.Fields(SearchThisString)

  9. #9

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262
    I have tried so many things that I can't remember what I've tried. So,... I will try your idea. Will let you know...

    Sal

  10. #10

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262

    Angry

    No Joy!!

    Man this is frustrating. You would think that the compiler wouldn't really care about this. But it does. Boy I really hope this isn't something silly like a wrong letter, cause I've been working on this for about 5 hours now.

    Thanks for your help megatron.

    Sal

    Need more help!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  11. #11
    Guest
    Have you tried Yonatan's example?

    instead of using chr(34) & yourtexthere & chr(34), use """yourtexthere"""



  12. #12
    Guest
    possibly your problem is something completely different. as far as i understand your problem, it is in your sql statement. if you are using ADO and the LIKE statement you cannot use '*' as a placeholder (which is not sql standard anyway, just Access dialect), instead use '%'. maybe this will work.

    good luck

    Sascha

  13. #13

    Thread Starter
    Hyperactive Member Sal's Avatar
    Join Date
    Mar 2000
    Posts
    262

    Cool

    Woooooooo Hoooooooooooooooooooooooo


    ****************** F I N A L L Y *******************

    Ok, here is the scoop!
    It was just as simple as leaving a space before and after the like statement. I must admit,... I don't have a formal VB education, and it's trial and error for me a lot of the time. Syntax blues! Maybe this is true for a lot of folks also.

    The Like statement was saved to a variable(dbLike) as " like "
    It seems that the space before and after like made the difference. Wow, 5 hours to figure this out. LOL to me.

    Here is the code:

    Code:
    Dim SearchField As String
    Dim SearchString As String
    Dim dbLike As String
    Dim Sq As String
    
    Private Sub cmdCancel_Click()
    Me.Hide
    End Sub
    
    Private Sub cmdFindNext_Click()
    SearchString = "'*" & txtFind & "*'"
    ' This was the source of a major headache here:
    frmMain.Data1.Recordset.FindNext SearchField & dbLike & SearchString
    ' dbLike contains the like statement. The big deal is the 
    ' space before and after like.  Tee - dee - us!!!
    MsgBox frmMain.Data1.Recordset.Fields(SearchField)
    End Sub
    
    
    
    Private Sub Form_Load()
    ' Here it is again (Note: space before and after like)
    dbLike = " like "
    End Sub
    
    ' The rest of this is really not a player, just passing 
    ' variables, and selecting which field the user wants to 
    ' search
    
    Private Sub optField_Click(Index As Integer)
    
    If optField(0).Value = True Then
    txtFind.Width = 3375
    SearchField = "question"
    ElseIf optField(1).Value = True Then
    txtFind.Width = 3375
    SearchField = "answer"
    ElseIf optField(2).Value = True Then
    txtFind.Width = 1335
    SearchField = "reference"
    ElseIf optField(3).Value = True Then
    txtFind.Width = 1335
    SearchField = "date"
    End If
    End Sub
    The quotes really weren't an issue at all. The real issue again was the like statement. I guess the machine was looking at my command and read it like this: "fieldlike'*txtwhatever*'" which is jibberish I will admit.
    So, when I formatted the like statement with a space, it must have read it like this instead:
    "field like '*txtwhatever*'" which is more logical. Gosh, I have a love-hate relationship with programming. Very time consuming.

    Well, thanks to this forum, I will eventually complete my application. Thanks for your help Megatron.

    Sal

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