A better way to add apostrophes with SQL? - Access VBA -


i'm in middle of creating notepad application access - stay sharp. i've created form housing notepad , several buttons varied functions. notepad saved table called tblcontents memo - because of limit of 255 characters found text.

i copied large amount of text , became aware of apostrophe problem. see when saving text table run sql statement which, when adding apostrophes, needs of syntax (which can't remember @ point) in order run.

to maintain user entered, apostrophes , all, there way add using same sql? don't want loop through input , have remove apostrophes.

here's code adding input user:

'save memo public sub savememo()      'examine memo object forms!frmnotepad!memo      'set focus memo in order length     .setfocus      if len(.text) > 0          'save table         dim memocontents string         memocontents = .text          dim strsql string         strsql = "insert tblcontents (contents)" & _                  "values ( '" & memocontents & "' ); "          'set database , execute sql         dim db database         set db = currentdb          db.execute strsql, dbfailonerror      else          msgbox ("nothing save!")      end if  end   end sub 

if you've been digging around here (and elsewhere) looking sample code may have encountered term sql injection. mechanism whereby user input containing apostrophes (and other funny business) can have surprising , serious side-effects.

if operating within access save trouble using recordset update table. instead of running insert statement use

dim cdb dao.database, rst dao.recordset set cdb = currentdb set rst = cdb.openrecordset("tblcontents", dbopendynaset) rst.addnew rst!contents = memocontents rst.update rst.close set rst = nothing set cdb = nothing 

that way don't need worry escaping characters or getting tripped sql injection.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -