Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Accommodating an apostrophe in SQL... [modified]

Accommodating an apostrophe in SQL... [modified]

Scheduled Pinned Locked Moved Database
databasehelpquestion
6 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    new_phoenix 0
    wrote on last edited by
    #1

    I need some assistance in accommodating an apostrophe in an SQL statement using VBA in MS Access. Please help. strSQLHyperion = "SELECT [COUNTRY], [TYPE], [BUSINESS_UNIT], " & _ "[ALT_GROUPING], [PERSONNEL_AREA], [L_R_G], [REGION], [JOB_FUNCTION], " & _ "[PRIMARY_KEY] FROM [TBLHYPERION] " & _ "WHERE [COUNTRY]='" & UCase(rstInputFile![COUNTRY]) & "' " & _ "AND [TYPE]='" & UCase(rstInputFile![Type]) & "' " & _ "AND [BUSINESS_UNIT]='" & UCase(rstInputFile![BUSINESS_UNIT]) & "' " & _ "AND [ALT_GROUPING]='" & UCase(rstInputFile![ALT_GROUPING]) & "' " & _ "AND [PERSONNEL_AREA]='" & UCase(rstInputFile![PERSONNEL_AREA]) & "' " & _ "AND [L_R_G]='" & UCase(rstInputFile![L_R_G]) & "' " & _ "AND [REGION]='" & UCase(rstInputFile![REGION]) & "' " & _ "AND [JOB_FUNCTION]='" & UCase(rstInputFile![JOB_FUNCTION]) & "'" What do I need to do to prevent it from hanging up on the apostrophe? The apostrophe needs to be accepted in: "AND [JOB_FUNCTION]='" & UCase(rstInputFile![JOB_FUNCTION]) & "'" :confused::confused::confused:

    modified on Wednesday, December 3, 2008 11:31 AM

    J W 2 Replies Last reply
    0
    • N new_phoenix 0

      I need some assistance in accommodating an apostrophe in an SQL statement using VBA in MS Access. Please help. strSQLHyperion = "SELECT [COUNTRY], [TYPE], [BUSINESS_UNIT], " & _ "[ALT_GROUPING], [PERSONNEL_AREA], [L_R_G], [REGION], [JOB_FUNCTION], " & _ "[PRIMARY_KEY] FROM [TBLHYPERION] " & _ "WHERE [COUNTRY]='" & UCase(rstInputFile![COUNTRY]) & "' " & _ "AND [TYPE]='" & UCase(rstInputFile![Type]) & "' " & _ "AND [BUSINESS_UNIT]='" & UCase(rstInputFile![BUSINESS_UNIT]) & "' " & _ "AND [ALT_GROUPING]='" & UCase(rstInputFile![ALT_GROUPING]) & "' " & _ "AND [PERSONNEL_AREA]='" & UCase(rstInputFile![PERSONNEL_AREA]) & "' " & _ "AND [L_R_G]='" & UCase(rstInputFile![L_R_G]) & "' " & _ "AND [REGION]='" & UCase(rstInputFile![REGION]) & "' " & _ "AND [JOB_FUNCTION]='" & UCase(rstInputFile![JOB_FUNCTION]) & "'" What do I need to do to prevent it from hanging up on the apostrophe? The apostrophe needs to be accepted in: "AND [JOB_FUNCTION]='" & UCase(rstInputFile![JOB_FUNCTION]) & "'" :confused::confused::confused:

      modified on Wednesday, December 3, 2008 11:31 AM

      J Offline
      J Offline
      Jay Royall
      wrote on last edited by
      #2

      You need to replace the apostrophy with 2 apostrophies. Best way is probably through a function (although I'm not sure of the exact syntax for VBA) - something like : Public Function MakeSqlSafe(strData) as string Return strData.Replace("'", "''") end function and then pass each one of your inputs through the function, e.g. '" & MakeSqlSafe(UCase(rstInputFile![REGION])) & "' Hope this helps.

      N 1 Reply Last reply
      0
      • J Jay Royall

        You need to replace the apostrophy with 2 apostrophies. Best way is probably through a function (although I'm not sure of the exact syntax for VBA) - something like : Public Function MakeSqlSafe(strData) as string Return strData.Replace("'", "''") end function and then pass each one of your inputs through the function, e.g. '" & MakeSqlSafe(UCase(rstInputFile![REGION])) & "' Hope this helps.

        N Offline
        N Offline
        new_phoenix 0
        wrote on last edited by
        #3

        Thank you, liqz for your assistance. The item that I have encountered in the implementation in the temporary copy of the application is: "Return without GoSub". The code is as follows in VBA: Public Function MakeSqlSafe(strData) As String strNewStrData = Replace(strData, "'", "''") Return End Function strSQLHyperion = "SELECT [COUNTRY], [TYPE], [BUSINESS_UNIT], " & _ "[ALT_GROUPING], [PERSONNEL_AREA], [L_R_G], [REGION], [JOB_FUNCTION], " & _ "[PRIMARY_KEY] FROM [TBLHYPERION] " & _ "WHERE [COUNTRY]='" & UCase(rstInputFile![COUNTRY]) & "' " & _ "AND [TYPE]='" & UCase(rstInputFile![Type]) & "' " & _ "AND [BUSINESS_UNIT]='" & UCase(rstInputFile![BUSINESS_UNIT]) & "' " & _ "AND IsNull([ALT_GROUPING]) " & _ "AND [PERSONNEL_AREA]='" & UCase(rstInputFile![PERSONNEL_AREA]) & "' " & _ "AND [L_R_G]='" & UCase(rstInputFile![L_R_G]) & "' " & _ "AND [REGION]='" & UCase(rstInputFile![REGION]) & "' " & _ "AND [JOB_FUNCTION]='" & MakeSqlSafe(UCase(rstInputFile![JOB_FUNCTION])) & "'" Any assistance in resolving this item would be greatly appreciated!!!

        J 1 Reply Last reply
        0
        • N new_phoenix 0

          Thank you, liqz for your assistance. The item that I have encountered in the implementation in the temporary copy of the application is: "Return without GoSub". The code is as follows in VBA: Public Function MakeSqlSafe(strData) As String strNewStrData = Replace(strData, "'", "''") Return End Function strSQLHyperion = "SELECT [COUNTRY], [TYPE], [BUSINESS_UNIT], " & _ "[ALT_GROUPING], [PERSONNEL_AREA], [L_R_G], [REGION], [JOB_FUNCTION], " & _ "[PRIMARY_KEY] FROM [TBLHYPERION] " & _ "WHERE [COUNTRY]='" & UCase(rstInputFile![COUNTRY]) & "' " & _ "AND [TYPE]='" & UCase(rstInputFile![Type]) & "' " & _ "AND [BUSINESS_UNIT]='" & UCase(rstInputFile![BUSINESS_UNIT]) & "' " & _ "AND IsNull([ALT_GROUPING]) " & _ "AND [PERSONNEL_AREA]='" & UCase(rstInputFile![PERSONNEL_AREA]) & "' " & _ "AND [L_R_G]='" & UCase(rstInputFile![L_R_G]) & "' " & _ "AND [REGION]='" & UCase(rstInputFile![REGION]) & "' " & _ "AND [JOB_FUNCTION]='" & MakeSqlSafe(UCase(rstInputFile![JOB_FUNCTION])) & "'" Any assistance in resolving this item would be greatly appreciated!!!

          J Offline
          J Offline
          Jay Royall
          wrote on last edited by
          #4

          Ah, try changing the function to :

          Function MakeSqlsafe(strData)
          MakeSqlSafe = Replace(strData, "'", "''")
          end Function

          N 1 Reply Last reply
          0
          • J Jay Royall

            Ah, try changing the function to :

            Function MakeSqlsafe(strData)
            MakeSqlSafe = Replace(strData, "'", "''")
            end Function

            N Offline
            N Offline
            new_phoenix 0
            wrote on last edited by
            #5

            It worked!!! Woo Hoo!!! Thank you liqz, for your assistance in resolving this item!!!

            1 Reply Last reply
            0
            • N new_phoenix 0

              I need some assistance in accommodating an apostrophe in an SQL statement using VBA in MS Access. Please help. strSQLHyperion = "SELECT [COUNTRY], [TYPE], [BUSINESS_UNIT], " & _ "[ALT_GROUPING], [PERSONNEL_AREA], [L_R_G], [REGION], [JOB_FUNCTION], " & _ "[PRIMARY_KEY] FROM [TBLHYPERION] " & _ "WHERE [COUNTRY]='" & UCase(rstInputFile![COUNTRY]) & "' " & _ "AND [TYPE]='" & UCase(rstInputFile![Type]) & "' " & _ "AND [BUSINESS_UNIT]='" & UCase(rstInputFile![BUSINESS_UNIT]) & "' " & _ "AND [ALT_GROUPING]='" & UCase(rstInputFile![ALT_GROUPING]) & "' " & _ "AND [PERSONNEL_AREA]='" & UCase(rstInputFile![PERSONNEL_AREA]) & "' " & _ "AND [L_R_G]='" & UCase(rstInputFile![L_R_G]) & "' " & _ "AND [REGION]='" & UCase(rstInputFile![REGION]) & "' " & _ "AND [JOB_FUNCTION]='" & UCase(rstInputFile![JOB_FUNCTION]) & "'" What do I need to do to prevent it from hanging up on the apostrophe? The apostrophe needs to be accepted in: "AND [JOB_FUNCTION]='" & UCase(rstInputFile![JOB_FUNCTION]) & "'" :confused::confused::confused:

              modified on Wednesday, December 3, 2008 11:31 AM

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              Although replacing single apostrophe with double apostrophes will correct your problem, I think you should use parameters instead. Concatenating literal strings leaves you open to sql injections. Also using parameters gives a performance advantage.

              The need to optimize rises from a bad design.My articles[^]

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups