Go Back   Access World Forums > Microsoft Access Reference > Sample Databases

 
Reply
 
Thread Tools Rate Thread Display Modes
Old 04-24-2017, 06:50 AM   #1
ridders
AWF VIP
Gold Supporter
 
ridders's Avatar
 
Join Date: Jan 2017
Location: Somerset, UK
Posts: 2,468
Thanks: 41
Thanked 588 Times in 543 Posts
ridders will become famous soon enough ridders will become famous soon enough
SQL to VBA and back again

Some years ago, the wonderful Allen Browne created a small utility called Sql2vba
See his website: http://allenbrowne.com/ser-71.html

The purpose was to save developer time:
"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.
"

I found this so useful that I extended the idea to work in the other direction as well : VBA to SQL so I could easily create & load a query based on VBA code.
Useful for testing when the VBA string is long & complex as shown below







To use this, just copy the form frmSql2vba & the 2 modules modFunctions & modResizeForm to your own database

The latter module contains code originally by Jamie Czernak used to automatically resize forms depending on users' screen resolution.
If you don't want to use that part:
a) omit the module modResizeForm
b) remove the line ResizeForm Me from the frmSql2vba Form_Open procedure

Any comments welcomed.
Attached Images
File Type: png #Query1.PNG (39.0 KB, 678 views)
File Type: png SQL.PNG (39.1 KB, 671 views)
File Type: png vba.PNG (43.7 KB, 695 views)
Attached Files
File Type: accdb SQL2VBA.accdb (488.0 KB, 241 views)

__________________
Colin
Access 2010 32-bit, Access 2016 32-bit & 64-bit, SQL Server Express 2014, Windows 10,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, Tapatalk


If this answer has helped, please click the Thanks button and/or click the 'reputation' scales symbol on the left and leave a comment. Cheers!

Last edited by ridders; 05-21-2017 at 09:40 AM. Reason: Added inline images
ridders is offline   Reply With Quote
The Following 3 Users Say Thank You to ridders For This Useful Post:
gleesonc (07-05-2017), NauticalGent (05-02-2017), usm01 (07-08-2017)
Old 05-08-2017, 02:09 AM   #2
Gasman
Access newbie
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 1,523
Thanks: 185
Thanked 216 Times in 202 Posts
Gasman will become famous soon enough Gasman will become famous soon enough
Re: SQL to VBA and back again

Hi Colin,
Thanks very much for this. I spend a fair bit of time creating a query in the designer and then try and amend for VBA.

Just for your info the display for the resize is a little off on my screen running at 1920*1080. The form is still usable, but thought I'd let you know.

Please see attached.
Attached Images
File Type: png sql2vba.png (76.0 KB, 41 views)
__________________
Access novice. Sometimes trying to give something back.
Access 2007
Gasman is offline   Reply With Quote
Old 10-25-2017, 05:09 AM   #3
ridders
AWF VIP
Gold Supporter
 
ridders's Avatar
 
Join Date: Jan 2017
Location: Somerset, UK
Posts: 2,468
Thanks: 41
Thanked 588 Times in 543 Posts
ridders will become famous soon enough ridders will become famous soon enough
Re: SQL to VBA and back again

Thanks for the info Gasman
By chance I have a secondary monitor with exactly that resolution.

Have modified the form size slightly & it now seems to be fine.

NOTE for other users:
No other changes made
If the earlier version worked for you, there is no need to download this
Attached Files
File Type: accdb SQL2VBA.accdb (492.0 KB, 61 views)

__________________
Colin
Access 2010 32-bit, Access 2016 32-bit & 64-bit, SQL Server Express 2014, Windows 10,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, Tapatalk


If this answer has helped, please click the Thanks button and/or click the 'reputation' scales symbol on the left and leave a comment. Cheers!
ridders is offline   Reply With Quote
Old 10-25-2017, 07:02 AM   #4
Gasman
Access newbie
 
Join Date: Sep 2011
Location: Swansea, South Wales,UK
Posts: 1,523
Thanks: 185
Thanked 216 Times in 202 Posts
Gasman will become famous soon enough Gasman will become famous soon enough
Re: SQL to VBA and back again

Thank you Colin.
I obviously have something wrong with my system. the new version hides the buttons even more.

I 've now amended the form, to fit on my system by changing the tab size. Thank you for leaving it to be able to be amended.
Attached Images
File Type: png design mode.PNG (15.4 KB, 38 views)
File Type: png form mode.PNG (12.2 KB, 41 views)
__________________
Access novice. Sometimes trying to give something back.
Access 2007
Gasman is offline   Reply With Quote
Old 11-14-2017, 09:31 AM   #5
ridders
AWF VIP
Gold Supporter
 
ridders's Avatar
 
Join Date: Jan 2017
Location: Somerset, UK
Posts: 2,468
Thanks: 41
Thanked 588 Times in 543 Posts
ridders will become famous soon enough ridders will become famous soon enough
Re: SQL to VBA and back again

I've just done a few minor tweaks & bug fixes to this utility (its now on v3.02 ....though the screenshot says 3.01!)

Main changes:
a) Fixed error 2046 that recently appeared when clicking 'Copy Active Window' button. It used to be fine . . .
b) Added the option to start the VBA statement generated with CurrentDB.Execute or DoCmd.RunSQL according to user preference



The eagle eyed will notice I've swopped the tab order so SQL is now on the left as its the usual starting point for most people using this.
Attached Images
File Type: png Capture.PNG (23.0 KB, 57 views)
Attached Files
File Type: zip SQL2VBA.zip (76.6 KB, 12 views)

__________________
Colin
Access 2010 32-bit, Access 2016 32-bit & 64-bit, SQL Server Express 2014, Windows 10,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
,
To view links or images in signatures your post count must be 10 or greater. You currently have 0 posts.
, Tapatalk


If this answer has helped, please click the Thanks button and/or click the 'reputation' scales symbol on the left and leave a comment. Cheers!

Last edited by ridders; 11-14-2017 at 09:36 AM.
ridders is offline   Reply With Quote
Reply

Tags
sql , vba

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Help I Need Ideas, Setup Local Back-end that Updates Server Back-end panthers1788 General 1 07-02-2013 12:15 AM
Back to back Ashes Series 2013 SpentGeezer Politics & Current Events 4 01-10-2013 08:38 PM
is there any way of getting them back? kevinasam Other Software 1 12-28-2011 03:45 PM
run repors back to back mrrayj60 Reports 4 08-11-2010 11:51 PM
Can't get into back end PaulJK General 3 11-20-2002 12:20 PM




All times are GMT -8. The time now is 12:34 AM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Sponsored Links

How to advertise

Media Kit


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World