April 18, 2024


Welcome to the core

MS Access – Convert SQL > VBA

SQL Query Into to VBA Language – The Converter

This process will enable to you build a SQL query to VBA converter.

As VBA cant work with a direct paste of SQL into the VBA scripts this convert will add it line by line with the correct quotation marks and joins.

Steps to produce – In Access 

Create a new form (in design view.)

Add a tab control.

In the first page of the tab control, add a unbound text box.

Set its Name property to txtSql.

Increase its Height and Width so you can see many long lines at once.


On the second page of the tab control, add another unbound text box.

Name it txtVBA, and increase its height and width.


Above the tab control, add a command button.

Name it cmdSql2Vba.

Set the On Click property to [Event Procedure].

Click the Build button (…) beside this property.

When Access opens the code window, set up the code like this:

Private Sub cmdSql2Vba_Click()

Dim strSql As String

‘Purpose:   Convert a SQL statement into a string to paste into VBA code.

Const strcLineEnd = ” “” & vbCrLf & _” & vbCrLf & “”””


If IsNull(Me.txtSQL) Then



strSql = Me.txtSQL

strSql = Replace(strSql, “”””, “”””””)  ‘Double up any quotes.

strSql = Replace(strSql, vbCrLf, strcLineEnd)

strSql = “strSql = “”” & strSql & “”””

Me.txtVBA = strSql


RunCommand acCmdCopy

End If

End Sub


To use the form:

Open your query in SQL View, and copy the SQL statement to clipboard (Ctrl+C.)

Paste into the first text box (Ctrl+V.)

Click the button.

Paste into a new line in your VBA procedure (Ctrl+V.)

Copyright © All rights reserved. | Newsphere by AF themes.