Results 1 to 7 of 7

Thread: [RESOLVED] SQL Statement for newbie !

  1. #1

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Resolved [RESOLVED] SQL Statement for newbie !

    Hi everybody ! I'm a new one in ADO and SQL Statement...

    Two questions for you:

    1) Is it normal that my statement got many (") ??? if answer it's no, is there a simple way to write this ???

    strSQL = "SELECT * FROM " & adoTable & " WHERE " & adoField & " = " & "'" & adoCombo.Text & "'"

    2) Finally, sometime I got an error because the value of adoCombo.Text = Column 6" x 8'

    Error: Syntax error (missing operator) in query expression...

    I thought it's because the (') after 8 and I tried to put 96" and I a got the same error ???

    Thanks in advance for your help !
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: SQL Statement for newbie !

    The " and ' are marking the end of the string statements (the SQL) which happens in the middle of your statement. You need to use parameters or do a preplace on the 6" x 8' like this:

    VB Code:
    1. dim strTest as string
    2.  strTest = Replace(adoCombo.Text,"'","''")
    3.  strTest = Replace (strTest.Text """","""""")

    Look at the qoute marks really carefully. The are some single qoutes that are doubled up and some double qoutes to double doubles and quad doubles.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: SQL Statement for newbie !

    And
    Code:
    strSQL = "SELECT * FROM " & adoTable & " WHERE " & adoField & " = " & "'" & adoCombo.Text & "'"
    I see nothing wrong with this statement.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement for newbie !

    1) You really shouldn't be using generic sql like that.... you should use specific SQL, where the Table and field(s) are known.
    Code:
    strSQL = "SELECT * FROM MyTable WHERE myField = " & "'" & adoCombo.Text & "'"
    2) Now is a good time to learn about parameterized queries. It's the use of " AND ' that is your problem. They are used by SQL as text indicators.... more on the solution in a sec

    3) Another reason fo parameterized queries is security. LEt's use your query as an example:
    Code:
    strSQL = "SELECT * FROM MyTable WHERE myField = " & "'" & adoCombo.Text & "'"
    What would happen if adboCombo contained this: '; DELETE * FROM someTable; SELECT * FROM Users WHERE Uname = '

    Your resulting SQL would look like this:
    SELECT * FROM MyTable WHERE myField = ''; DELETE * FROM someTable; SELECT * FROM Users WHERE Uname = ''

    As you can see.... that would be very bad, now wouldn't it?

    But if you use a parameterized query:
    Code:
    strSQL = "SELECT * FROM MyTable WHERE myField = @FldParam"
    Then use the .CreateParameter (of the command object) to create a paremeter and it's value to pass in, then add it to the parameters collection using .Parameters.Add (again, in the command object).

    Hope this is enough info to get you started looking in the right places

    -tg


    edit: ugh... Gary - geezes... what's with the grep solution? Why does eveyone think that solves things? IMHO that just makes it worse since you're modifying data. Data should never be modified just to store it in the DB. Hack - this trout's for you while syntaticaly correct, there's everything wrong with it.
    Last edited by techgnome; Sep 5th, 2006 at 09:46 AM. Reason: Gary & Hack posted while I was composing.
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SQL Statement for newbie !

    See also this thread for a similar notes about sql concatenation.
    http://vbforums.com/showthread.php?t=426312

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: SQL Statement for newbie !

    Quote Originally Posted by GaryMazzone
    The " and ' are marking the end of the string statements (the SQL) which happens in the middle of your statement. You need to use parameters or do a preplace on the 6" x 8' like this:

    VB Code:
    1. dim strTest as string
    2.  strTest = Replace(adoCombo.Text,"'","''")
    3.  strTest = Replace (strTest.Text """","""""")

    Look at the qoute marks really carefully. The are some single qoutes that are doubled up and some double qoutes to double doubles and quad doubles.
    Thanks again !

    Quote Originally Posted by Hack
    And
    Code:
    strSQL = "SELECT * FROM " & adoTable & " WHERE " & adoField & " = " & "'" & adoCombo.Text & "'"
    I see nothing wrong with this statement.
    Cool ! It's complicated to read because there are many (")
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

  7. #7

    Thread Starter
    Hyperactive Member DubweiserTM's Avatar
    Join Date
    Dec 2005
    Location
    St-Ferdinand, Québec
    Posts
    427

    Re: SQL Statement for newbie !

    Quote Originally Posted by techgnome
    2) Now is a good time to learn about parameterized queries. It's the use of " AND ' that is your problem. They are used by SQL as text indicators.... more on the solution in a sec

    Hope this is enough info to get you started looking in the right places...
    Thanks you too ! I will check for this !
    Last edited by DubweiserTM; Sep 5th, 2006 at 10:03 AM.
    DubweiserTM

    If your question has been answered, you can mark a thread as resolved...

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