weightinwildcat
Programmer
I have been working on a form that allows users to enter information on parts prior to having work done on a CNC machine. The information will be added to the Message field of a new record, the field is Long Text.
In the course of adding the information we need to get rid of some special characters, since the new record will be added using a query that is saved in a String variable and run with the db.Execute command.
The code for getting rid of special characters is presently as follows:
charCount = Len(newText)
For i = 1 To charCount
If Asc(Mid(newText, i, 1)) = 39 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, "''", i, 1)
If Asc(Mid(newText, i, 1)) = 34 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, """", i, 1)
End If
End If
newTextChar = ""
Next
I am trying to make things more modular in case we ever need to get rid of any other special characters. However, I have run into error number 5 when I try different configurations for this code.
In one case I did this:
For i = 1 To charCount
If Asc(Mid(newText, i, 1)) = 39 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, "''", i, 1)
End If
newTextChar = ""
Next
For j = 1 To charCount
If Asc(Mid(newText, j, 1)) = 34 Then
newTextChar = Mid(newText, j, 1)
newText = Replace(newText, newTextChar, """", j, 1)
End If
newTextChar = ""
Next
In another case I did this:
If Asc(Mid(newText, i, 1)) = 39 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, "''", i, 1)
ElseIf Asc(Mid(newText, i, 1)) = 34 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, """", i, 1)
End If
newTextChar = ""
I also tried this:
For i = 1 To charCount
If Asc(Mid(newText, i, 1)) = 39 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, "''", i, 1)
ElseIf Asc(Mid(newText, i, 1)) < 32 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, " ", i, 1)
ElseIf Asc(Mid(newText, i, 1)) > 126 And Asc(Mid(newText, i, 1)) < 192 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, " ", i, 1)
End If
newTextChar = ""
Next
So why does everything but the first method give me an error 5?
In the course of adding the information we need to get rid of some special characters, since the new record will be added using a query that is saved in a String variable and run with the db.Execute command.
The code for getting rid of special characters is presently as follows:
charCount = Len(newText)
For i = 1 To charCount
If Asc(Mid(newText, i, 1)) = 39 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, "''", i, 1)
If Asc(Mid(newText, i, 1)) = 34 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, """", i, 1)
End If
End If
newTextChar = ""
Next
I am trying to make things more modular in case we ever need to get rid of any other special characters. However, I have run into error number 5 when I try different configurations for this code.
In one case I did this:
For i = 1 To charCount
If Asc(Mid(newText, i, 1)) = 39 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, "''", i, 1)
End If
newTextChar = ""
Next
For j = 1 To charCount
If Asc(Mid(newText, j, 1)) = 34 Then
newTextChar = Mid(newText, j, 1)
newText = Replace(newText, newTextChar, """", j, 1)
End If
newTextChar = ""
Next
In another case I did this:
If Asc(Mid(newText, i, 1)) = 39 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, "''", i, 1)
ElseIf Asc(Mid(newText, i, 1)) = 34 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, """", i, 1)
End If
newTextChar = ""
I also tried this:
For i = 1 To charCount
If Asc(Mid(newText, i, 1)) = 39 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, "''", i, 1)
ElseIf Asc(Mid(newText, i, 1)) < 32 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, " ", i, 1)
ElseIf Asc(Mid(newText, i, 1)) > 126 And Asc(Mid(newText, i, 1)) < 192 Then
newTextChar = Mid(newText, i, 1)
newText = Replace(newText, newTextChar, " ", i, 1)
End If
newTextChar = ""
Next
So why does everything but the first method give me an error 5?