Results 1 to 14 of 14

Thread: how do i base a sql query on a value of a variable???

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2000
    Posts
    103
    Code:
    SQL = "select * from clients where clientID =" myvalue
    how do i base my sql query to pull all the records out according to what the myvalue variable is???

    any ideas??
    thanks

  2. #2
    Junior Member
    Join Date
    Nov 2000
    Posts
    20
    It seems like you have the query built correcly, what
    seems to be the problem?

  3. #3
    Guest

    Wink How about trying

    Originally posted by johnnyboy23
    Code:
    SQL = "select * from clients where clientID = " & myvalue

  4. #4
    Junior Member
    Join Date
    Nov 2000
    Location
    Belgium
    Posts
    21

    Exclamation

    Make sure myvalue is a string.
    If myvalue is numeric, use Str(myvalue)

  5. #5
    Member
    Join Date
    Oct 2000
    Location
    Netherlands
    Posts
    54

    Use quotes if string

    Be sure to use the quotes if you search for string. When I started programmin in VB3 (there was no Internet yet commonly spread) it took me 3 days to figure it out. Wish someone has told me that then:

    Code:
    SQL = "Select * FROM tblClients WHERE clientID = '" & str(myValue) & "'"

    A mind is like a parachute, it has to open to let it work
    www.2beesoft.com for Icon Manager with over 20.000 free icons
    VB6 Ent. SP4, ASP, W2000/W98

  6. #6
    Guest

    Re: Use quotes if string

    Originally posted by Berthil
    Be sure to use the quotes if you search for string. When I started programmin in VB3 (there was no Internet yet commonly spread) it took me 3 days to figure it out. Wish someone has told me that then:

    Code:
    SQL = "Select * FROM tblClients WHERE clientID = '" & str(myValue) & "'"

    Exactly right. Forgot about the quotes thing for strings, another one of the crazy things you have to do with vb, other languages don't require them.

  7. #7
    Hyperactive Member JazzBass's Avatar
    Join Date
    Jun 1999
    Posts
    393

    Smile Hey Jethro

    Hi Jethro,
    About your comment:

    "another one of the crazy things you have to do with vb, other languages don't require them"

    Are you sure about this? I thought it's standard SQL to include the single quotes for strings, like Berthil explained.

    I use them in Access and Oracle.

    Not trying to sound like a smarty, just wondering.

    Thanks,
    JazzBass
    JazzBass
    In the .NET era
    Trying to remember VB6
    Progress:
    XP Professional @ Home
    and @ the Office

  8. #8
    Guest

    Thumbs up Not a problem Jazz

    We use both Pick and Universe, under these languages you can do something like.

    Code:
    txtName = "Kerry O'Brian"
    select customer where name = txtname
    name is of course a field in the file customer, and as such the relation is implied and doesn't have to be stated. Any variable can be thrown into a statement without need of quotation marks. Even better it doesn't matter if the variable is null...

  9. #9
    Junior Member
    Join Date
    Feb 2000
    Location
    Dallas,Tx,USA
    Posts
    30


    I have a problem i want to pull data from Oracle.

    This code works well.

    sql_command = "select * from COMPANY where CO_NM1 = '" & c_name3 & "'"

    But when c_name3 = "Arthurs's Books"

    Then i am out of luck and get an error because of the single quote in Arthur's.

    I have numerous ways around this problem but i do fin until i get to a variable with a single quote in it.

    sql_command = "select * from COMPANY where CO_NM1 = """ & c_name3 & """"




    Please help me.


    Mark S


  10. #10
    Guest

    Wink I think its to do with a single quote

    Being a notation for a comment line in vb.

  11. #11
    Guest

    F@#$%&G single quotes!!

    Single quotes are a curse to all VB database programmers! If there is one in your data, as Jethro said, the rest of the string is interpreted as a comment, not as SQL like you intended. Where I work we have a function that checks all text before it is put into a database so there are no hassles when it comes to retrieving the data. I think it was from a book(all due credit to its author!). This guarantees the data in your db is clean. If you really like single quotes in text try replacing them with ` before adding to the database(the function is called replace). You can see everything the function cleans out in the illegals string, I added a few charcters for the sake of the demo, you can alter this as you please.

    usage
    Code:
    cleanstring(text1.text)
    'then perform the insert of text1.text into database
    Code:
    Private Function cleanstring(str As String) As String
    'clean illegal characters out of data
    
    Dim i As Integer
    Dim illegals As String
    
    illegals = "~@^#&()`:;{}<>_?$'*%"""
    
    For i = 1 To Len(illegals)
        str = Replace(str, Mid(illegals, i, 1), " ")
    Next i
    cleanstring = str
    
    End Function

  12. #12
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    In SQL Server, there is an option quoted identifier which allows the replacement of ' with '' in string literals. There must be an equivalent in Oracle. Have you tried ''?

    P.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  13. #13
    Hyperactive Member JazzBass's Avatar
    Join Date
    Jun 1999
    Posts
    393

    Talking Jethro

    Thanks!

    That sounds pretty cool. I don't mind the single quotes that much, since that's what I started with. Try asking me that 2 days ago when I was working on my own program trying to do a INSERT INTO state with about 8 variables. Then I would have been singing a different tune.

    Thanks again for the reply.

    JazzBass
    JazzBass
    In the .NET era
    Trying to remember VB6
    Progress:
    XP Professional @ Home
    and @ the Office

  14. #14
    Guest

    Unhappy Madworm tried a similar thing

    But our customers needed the single quote in names like O'Connor etc, couldn't train them into not using it....got the code from http://www.planetsourcecode.com to over-come this, but caren't quite find the actual source at the moment...must be in one of our dlls somewhere, though l would have thought our standards would have forced use of a function.

    Will post up when l find 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