Easily copy an SQL statement from a query to VBA

        9 votes: *****     7,235 views      1 comment
by Allen Browne, 23 March 2007    (for ALL VERSIONS of Access)

Microsoft Access Tips for Serious Users

Provided by Allen Browne.  Created: March 2007.  Last Updated: November 2008.

Copy SQL statement from query to VBA

Screenshot of SQL to VBA form

Rather than typing complex query statements into VBA code, developers often mock up a query graphically, switch it to SQL View, copy, and paste into VBA.

If you've done it, you know how messy it is sorting out the quotes, and the line endings.

Solution: create a form where you paste the SQL statement, and get Access to create the SQL string for you.

Creating the form

The form just needs two text boxes, and a command button. SQL statements can be quite long, so you put the text boxes on different pages of a tab control.

  1. Create a new form (in design view.)
  2. Add a tab control.
  3. 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.
  4. In the second page of the tab control, add another unbound text box.
    Name it txtVBA, and increase its height and width.
  5. Above the tab control, add a command button.
    Name it cmdSql2Vba.
    Set its 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

Using the form

To use the form:

  1. Open your query in SQL View, and copy the SQL statement to clipboard (Ctrl+C.)
  2. Paste into the first text box (Ctrl+V.)
  3. Click the button.
  4. Paste into a new line in your VBA procedure (Ctrl+V.)

Hint: If you want extra line breaks in your VBA code, press Enter to create those line breaks in the SQL View of the query or in your form.

Access 97 and earlier

These versions lacked the Replace() function. To use the code with those versions, copy the custom Replace() function from this utility.

HomeIndex of tipsTop

Rate this article:  Your rating: PoorYour rating: Not so goodYour rating: AverageYour rating: GoodYour rating: Excellent

This is a cached tutorial, reproduced with permission.

Have your say - comment on this article.

What did you think of 'Easily copy an SQL statement from a query to VBA'?


Ed Ebbs says...

19 Aug 2008

So Cool :-)

Have your say...

E-mail (e-mail address will be kept private)

Comments require approval before being displayed on this page (allow 24 hours).