Results 1 to 11 of 11

Thread: [RESOLVED] sql query in vb HELP

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    182

    Resolved [RESOLVED] sql query in vb HELP

    If an sql query has ' then how to avoid is..I know v can use the replace function n i have tried but it still gives me an error...does anybody have any function that returns a string..that formats a query..thanx
    Last edited by Hack; Mar 7th, 2006 at 09:10 AM. Reason: Added [RESOLVED] to thread title and green "resolved" checkmark

  2. #2
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: sql query in vb HELP

    so you've already tried the below?

    VB Code:
    1. sql = Replace$(sql, "'", "''")

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    182

    Re: sql query in vb HELP

    yes i have tried it but still error..
    my query is

    psql = "select PriceCode from InsuranceTypes where Name='" & value & "'"

    and value =Railway & Transport Employees' Friendly Society He

    if i put that an call the function it still find a problem

  4. #4

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    182

    Re: sql query in vb HELP

    the error is the i replaces " with '..if i format the value only then its fine

  6. #6
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: sql query in vb HELP

    so you just need:

    VB Code:
    1. psql = "select PriceCode from InsuranceTypes where Name='" & Replace$(value, "'", "''") & "'"

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    182

    Re: sql query in vb HELP

    This is not working either now..how can i solve this..its really a pain

  8. #8
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: sql query in vb HELP

    Place

    VB Code:
    1. Debug.Print psql

    after you contruct the query and check the output for errors before sending it to the database.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    182

    Re: sql query in vb HELP

    thanx mate..i solved it but its a bit strange..why can't v replace all the string but instead v have to do it with the value parameter

  10. #10
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: sql query in vb HELP

    why can't v replace all the string but instead v have to do it with the value parameter
    All the text has to be enclosed within single quotes ('text goes here'), e.g.:

    VB Code:
    1. psql = Replace("select PriceCode from InsuranceTypes where Name='" & value & "'", "'", "''")
    outputs:
    VB Code:
    1. select PriceCode from InsuranceTypes where Name= '' Railway & Transport Employees '' Friendly Society He ''
    and none of the text is actually between the single quotes - so it errors:


    However, with:
    VB Code:
    1. psql = "select PriceCode from InsuranceTypes where Name='" & Replace$(value, "'", "''") & "'"
    the output is:
    VB Code:
    1. select PriceCode from InsuranceTypes where Name= 'Railway & Transport Employees' 'Friendly Society He'
    and all the text is enclosed in single quotes - and it is processed correctly.

    (I've edited the output strings so the quote pairs are made more obvious, it's not the true output.)

    Is that what you were asking?

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Oct 2005
    Posts
    182

    Re: sql query in vb HELP

    I have to try one of ur first options the way ur doing it..wat i was doing is that constructing a string and passing it to a function that formats it..i'll c it n if i have any problems..i'l ask u..thanx for ur help

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