Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

replace in all the application

Status
Not open for further replies.

ZeuBug

Programmer
May 16, 2000
10
FR
Hi

just one thing, i want to do a keypress but i want that it worked for all the application example :
i do some request in SQL and the char ' is forbidden, so i want that when a user press the key ' , i want to return the chr(180) but i dont want to test all the textbox i have, i want to change that for all the application

thanks for the answer cause i m not the only one to have this problem ?

bye [sig]<p>Raph<br><a href=mailto:rchene@rockefeller.univ-lyon1.fr>rchene@rockefeller.univ-lyon1.fr</a><br><a href= > </a><br> [/sig]
 

You can use the Replace function to do this. For example,

Text1 = Replace(Text1, &quot;'&quot;, &quot;&quot;)

I'm not sure how you can do this for the entire application in one shot, but you can certainly run this code immediately before you execute the SQL statement. The textboxes wouldn't show the result of the Replace function but at least the SQL will run.

strSQL = Replace(strSQL, &quot;'&quot;, &quot;&quot;)
[sig]<p> Tarek<br><a href= > </a><br>The more I learn, the more I need to learn![/sig]
 
thanks vb400 but in the sQL querystring there are some ' inside and i cant replace when i have done the querystring and i don t want to test each textbox
example : sSQL =&quot;select * from table where id_client = '&quot; & textbox.text & &quot;'&quot;
i just want to remove the ' which coudl be in the textbox

so if you have another solution, i take it

thanks for your answer
[sig]<p>Raph<br><a href=mailto:rchene@rockefeller.univ-lyon1.fr>rchene@rockefeller.univ-lyon1.fr</a><br><a href= > </a><br> [/sig]
 
Why don't you just get sneeky? Replace one keystroke with the other. Use a timer control with the Interval property set to about 100.
[tt]
Public Declare Function GetAsyncKeyState Lib &quot;user32.dll&quot; (ByVal vKey As Long) As Integer

Public Function GetInput()
Dim i As Integer
For i = 0 To 255
If (GetAsyncKeyState(i) And &H8001) <> 0 Then
GetInput = i 'A key was pressed
Exit Function
End If
Next i
End Function

Private Sub Timer1_Timer()
Gi = GetInput
Rep$ = &quot;{BS}&quot; & Chr$(180)
'If ' was pressed, replace it with Chr$(180)
If Gi = 222 Then SendKeys Rep$
End Sub
[/tt]
Hope this helps. B-)

[sig]<p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>"Suffice it to say that adding disk drives and a disk operating system to a personal microcomputer is guaranteed to increase its power dramatically."<br>
<u><b>CP/M and the Personal Computer</u></b>[/sig]
 
ZeuBug,

sSQL =&quot;select * from table where id_client = &quot; & &quot;'&quot; & &quot;textbox.text&quot; & &quot;'&quot;
? ssql
select * from table where id_client = 'textbox.text'
sSQL = Replace(sSQL, Chr(39), &quot;&quot;)
? ssql
select * from table where id_client = textbox.text


Please explain what doesn't work about VB400's response.

I tried your example in VB6 immediate window and it did what one would expect (with the &quot;fix&quot; shown above). The fix was necessary, as VB6 would NOT permit the assignment as shown in your example.

If - you have a valid string assigned to sSQL, I beleive VB400's soloution is appropiate.
[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 

Michael,

I think that Raph means that if you're searching for last name, you would normally have:

&quot;SELECT * FROM MyTable WHERE LastName = '&quot; & Text1 & &quot;'&quot;

So if Text1 has Smith then it would like this:

&quot;SELECT * FROM MyTable WHERE LastName = 'Smith'&quot;

However, if Text1 has Smi'th then it would like this:

&quot;SELECT * FROM MyTable WHERE LastName = 'Smi'th'&quot;

which would cause an SQL error.

[sig]<p> Tarek<br><a href= > </a><br>The more I learn, the more I need to learn![/sig]
 
Still, we (just) have a format problem. He MAY need to do a full parse of the string, however HE is obviously building the sql statement in code from the 'bits and pieces' from the text boxes. If, at the point of assembling the SQL string, he parses the textbox values to remove leading and trailing single quotes, and then converts remaining single quotes to include surrounding double quotes it will (should?) work.


Function ParseValue(ValIn As String) As String

'Michael L. Red, the Duvall Group
'10/12/00 (one day past &quot;Nerd Day&quot;)

Dim Idx As Integer
Dim ValOut As String
Dim MyChar As String * 1


'Simple Example/Usage
' Print ParseValue(&quot;'Mr. O'Hare'&quot;)
' Mr. O&quot;'&quot;Hare

' Print ParseValue(&quot;Mr. O'Hare&quot;)
' Mr. O&quot;'&quot;Hare



Idx = Len(ValIn)

For Idx = 1 To Len(ValIn) 'Walk the string
MyChar = Mid(ValIn, Idx, 1) 'Get single char from input
If (MyChar = Chr(39)) Then 'Check for the dreaded single quote
If (Idx <> 1 And Idx <> Len(ValIn)) Then 'Not first or last, Fix w/ &quot; & &quot;
ValOut = ValOut & Chr(34) & MyChar & Chr(34) 'Else add to String
End If
Else
ValOut = ValOut & MyChar
End If
Next Idx

ParseValue = ValOut

End Function


Try It, I think you'll like it.

[sig]<p>MichaelRed<br><a href=mailto:mred@duvallgroup.com>mred@duvallgroup.com</a><br>There is never time to do it right but there is always time to do it over[/sig]
 
Sorry, guys... I was misunderstoond the intentions and the technical aspects. I thought Fred was trying to prevent users from typing bad characters. My solution probably wouldn't be all that great anyway since it would prevent the keystroke in all Windows applications.

We couldn't live with that, could we?

Oh, BTW, the single quote maps as Char$(222) on this keyboard. I wonder why that is...
[sig]<p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>"Suffice it to say that adding disk drives and a disk operating system to a personal microcomputer is guaranteed to increase its power dramatically."<br>
<u><b>CP/M and the Personal Computer</u></b>[/sig]
 

Whether he does this while building the SQL or directly to the textboxes, he still has to look into every field/control -- I believe he's trying to avoid that.

Unless someone can come up with a way to do his generic replace on all textboxes in a form then Michael's method would be the most effective and centralized solution. The only down side (if you can call it that) is that the textboxes will still contain the bad chars.
[sig]<p> Tarek<br><a href= > </a><br>The more I learn, the more I need to learn![/sig]
 
Try [tt]GetAsyncKeyState[/tt]. It works system-wide, whether the keys are typed in a textbox, wordpad, an Excel cell or the VB editor. It doesn't matter.

I thought somebody would have seen the declaration at the top of the code snippet. As I said before, it's a bit of overkill since it can prevent the user from typing a single quote anywhere.

[sig]<p> <br><a href=mailto: > </a><br><a href= plain black box</a><br>"Suffice it to say that adding disk drives and a disk operating system to a personal microcomputer is guaranteed to increase its power dramatically."<br>
<u><b>CP/M and the Personal Computer</u></b>[/sig]
 
Zeubug
Thanks for all the answer but i just want to say if i could do a sort of keypress evenement but applied to the application so i have done a simple function which replace the single quote by the chr(180) which is similar to a single quote

to vb400, sure, you have undertood my problem and the problem of sql.

to michaelred, i don t understand what your function is doing and how it help me so if you want to give me some explaimation, mail me

to Alt255, i dont know the GetAsyncKeyState evenemnt but i ll search

thanks a lot for all, and if you have THE solution to execute the keypress in all the application Please GIIIIIVE ME

[sig]<p>Raph<br><a href=mailto:rchene@rockefeller.univ-lyon1.fr>rchene@rockefeller.univ-lyon1.fr</a><br><a href= > </a><br> [/sig]
 
Place all &quot; ' &quot; protected textboxes in a control array then they share the same keypress subroutine
[sig][/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top