Wrox Programmer Forums
|
Access VBA Discuss using VBA for Access programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 30th, 2004, 06:17 PM
Registered User
 
Join Date: Nov 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Running an SQL query in VBA...

Hi,

I'm coming from an ASP background, so maybe I'm going about this the wrong way.

I have Two forms:

In Form A, people make some selections, then I dynamically generate an SQL Query String using VBA and open up Form B, execute the String and place the resulting SUM in a Text field.

I've got everything working, except for figuring out how to call up the SQL query within VBA...! I've done some extensive Googleing, but cannot find a working answer. (I'm using Access 2003).

Should I be looking to Open Form B with my SQL query somehow, or should I open up an unbound form, run the Query in VBA (how), and then fill the text field with the correct value?

Can anyone help or point me in the right direction?

Thanx,

Augusta.

 
Old December 1st, 2004, 04:30 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Augusta,

If you are placing the result in a text field, why do you need the second form? Or do you mean a text box on the second form?

Does the following help:

Generate the SQL string as the caption of a label (it can be hidden) on Form A.
Click a command button on Form A to open Form B.
In the On_Open event of Form B put your VBA code. You will need something like this:
Dim db as Database
Dim rs as Recordset
Dim strResult as String
Dim strSQL AS String
strSQL = Forms!frmFormA!label1.caption
Set db = CurrentDB
Set rs = db.OpenRecordset(strSQL)
strResult = rs(0)
Me!Text1 = strResult




Clive Astley
 
Old December 1st, 2004, 05:07 AM
Registered User
 
Join Date: Nov 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Clive - you pointed me in the right direction.
With the above code I was able to find my other bug (incorrect syntax on the SQL Query itself).

You've just saved me countless hours of troubleshooting - I'm very greatful.

Augusta.

 
Old December 1st, 2004, 05:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 129
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Glad you solved the problem Augusta.

Best wishes,


Clive Astley





Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA - SQL query returns nothing from MySQL Calligra Access VBA 6 September 19th, 2007 04:10 PM
Problem running SQL query on Access DesignsOnline.co.uk Access ASP 1 November 26th, 2006 06:09 PM
SQL Date query in Excel VBA lanewalk Excel VBA 2 September 27th, 2006 02:04 AM
Help With SQL Query in VBA Paul_Tic Access VBA 4 May 30th, 2006 06:34 AM
too few variables when running sql qry through vba Christaaay Access 2 December 19th, 2005 04:21 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.