PC Review


Reply
Thread Tools Rate Thread

Refer to SQL statements globally

 
 
Kurt Heisler
Guest
Posts: n/a
 
      26th May 2010
I have two large SQL statements that I need to use across several
forms, reports, etc.

Rather than repeat them in each form, I'm trying to store and
reference them globally.

For example, I'd like to store strSQL1 and strSQL2 in a global module,
so I can use either like this in a form:

Random form:

Me!lstResults.RowSource = strSQL1

Random report:

Me.Report.RecordSource = strSQL2

Would I just store the SQL statements in a global module like:

Public Function SQLSource() As String

Dim strSQL1 As String
Dim strSQL2 As String

strSQL1 = "SELECT blah blah"
strSQL2 = "SELECT blah blah"

End Function

If so, how would I refer to it elsewhere. Like:

Me.Report.RecordSource = strSQL2 'need to call the function first
(SQLSource), and then pick the correct SQL

Thanks.


 
Reply With Quote
 
 
 
 
Douglas J. Steele
Guest
Posts: n/a
 
      26th May 2010
Why not store them in a table, and look them up using DLookup when you need
them?

If that's not sufficient, no, what you're proposing won't work. What you can
try is create a new module (not a class module or a module associated with a
form or report) and put the following in it:

Public Const strSQL1 As String = "SELECT blah blah"
Public Const strSQL2 As String = "SELECT blah blah"

--
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/djsteele
Co-author: Access 2010 Solutions, published by Wiley
(no e-mails, please!)



"Kurt Heisler" <(E-Mail Removed)> wrote in message
news:df0ab2e2-c734-4a22-b3ae-(E-Mail Removed)...
>I have two large SQL statements that I need to use across several
> forms, reports, etc.
>
> Rather than repeat them in each form, I'm trying to store and
> reference them globally.
>
> For example, I'd like to store strSQL1 and strSQL2 in a global module,
> so I can use either like this in a form:
>
> Random form:
>
> Me!lstResults.RowSource = strSQL1
>
> Random report:
>
> Me.Report.RecordSource = strSQL2
>
> Would I just store the SQL statements in a global module like:
>
> Public Function SQLSource() As String
>
> Dim strSQL1 As String
> Dim strSQL2 As String
>
> strSQL1 = "SELECT blah blah"
> strSQL2 = "SELECT blah blah"
>
> End Function
>
> If so, how would I refer to it elsewhere. Like:
>
> Me.Report.RecordSource = strSQL2 'need to call the function first
> (SQLSource), and then pick the correct SQL
>
> Thanks.
>
>


 
Reply With Quote
 
 
 
 
Tom van Stiphout
Guest
Posts: n/a
 
      27th May 2010
On Wed, 26 May 2010 14:11:42 -0700 (PDT), Kurt Heisler
<(E-Mail Removed)> wrote:

As a variant on what Doug suggested, I would say create some functions
in a standard module that return sqlstatements. Pass in the form name
into these functions, which would give you flexibility down the road
if you want to do things SLIGHTLY diffent based on which form it is
for. Sample code:
public function GetFirstSqlStatement(frm as Form)
dim sql as string
sql = "select ..."
GetFirstSqlStatement = sql
end function

In the future you could write:
if ControlExists(frm, "myControl") then
sql = sql & " where " & frm.Controls("myControl").ControlSource & "
= " & frm.Controls("myControl").Value

Or some other form-dependent processing, e.g.:
select case frm.name
case "thisForm"
sql = sql & " where StatusID = 1"
case "thatForm"
sql = sql & " where StatusID = 2"
end select

-Tom.
Microsoft Access MVP

>I have two large SQL statements that I need to use across several
>forms, reports, etc.
>
>Rather than repeat them in each form, I'm trying to store and
>reference them globally.
>
>For example, I'd like to store strSQL1 and strSQL2 in a global module,
>so I can use either like this in a form:
>
>Random form:
>
>Me!lstResults.RowSource = strSQL1
>
>Random report:
>
>Me.Report.RecordSource = strSQL2
>
>Would I just store the SQL statements in a global module like:
>
>Public Function SQLSource() As String
>
>Dim strSQL1 As String
>Dim strSQL2 As String
>
>strSQL1 = "SELECT blah blah"
>strSQL2 = "SELECT blah blah"
>
>End Function
>
>If so, how would I refer to it elsewhere. Like:
>
>Me.Report.RecordSource = strSQL2 'need to call the function first
>(SQLSource), and then pick the correct SQL
>
>Thanks.
>

 
Reply With Quote
 
david
Guest
Posts: n/a
 
      27th May 2010
Store the SQL as a query.

That makes it easy to test, find, develop and use.

For example, store SQL1 as query1, then use:

Me!lstResuts.RowSource = "Query1"

Sometimes you wish to modify the sql stored
in a query. You can get it like this:

strSQL1 = codedb.querydefs("Query1").SQL

(david)

"Kurt Heisler" <(E-Mail Removed)> wrote in message
news:df0ab2e2-c734-4a22-b3ae-(E-Mail Removed)...
>I have two large SQL statements that I need to use across several
> forms, reports, etc.
>
> Rather than repeat them in each form, I'm trying to store and
> reference them globally.
>
> For example, I'd like to store strSQL1 and strSQL2 in a global module,
> so I can use either like this in a form:
>
> Random form:
>
> Me!lstResults.RowSource = strSQL1
>
> Random report:
>
> Me.Report.RecordSource = strSQL2
>
> Would I just store the SQL statements in a global module like:
>
> Public Function SQLSource() As String
>
> Dim strSQL1 As String
> Dim strSQL2 As String
>
> strSQL1 = "SELECT blah blah"
> strSQL2 = "SELECT blah blah"
>
> End Function
>
> If so, how would I refer to it elsewhere. Like:
>
> Me.Report.RecordSource = strSQL2 'need to call the function first
> (SQLSource), and then pick the correct SQL
>
> Thanks.
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
IF Statements (Mutliple Statements) =?Utf-8?B?RGVlemVs?= Microsoft Excel Worksheet Functions 3 19th Oct 2006 06:13 AM
if statements, and statements =?Utf-8?B?U3VtIExpbWl0IGFuZCBtYXJraW5n?= Microsoft Excel Worksheet Functions 3 29th Mar 2006 03:25 PM
Is there a Globally Unique ID in SQL Server? Jeff via AccessMonster.com Microsoft Access ADP SQL Server 1 15th Sep 2005 06:13 PM
operator statements, shorting when reusing one of the statements? KR Microsoft Excel Programming 1 4th Aug 2005 06:20 PM
IF statements/ AND Statements Suresh Nair Microsoft Excel Worksheet Functions 0 9th Aug 2003 09:19 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:30 PM.