+ Reply to Thread
Results 1 to 6 of 6

SQL statement problem

  1. #1
    Registered User
    Join Date
    04-19-2007
    Posts
    50

    SQL statement problem

    I have a variable declared as a string (Dim myComment as String)

    myComment is equal to a cell on my spreadsheet. If the user types in that cell special characters such as quotations or single quotes, my code produces an syntax error. My code is trying to execute an SQL statement that includes the variable myComment. How do I get the SQL statement to execute?

    For example: mySQL = "SELECT tblContractorDataOnly.FName FROM tblContractorDataOnly WHERE FName = "Mary's"). In this example "Mary's" is what is in the cell. myComment = "Mary's".

    My SQL statement won't execute because of the single quote in Mary's.

    What can I do?

  2. #2
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    talytech

    Please wrap your VBA code - see red link below for instructions
    Please Read Forum Rules Before Posting
    Wrap VBA code by selecting the code and clicking the # icon or Read This
    How To Cross Post politely

    Top Excel links for beginners to Experts

    If you are pleased with a member's answer then use the Scales icon to rate it
    If my reply has assisted or failed to assist you I welcome your Feedback.

  3. #3
    Registered User
    Join Date
    04-19-2007
    Posts
    50
    I don't quite understand but I'll try again.

    I have a variable declared as a string
    Please Login or Register  to view this content.
    myComment is equal to a cell on my spreadsheet. If the user types in that cell special characters such as quotations or single quotes, my code produces an syntax error. My code is trying to execute an SQL statement that includes the variable myComment. How do I get the SQL statement to execute?

    For example:
    Please Login or Register  to view this content.
    In this example "Mary's" is what is in the cell. myComment equals "Mary's".

    My SQL statement won't execute because of the single quote in Mary's.

    What can I do?

  4. #4
    Registered User
    Join Date
    04-19-2007
    Posts
    50
    Does anyone have a clue of what I'm asking? If so, please help.

  5. #5
    Valued Forum Contributor mudraker's Avatar
    Join Date
    11-10-2003
    Location
    Melbourne, Australia
    Posts
    3,983
    You may need to post your question/problem on another site

    Try ozgrid.com or vbaexpress.com They both have forums dealing with Excel & Access

    If you do post to these sites make sure you follow our & their forum rules about cross posting the same question to multiple sites

  6. #6
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591
    Hi

    I'm guessing that your code would be along the lines of

    Please Login or Register  to view this content.
    If so then try it as

    Please Login or Register  to view this content.
    Some SQL requires that the item be surrounded by a single quote character and this may allow it to work. If it doesn't then try using the REPLACE or SUBSTITUTE on the myComment variable to change the ' to \' and still surround with the '. Depending on your instance of SQL, you may have to find whatever the character is that specifies a special character follows. Sometimes \, or it may be ~

    HTH

    rylo

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1