Results 1 to 12 of 12

Thread: SQL Statement

  1. #1

    Thread Starter
    Hyperactive Member fasi's Avatar
    Join Date
    Nov 2000
    Posts
    474

    SQL Statement

    Well i spent hours and hours but couldnt figure this probelem out

    I am querying the database on a variablename that is a string.. it works fine .. but as soon as my varable name has an appostrpohe then the whole program crashes.

    LIke i wm ausing the statement

    rst.Open"SELECT * from Customers wher CustomerName = ' " & variable & " ';", cnn

    If the customer name is say

    Kevin it works fine

    But when it is
    Kevin's

    it crashes


    any solutions to this problem


  2. #2
    Fanatic Member ExcalibursZone's Avatar
    Join Date
    Feb 2000
    Location
    Western NY State
    Posts
    908
    Try this:
    Code:
    Replace(variable, "'", "' + CHAR(27) + '")
    If Right(Variable, 4) = " + '" Then
        Variable = Left(Variable, Len(Variable) - 4)
    End If
    Hope this helps you out. CHAR() is the SQL equivalent of Chr$ in VB.
    -Excalibur

  3. #3
    Junior Member
    Join Date
    Oct 2000
    Location
    Purgatory
    Posts
    31
    try:
    Code:
    rst.Open"SELECT * from Customers wher CustomerName = ' " & replace(variable,"'","''") & " ';", cnn

  4. #4

    Thread Starter
    Hyperactive Member fasi's Avatar
    Join Date
    Nov 2000
    Posts
    474
    PARAGON

    Dont you think this code will replace one apostrophe with two ... bacause what i think is that it is reading the apostropphe as the end of the statement

    can you explain a little bit what this code will do ??

  5. #5
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You may want to use the 'Like' Operator. Without knowing the full impliminations of the program, I'm just throwing out another suggestion.

    Code:
    rst.Open "SELECT * from Customers where CustomerName Like '" & variable & "%" & "';"

  6. #6

    Thread Starter
    Hyperactive Member fasi's Avatar
    Join Date
    Nov 2000
    Posts
    474
    Well Lethal I am not very familiar with the like operator .. Can you tell me what is the difference in like and = operators
    .

  7. #7
    Junior Member
    Join Date
    Oct 2000
    Location
    Purgatory
    Posts
    31
    The reason why I replace one ' with '' is because sql statements don't like strings with ' i.e. "where customer = 'It's'" will cause an error because it thinks you're ending the string with the second apostrophe instead of the third but if you were to have "where customer = 'It''s'" will work. (just tested it, just in case) It's just one of those weird things that you need to because of when creating sql statements.

  8. #8

    Thread Starter
    Hyperactive Member fasi's Avatar
    Join Date
    Nov 2000
    Posts
    474
    Paragon Thanx alor I tried it It works .. but tell me one thing .. this thing i used was for storing in the database .. Is is going to effect the query in any way... when i have to retrieve the data

  9. #9

    Thread Starter
    Hyperactive Member fasi's Avatar
    Join Date
    Nov 2000
    Posts
    474
    and what about the names that doen't have an apostrophe .. is it going to effect them

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    The "Like" operator instructs the dbms that the following search pattern is to compared using a wildcard match, rather than a exact math. Hence, Kevin is equal to Kevin's or Kevin1234, etc...

    It should solve your problem, but unexpected data may creep up, but if your only searching on a first name field, I wouldn't really worry about it to much.

  11. #11
    Junior Member
    Join Date
    Oct 2000
    Location
    Purgatory
    Posts
    31
    Yep, if you were to try:
    Code:
    rst.Open "INSERT INTO Customers(CustomerName) Values ('It''s');"
    This would actually insert "It's" into the record.

  12. #12
    Junior Member
    Join Date
    Oct 2000
    Location
    Purgatory
    Posts
    31
    Nope it won't affect names without ' it just won't replace anything in it

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