Access asking for a query parameter unnecessarily? (Access VBA and SQL)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • NeoPa
    Recognized Expert Moderator MVP
    • Oct 2006
    • 32645

    #16
    To make this Injection-proof, simply add the following code after line #5 :
    Code:
    Dim strCheck As String
    
    If Replace(Replace(Forms!frmMain![txtSearch], _
                       """", _
                       ""), _
               "'", _
               "") <> Forms!frmMain![txtSearch] Then
        'Handle SQL Injection here
    End If
    PS. Alternatively, you may decide simply to strip out any quote characters and pass that string along, instead of advertising that your code is on to them ;)

    Comment

    • patjones
      Recognized Expert Contributor
      • Jun 2007
      • 931

      #17
      Thanks to the both of you for addressing my issue. I will absorb this overnight, and let you know how it works out!

      Pat

      Comment

      • ChipR
        Recognized Expert Top Contributor
        • Jul 2008
        • 1289

        #18
        An alternative is the Instr function.
        Code:
        If Instr(Forms!frmMain![txtSearch], """") > 0 Or _
           Instr(Forms!frmMain![txtSearch], "'") > 0 Then

        Comment

        • patjones
          Recognized Expert Contributor
          • Jun 2007
          • 931

          #19
          Thank you all. Things work quite well.

          ADezii -

          I put all the code in the Click event for the command button as you suggested, and removed the parameter formalism. I also really like the tests for existence of qdf and rst at the end of the sub and have added those as well.

          NeoPa -

          I like the Replace method, but am simply assigning those nested Replace statements to a string variable and then putting that into the SQL string rather than doing the test.

          I appreciate the time and effort. Thanks so much.

          Comment

          • NeoPa
            Recognized Expert Moderator MVP
            • Oct 2006
            • 32645

            #20
            It's always a pleasure working with someone who's prepared to get involved Pat. Responding so fully, as you do, keeps everyone in the picture and everyone feeling valued.

            BTW I hope you agree with me that ADezii's post #15 is the most apposite for the question (as I've selected it).

            Comment

            • patjones
              Recognized Expert Contributor
              • Jun 2007
              • 931

              #21
              ADezii's code is basically what I implemented and consider the best solution.

              I'm still not sure why the parameter didn't work though. I usually use unbound forms, pulling the data from the tables with ADO and then assigning text box values item by item referencing the recordset. I've found that ADO makes use of parameters straightfoward.

              Thanks!

              Comment

              • ChipR
                Recognized Expert Top Contributor
                • Jul 2008
                • 1289

                #22
                I recommend you try using bound forms the next chance you get. Being a programmer before a database administrator, I started with unbound forms and lots of code, but have learned enough to use bound forms. Funny how the "easy" way was not as easy as just writing code. Anyway, the result is much cleaner and faster, and I have redone most of my forms.

                Comment

                • patjones
                  Recognized Expert Contributor
                  • Jun 2007
                  • 931

                  #23
                  Indeed, I am using a bound form for this project...reall y my first time using one. It does seem to cut down on the code quite a bit. It also makes edits really easy.

                  Thanks for your insight Chip.

                  Comment

                  Working...