A Microsoft Office (Excel, Word) forum. OfficeFrustration

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Running & Setting Up Queries
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

VB Code - Query



 
 
Thread Tools Display Modes
  #1  
Old July 14th, 2008, 04:43 PM posted to microsoft.public.access.queries
Rus925
external usenet poster
 
Posts: 10
Default VB Code - Query

I have VB code for a query, but I don't know how to turn it into an actual
query. Any advice? Thanks!

Here's the basic code (from
http://www.microsoft.com/office/comm...b&sloc=en-us):

Dim str AS string
str= " SELECT "

if check1 then str=str & " field1,"
if check2 then str = str & " field2,"
....

' remove the extra coma ( I assume at least ONE field will be selected)
str=Left(str, len(str)-1)

'complete the statement
str= str & " FROM tableName ... "

'use the string a record source (form/report) or row source (combo box,
list box)

  #2  
Old July 14th, 2008, 10:44 PM posted to microsoft.public.access.queries
[email protected]
external usenet poster
 
Posts: 744
Default VB Code - Query

On Jul 14, 10:43*am, Rus925 wrote:
I have VB code for a query, but I don't know how to turn it into an actual
query. *Any advice? *Thanks! *

```
Create a querydef object, (make sure it has a name), assign the .SQL
property of the object to your SQL statement/function that returns the
SQL Statement, and then append it to the querydefs collection.

Okay, now tell WHY you need this? Are you building a filter on the
fly for your report? There's an answer for that at Access Web.
www.mvps.org/access Search for it.

Do you intend to just build a query where you can modify the SQL on
the fly? (Running out of ideas...)
  #3  
Old July 14th, 2008, 11:08 PM posted to microsoft.public.access.queries
Rus925
external usenet poster
 
Posts: 10
Default VB Code - Query

Thanks! Exactly: I am using VB to dynamically generate SQL code based on the
user's input in a form (which will also ask for the parameters of the query).
Basically, I want the user to quickly be able to choose which fields he/she
wants visible in the report and by which field he/she wants to sort from the
form, along with what he/she wants to search for.

How do I create a querydef object? All I have right now is a module with
the code.

" wrote:

On Jul 14, 10:43 am, Rus925 wrote:
I have VB code for a query, but I don't know how to turn it into an actual
query. Any advice? Thanks!

```
Create a querydef object, (make sure it has a name), assign the .SQL
property of the object to your SQL statement/function that returns the
SQL Statement, and then append it to the querydefs collection.

Okay, now tell WHY you need this? Are you building a filter on the
fly for your report? There's an answer for that at Access Web.
www.mvps.org/access Search for it.

Do you intend to just build a query where you can modify the SQL on
the fly? (Running out of ideas...)

  #4  
Old July 14th, 2008, 11:59 PM posted to microsoft.public.access.queries
Douglas J. Steele
external usenet poster
 
Posts: 9,313
Default VB Code - Query

Dim qdfNew As DAO.QueryDef
Dim strSQL As String

strSQL = "SELECT ...."
Set qdfNew = CurrentDb.CreateQueryDef("QueryName", strSQL)


--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no private e-mails, please)


"Rus925" wrote in message
...
Thanks! Exactly: I am using VB to dynamically generate SQL code based on
the
user's input in a form (which will also ask for the parameters of the
query).
Basically, I want the user to quickly be able to choose which fields
he/she
wants visible in the report and by which field he/she wants to sort from
the
form, along with what he/she wants to search for.

How do I create a querydef object? All I have right now is a module with
the code.

" wrote:

On Jul 14, 10:43 am, Rus925 wrote:
I have VB code for a query, but I don't know how to turn it into an
actual
query. Any advice? Thanks!

```
Create a querydef object, (make sure it has a name), assign the .SQL
property of the object to your SQL statement/function that returns the
SQL Statement, and then append it to the querydefs collection.

Okay, now tell WHY you need this? Are you building a filter on the
fly for your report? There's an answer for that at Access Web.
www.mvps.org/access Search for it.

Do you intend to just build a query where you can modify the SQL on
the fly? (Running out of ideas...)



 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 05:10 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.