|
-
May 29th, 2000, 03:19 AM
#1
Thread Starter
Hyperactive Member
"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
-
May 29th, 2000, 03:22 AM
#2
Try using the Chr(34) which is a ".
Code:
Dim MyVar As String
MyVar = Chr(34) & Text1 & Chr(34)
-
May 29th, 2000, 03:29 AM
#3
Thread Starter
Hyperactive Member
Gonna try it...thanks
Will let you know how it works.
Sal
-
May 29th, 2000, 04:16 AM
#4
Thread Starter
Hyperactive Member
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]
-
May 29th, 2000, 04:44 AM
#5
Guru
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"
"
-
May 29th, 2000, 04:46 AM
#6
Can you please repost that? You accidently put a \ when ending the code instead of a /.
-
May 29th, 2000, 05:08 AM
#7
Thread Starter
Hyperactive Member
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
-
May 29th, 2000, 05:29 AM
#8
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)
-
May 29th, 2000, 05:34 AM
#9
Thread Starter
Hyperactive Member
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
-
May 29th, 2000, 05:42 AM
#10
Thread Starter
Hyperactive Member
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!!!!!!!!!!!!!!!!!!!!!!!!!!!!
-
May 29th, 2000, 05:48 AM
#11
Have you tried Yonatan's example?
instead of using chr(34) & yourtexthere & chr(34), use """yourtexthere"""
-
May 29th, 2000, 06:00 AM
#12
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
-
May 29th, 2000, 06:07 AM
#13
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|