SQL Tips for Microsoft Access VBA


I find myself writing more and more code in VBA to handle SQL updates and inserts lately.  A great tip for doing this is to create a query in Access first, change it to an update or append query as needed, then switch it to SQL view….voila!  You have the almost perfect code needed to insert into your VBA event.  Modify the syntax to conform to VBA (usually involves a little bit of clean up with quotes and extra words).  Then declare a variable like strsql as string, and set the new code with a line like strsql = ….then add a docmd.runsql strsql and your set!

This technique has saved me countless hours figuring out the right syntax to get rid of slow queries and move to cleaner faster VBA.  I also turn off warnings before running these commands and turn them on again after.  But that’s another blog!

Related Posts

Access 2016 Relationships

Short blog today, this is a great read on relationships in Access 2016: Access 2016 Relationships Thanks to Microsoft for this one!

Microsoft Access Union Query from Hell. Skill Level: Demi-God

The worst query I ever had to write which powers a master report for the database user.  Here it is.  I don't think I can make it smaller, there were too many weird rules, aggregates, and translations in it.  And we needed to pull different columns based on criteria. ...