Form variable in query

T

Tezza

I am using this in a query...

Between [forms]![frmReports].[fldPeriodFrom] And
[forms]![frmReports].[fldPeriodTo]

but i really want to assign the two fields to variables and use the
variables in the query. What's the correct format of the query criteria to
achieve this?

This is the variable coding on the button that runs the report (that is fed
by the query in question)

Dim strDateFrom As Date
Dim strDateTo As Date
strDateFrom = Me.fldPeriodFrom
strDateTo = Me.fldPeriodTo

I have just seen the post 'Global Var in queries' but i'm not yet
experienced in Function coding. So I hope that's not the only way to do it.

TIA
ty
 
M

Michel Walsh

Hi,


You cannot refer to VBA variables inside a query. But you can use a public
function (in a standard module, not a class, not a form) that will return
your variable, from within your query.


-----start of a standard module---

Dim var1 As long
Dim var2 As long

Public Function MyVar1() AS Long
MyVar1=var1
End Function

Public Function MyVar2() AS Long
MyVar2=var2
End Function

----------end--------------------------------



In your SQL statement, use MyVar1() and MyVar2().


Hoping it may help,
Vanderghast, Access MVP
 
T

Tezza

I am using this in a query...
Between [forms]![frmReports].[fldPeriodFrom] And
[forms]![frmReports].[fldPeriodTo]

but i really want to assign the two fields to variables and use the
variables in the query. What's the correct format of the query criteria
to achieve this?

This is the variable coding on the button that runs the report (that is
fed by the query in question)

Dim strDateFrom As Date
Dim strDateTo As Date
strDateFrom = Me.fldPeriodFrom
strDateTo = Me.fldPeriodTo

I have just seen the post 'Global Var in queries' but i'm not yet
experienced in Function coding. So I hope that's not the only way to do
it.

TIA
ty
Hi,


You cannot refer to VBA variables inside a query. But you can use a public
function (in a standard module, not a class, not a form) that will return
your variable, from within your query.


-----start of a standard module---

Dim var1 As long
Dim var2 As long

Public Function MyVar1() AS Long
MyVar1=var1
End Function

Public Function MyVar2() AS Long
MyVar2=var2
End Function

----------end--------------------------------



In your SQL statement, use MyVar1() and MyVar2().


Hoping it may help,
Vanderghast, Access MVP

Well... I get no errors now but i get no records returned either. This is
what i have done...

Module1........

Option Compare Database

Dim var1 As Long
Dim var2 As Long

Public Function MyVar1() As Long
MyVar1 = var1
End Function

Public Function MyVar2() As Long
MyVar2 = var2
End Function

Button on Form (that runs query)...........

var1 = Me.fldPeriodFrom
var2 = Me.fldPeriodTo

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

breakpoints show that var1 and var2 have the appropriate dates assigned, but
MyVar1 and MyVar2 = 0

What have i missed?

TIA
ty
 
J

John Spencer

Module1........

Option Compare Database

Dim var1 As Date
Dim var2 As Date

Public Function MyVar1() As Date
MyVar1 = var1
End Function

Public Function MyVar2() As Date
MyVar2 = var2
End Function


Button on Form (that runs query)...........

var1 = CDate(Me.fldPeriodFrom)
var2 = CDate(Me.fldPeriodTo)

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

Tezza said:
I am using this in a query...

Between [forms]![frmReports].[fldPeriodFrom] And
[forms]![frmReports].[fldPeriodTo]

but i really want to assign the two fields to variables and use the
variables in the query. What's the correct format of the query criteria
to achieve this?

This is the variable coding on the button that runs the report (that is
fed by the query in question)

Dim strDateFrom As Date
Dim strDateTo As Date
strDateFrom = Me.fldPeriodFrom
strDateTo = Me.fldPeriodTo

I have just seen the post 'Global Var in queries' but i'm not yet
experienced in Function coding. So I hope that's not the only way to do
it.

TIA
ty
Hi,


You cannot refer to VBA variables inside a query. But you can use a
public function (in a standard module, not a class, not a form) that
will return your variable, from within your query.


-----start of a standard module---

Dim var1 As long
Dim var2 As long

Public Function MyVar1() AS Long
MyVar1=var1
End Function

Public Function MyVar2() AS Long
MyVar2=var2
End Function

----------end--------------------------------



In your SQL statement, use MyVar1() and MyVar2().


Hoping it may help,
Vanderghast, Access MVP

Well... I get no errors now but i get no records returned either. This is
what i have done...

Module1........

Option Compare Database

Dim var1 As Long
Dim var2 As Long

Public Function MyVar1() As Long
MyVar1 = var1
End Function

Public Function MyVar2() As Long
MyVar2 = var2
End Function

Button on Form (that runs query)...........

var1 = Me.fldPeriodFrom
var2 = Me.fldPeriodTo

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

breakpoints show that var1 and var2 have the appropriate dates assigned,
but MyVar1 and MyVar2 = 0

What have i missed?

TIA
ty
 
T

Tezza

Ok, so the differences here are module variables assigned as Date (changed
from Long) and CDate on the fields. Breakpoints on module now shows content
of varX and MyVarX = 00:00:00. Breakpoint on form code shows var1 and var2
contained dates (01/01/2005 and 31/03/2006) at runtime.

Thanks for your help so far guys, i'm sure it's something pretty simple i've
missed but i just can't figure it.

Anything further?

ty



John Spencer said:
Module1........

Option Compare Database

Dim var1 As Date
Dim var2 As Date

Public Function MyVar1() As Date
MyVar1 = var1
End Function

Public Function MyVar2() As Date
MyVar2 = var2
End Function


Button on Form (that runs query)...........

var1 = CDate(Me.fldPeriodFrom)
var2 = CDate(Me.fldPeriodTo)

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

Tezza said:
I am using this in a query...

Between [forms]![frmReports].[fldPeriodFrom] And
[forms]![frmReports].[fldPeriodTo]

but i really want to assign the two fields to variables and use the
variables in the query. What's the correct format of the query criteria
to achieve this?

This is the variable coding on the button that runs the report (that is
fed by the query in question)

Dim strDateFrom As Date
Dim strDateTo As Date
strDateFrom = Me.fldPeriodFrom
strDateTo = Me.fldPeriodTo

I have just seen the post 'Global Var in queries' but i'm not yet
experienced in Function coding. So I hope that's not the only way to do
it.

TIA
ty
Hi,


You cannot refer to VBA variables inside a query. But you can use a
public function (in a standard module, not a class, not a form) that
will return your variable, from within your query.


-----start of a standard module---

Dim var1 As long
Dim var2 As long

Public Function MyVar1() AS Long
MyVar1=var1
End Function

Public Function MyVar2() AS Long
MyVar2=var2
End Function

----------end--------------------------------



In your SQL statement, use MyVar1() and MyVar2().


Hoping it may help,
Vanderghast, Access MVP

Well... I get no errors now but i get no records returned either. This is
what i have done...

Module1........

Option Compare Database

Dim var1 As Long
Dim var2 As Long

Public Function MyVar1() As Long
MyVar1 = var1
End Function

Public Function MyVar2() As Long
MyVar2 = var2
End Function
also tried assigning variables as Date

Button on Form (that runs query)...........

var1 = Me.fldPeriodFrom
var2 = Me.fldPeriodTo

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

breakpoints show that var1 and var2 have the appropriate dates assigned,
but MyVar1 and MyVar2 = 0

What have i missed?

TIA
ty
 
J

John Spencer

Can only guess that the problem is somewhere in the code?

Are you assigning the values to Var1 and Var2 in the code, before you
execute the query? Can you post the code for the button event that calls
the query.
Tezza said:
Ok, so the differences here are module variables assigned as Date (changed
from Long) and CDate on the fields. Breakpoints on module now shows
content of varX and MyVarX = 00:00:00. Breakpoint on form code shows var1
and var2 contained dates (01/01/2005 and 31/03/2006) at runtime.

Thanks for your help so far guys, i'm sure it's something pretty simple
i've missed but i just can't figure it.

Anything further?

ty



John Spencer said:
Module1........

Option Compare Database

Dim var1 As Date
Dim var2 As Date

Public Function MyVar1() As Date
MyVar1 = var1
End Function

Public Function MyVar2() As Date
MyVar2 = var2
End Function


Button on Form (that runs query)...........

var1 = CDate(Me.fldPeriodFrom)
var2 = CDate(Me.fldPeriodTo)

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

Tezza said:
I am using this in a query...

Between [forms]![frmReports].[fldPeriodFrom] And
[forms]![frmReports].[fldPeriodTo]

but i really want to assign the two fields to variables and use the
variables in the query. What's the correct format of the query
criteria to achieve this?

This is the variable coding on the button that runs the report (that
is fed by the query in question)

Dim strDateFrom As Date
Dim strDateTo As Date
strDateFrom = Me.fldPeriodFrom
strDateTo = Me.fldPeriodTo

I have just seen the post 'Global Var in queries' but i'm not yet
experienced in Function coding. So I hope that's not the only way to
do it.

TIA
ty

Hi,


You cannot refer to VBA variables inside a query. But you can use a
public function (in a standard module, not a class, not a form) that
will return your variable, from within your query.


-----start of a standard module---

Dim var1 As long
Dim var2 As long

Public Function MyVar1() AS Long
MyVar1=var1
End Function

Public Function MyVar2() AS Long
MyVar2=var2
End Function

----------end--------------------------------



In your SQL statement, use MyVar1() and MyVar2().


Hoping it may help,
Vanderghast, Access MVP

Well... I get no errors now but i get no records returned either. This
is what i have done...

Module1........

Option Compare Database

Dim var1 As Long
Dim var2 As Long

Public Function MyVar1() As Long
MyVar1 = var1
End Function

Public Function MyVar2() As Long
MyVar2 = var2
End Function

also tried assigning variables as Date

Button on Form (that runs query)...........

var1 = Me.fldPeriodFrom
var2 = Me.fldPeriodTo

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

breakpoints show that var1 and var2 have the appropriate dates assigned,
but MyVar1 and MyVar2 = 0

What have i missed?

TIA
ty
 
T

Tezza

previous discussion snipped. Current status of various elements:

Module1...

Option Compare Database

Dim var1 As Date
Dim var2 As Date

Public Function MyVar1() As Date
MyVar1 = var1
End Function

Public Function MyVar2() As Date
MyVar2 = var2
End Function

Complete Form Button code...

Dim stDocName As String
var1 = CDate(Me.fldPeriodFrom) 'field contains 01/01/2006
var2 = CDate(Me.fldPeriodTo) 'fieldcontains 31/03/2006
stDocName = "qry_rep_Referred_AND_Taken_ON"
DoCmd.OpenQuery stDocName, acNormal, acEdit

All dates (tbl and frm) are set as Short Date

SQL of the above named query
SELECT tblProject.fldRef, tblProject.fldSurname, tblProject.fldDateOn
FROM tblProject
WHERE (((tblProject.fldDateOn) Between MyVar1() And MyVar2()));
Between #01/01/06# And #31/03/06#
............returns no records

The following SQL returns 26 records
SELECT tblProject.fldRef, tblProject.fldSurname, tblProject.fldDateOn
FROM tblProject
WHERE (((tblProject.fldDateOn) Between #1/1/2006# And #3/31/2006#));

Aah, now that's interesting. I usually use Design View for Queries unless
i'm pasting the sql into code. But the SQL view of this query shows the
'And' date is converted to US format. I'm in the UK and Design View shows
the date as dd/mm/yyyy

Is this my problem? And if so (Office 2003), where do i correct it?

thanks in advance
Ty
 
J

John Spencer

Access expects literal date strings to be in one of two formats. MM/DD/YYyy
or YYYY/MM/DD

However, I would think that using Date as the type for the functions would
prevent the problem from occuring.

Take a look at
International Dates in Access at:
http://allenbrowne.com/ser-36.html

Perhaps that will explain the problem.
 
T

Tezza

John, thanks for the link, this is what i found on that page...

In all versions of Access, JET SQL clauses expect dates in American format.
To demonstrate this, enter any date in the Criteria row under a date field
in Query Design, and then switch to SQL View. In Query Design view, you see
the date according to your local settings, but the SQL statement uses
mm/dd/yy format.

This explains what i found when i switched to sql view, but i don't think it
brings me any closer to a fix.

Breakpoints show that the values assigned to var1 and var2 on the form are
somehow lost by the time they reach the module. For example, on the form
var1 = "01/01/2006" and in the module var1 (and thus MyVar1()) = "00:00:00".

i'm gonna have to resolve it with an ungraceful hack if i can't sort it
(multiple queries and changing the recordsource at report runtime).

Thanks for your help so far, i understand if you now have better things to
do with your time!!

Ty
 
M

Michel Walsh

Hi,


Dim the variable and function as Date instead of as Long

There is 3 main scopes, in VBA: global scope, class (and form, and report)
scope and procedure scope.


As example, under a form (class):

Option Explicit
Dim x As Long

Public Sub sub1()
Dim y As Long
x=33 ' <- the form scope
y=11
z=22 ' only if z is define 'globally'
Debug.Print y
End Sub

Public Sub sub2()
Dim x As Long
x=44 ' <- the procedure scope
End Sub


In Sub2, the x is the x defined in the procedure scope that is visible, the
x defined at the form level (and an x defined at a global level) is not
directly visible. Inside Sub1, the x we reached is the x defined at the
class level; the y we reach is the one defined at the procedure level and
for z, that would be the z defined at the global level.


So, in your case, be sure var1 and var2 are at the right level, or, if you
prefer, defined the one in the declaration section of the module as:


Dim globalVar1 As Date
Dim globalVar2 As Date




then, define, in the same module:


Public Function MyVar1() As Date
MyVar1 = globalVar1
End Function




in the form, use:


globalVar1 = Me.fldPeriodFrom


and DO NOT DIM a variable with the name globalVar1 in the form declaration,
neither in the subroutine having that line of code!


You can, for debug purposes, modify your query like:



SELECT fldRef, fldSurname, fldDateOn, MyVar1(), MyVar2()
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));




and thus, see if the query get the right values, or not.


Hoping it may help,
Vanderghast, Access MVP


Tezza said:
I am using this in a query...

Between [forms]![frmReports].[fldPeriodFrom] And
[forms]![frmReports].[fldPeriodTo]

but i really want to assign the two fields to variables and use the
variables in the query. What's the correct format of the query criteria
to achieve this?

This is the variable coding on the button that runs the report (that is
fed by the query in question)

Dim strDateFrom As Date
Dim strDateTo As Date
strDateFrom = Me.fldPeriodFrom
strDateTo = Me.fldPeriodTo

I have just seen the post 'Global Var in queries' but i'm not yet
experienced in Function coding. So I hope that's not the only way to do
it.

TIA
ty
Hi,


You cannot refer to VBA variables inside a query. But you can use a
public function (in a standard module, not a class, not a form) that
will return your variable, from within your query.


-----start of a standard module---

Dim var1 As long
Dim var2 As long

Public Function MyVar1() AS Long
MyVar1=var1
End Function

Public Function MyVar2() AS Long
MyVar2=var2
End Function

----------end--------------------------------



In your SQL statement, use MyVar1() and MyVar2().


Hoping it may help,
Vanderghast, Access MVP

Well... I get no errors now but i get no records returned either. This is
what i have done...

Module1........

Option Compare Database

Dim var1 As Long
Dim var2 As Long

Public Function MyVar1() As Long
MyVar1 = var1
End Function

Public Function MyVar2() As Long
MyVar2 = var2
End Function

Button on Form (that runs query)...........

var1 = Me.fldPeriodFrom
var2 = Me.fldPeriodTo

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

breakpoints show that var1 and var2 have the appropriate dates assigned,
but MyVar1 and MyVar2 = 0

What have i missed?

TIA
ty
 
M

Michel Walsh

Hi,


Just scrolled down a little bit and saw you already got an answer... :)


Vanderghast, Access MVP

Michel Walsh said:
Hi,


Dim the variable and function as Date instead of as Long

There is 3 main scopes, in VBA: global scope, class (and form, and
report) scope and procedure scope.


As example, under a form (class):

Option Explicit
Dim x As Long

Public Sub sub1()
Dim y As Long
x=33 ' <- the form scope
y=11
z=22 ' only if z is define 'globally'
Debug.Print y
End Sub

Public Sub sub2()
Dim x As Long
x=44 ' <- the procedure scope
End Sub


In Sub2, the x is the x defined in the procedure scope that is visible,
the x defined at the form level (and an x defined at a global level) is
not directly visible. Inside Sub1, the x we reached is the x defined at
the class level; the y we reach is the one defined at the procedure level
and for z, that would be the z defined at the global level.


So, in your case, be sure var1 and var2 are at the right level, or, if you
prefer, defined the one in the declaration section of the module as:


Dim globalVar1 As Date
Dim globalVar2 As Date




then, define, in the same module:


Public Function MyVar1() As Date
MyVar1 = globalVar1
End Function




in the form, use:


globalVar1 = Me.fldPeriodFrom


and DO NOT DIM a variable with the name globalVar1 in the form
declaration, neither in the subroutine having that line of code!


You can, for debug purposes, modify your query like:



SELECT fldRef, fldSurname, fldDateOn, MyVar1(), MyVar2()
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));




and thus, see if the query get the right values, or not.


Hoping it may help,
Vanderghast, Access MVP


Tezza said:
I am using this in a query...

Between [forms]![frmReports].[fldPeriodFrom] And
[forms]![frmReports].[fldPeriodTo]

but i really want to assign the two fields to variables and use the
variables in the query. What's the correct format of the query criteria
to achieve this?

This is the variable coding on the button that runs the report (that is
fed by the query in question)

Dim strDateFrom As Date
Dim strDateTo As Date
strDateFrom = Me.fldPeriodFrom
strDateTo = Me.fldPeriodTo

I have just seen the post 'Global Var in queries' but i'm not yet
experienced in Function coding. So I hope that's not the only way to do
it.

TIA
ty
Hi,


You cannot refer to VBA variables inside a query. But you can use a
public function (in a standard module, not a class, not a form) that
will return your variable, from within your query.


-----start of a standard module---

Dim var1 As long
Dim var2 As long

Public Function MyVar1() AS Long
MyVar1=var1
End Function

Public Function MyVar2() AS Long
MyVar2=var2
End Function

----------end--------------------------------



In your SQL statement, use MyVar1() and MyVar2().


Hoping it may help,
Vanderghast, Access MVP

Well... I get no errors now but i get no records returned either. This is
what i have done...

Module1........

Option Compare Database

Dim var1 As Long
Dim var2 As Long

Public Function MyVar1() As Long
MyVar1 = var1
End Function

Public Function MyVar2() As Long
MyVar2 = var2
End Function
also tried assigning variables as Date

Button on Form (that runs query)...........

var1 = Me.fldPeriodFrom
var2 = Me.fldPeriodTo

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

breakpoints show that var1 and var2 have the appropriate dates assigned,
but MyVar1 and MyVar2 = 0

What have i missed?

TIA
ty
 
T

Tezza

Well, it pretty much looks like dutch to me, on the eve of the hottest UK
July day on record! But it does gives me something to work on, but not at
this late hour. i will get back to it on the morrow, thanks for your
continued support.

Ty

Michel Walsh said:
Hi,


Dim the variable and function as Date instead of as Long

There is 3 main scopes, in VBA: global scope, class (and form, and
report) scope and procedure scope.


As example, under a form (class):

Option Explicit
Dim x As Long

Public Sub sub1()
Dim y As Long
x=33 ' <- the form scope
y=11
z=22 ' only if z is define 'globally'
Debug.Print y
End Sub

Public Sub sub2()
Dim x As Long
x=44 ' <- the procedure scope
End Sub


In Sub2, the x is the x defined in the procedure scope that is visible,
the x defined at the form level (and an x defined at a global level) is
not directly visible. Inside Sub1, the x we reached is the x defined at
the class level; the y we reach is the one defined at the procedure level
and for z, that would be the z defined at the global level.


So, in your case, be sure var1 and var2 are at the right level, or, if you
prefer, defined the one in the declaration section of the module as:


Dim globalVar1 As Date
Dim globalVar2 As Date




then, define, in the same module:


Public Function MyVar1() As Date
MyVar1 = globalVar1
End Function




in the form, use:


globalVar1 = Me.fldPeriodFrom


and DO NOT DIM a variable with the name globalVar1 in the form
declaration, neither in the subroutine having that line of code!


You can, for debug purposes, modify your query like:



SELECT fldRef, fldSurname, fldDateOn, MyVar1(), MyVar2()
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));




and thus, see if the query get the right values, or not.


Hoping it may help,
Vanderghast, Access MVP


Tezza said:
I am using this in a query...

Between [forms]![frmReports].[fldPeriodFrom] And
[forms]![frmReports].[fldPeriodTo]

but i really want to assign the two fields to variables and use the
variables in the query. What's the correct format of the query criteria
to achieve this?

This is the variable coding on the button that runs the report (that is
fed by the query in question)

Dim strDateFrom As Date
Dim strDateTo As Date
strDateFrom = Me.fldPeriodFrom
strDateTo = Me.fldPeriodTo

I have just seen the post 'Global Var in queries' but i'm not yet
experienced in Function coding. So I hope that's not the only way to do
it.

TIA
ty
Hi,


You cannot refer to VBA variables inside a query. But you can use a
public function (in a standard module, not a class, not a form) that
will return your variable, from within your query.


-----start of a standard module---

Dim var1 As long
Dim var2 As long

Public Function MyVar1() AS Long
MyVar1=var1
End Function

Public Function MyVar2() AS Long
MyVar2=var2
End Function

----------end--------------------------------



In your SQL statement, use MyVar1() and MyVar2().


Hoping it may help,
Vanderghast, Access MVP

Well... I get no errors now but i get no records returned either. This is
what i have done...

Module1........

Option Compare Database

Dim var1 As Long
Dim var2 As Long

Public Function MyVar1() As Long
MyVar1 = var1
End Function

Public Function MyVar2() As Long
MyVar2 = var2
End Function
also tried assigning variables as Date

Button on Form (that runs query)...........

var1 = Me.fldPeriodFrom
var2 = Me.fldPeriodTo

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

breakpoints show that var1 and var2 have the appropriate dates assigned,
but MyVar1 and MyVar2 = 0

What have i missed?

TIA
ty
 
J

John Spencer

I am baffled. This technique should work.

AUGHH!!!! I just realized something. I don't believe that I've overlooked
this. In my defense, I avoid global variables as I believe they are
generally not a good idea since they can be reset (whenever unhandled errors
occur) or can get unexpected values (set to one value in one place and then
accidentally used elsewhere with the new value).

Try the following:
Option Compare Database
Option Explicit
PUBLIC var1 As Date '<<<<<< PUBLIC versus DIM
PUBLIC var2 As Date

If you are going to use Global variables, I suggest that you might want to
adopt a naming convention to flag them visually. Something like
g_Var1 and g_Var2. Any variable that starts with "g_" would be a global
variable.

I'm willing to bet that you don't have Option Explicit in all your code
modules. If you did you would have gotten an error when you were assigning
the values to var1 and var2. Using OPTION Explicit is a practice that
should be followed. You can set Access so that whenever a module is created
the Option Explicit is automatically added to the declarations section of
the module.

In VBA window,
-- Select Tools: Options from the menu
-- On the Editor window, check "Require Variable Declaration"
 
T

Tezza

Wahoooooooo! Thank you so much for persisting with it, John. Much
appreciated. And...

g_ prefix added to Globals
"Require Variable Declaration" checked

thanks again
Ty
 
T

Tezza

Got it sorted now, but thanks a lot for your time Michel.

best regards
Ty

Michel Walsh said:
Hi,


Dim the variable and function as Date instead of as Long

There is 3 main scopes, in VBA: global scope, class (and form, and
report) scope and procedure scope.


As example, under a form (class):

Option Explicit
Dim x As Long

Public Sub sub1()
Dim y As Long
x=33 ' <- the form scope
y=11
z=22 ' only if z is define 'globally'
Debug.Print y
End Sub

Public Sub sub2()
Dim x As Long
x=44 ' <- the procedure scope
End Sub


In Sub2, the x is the x defined in the procedure scope that is visible,
the x defined at the form level (and an x defined at a global level) is
not directly visible. Inside Sub1, the x we reached is the x defined at
the class level; the y we reach is the one defined at the procedure level
and for z, that would be the z defined at the global level.


So, in your case, be sure var1 and var2 are at the right level, or, if you
prefer, defined the one in the declaration section of the module as:


Dim globalVar1 As Date
Dim globalVar2 As Date




then, define, in the same module:


Public Function MyVar1() As Date
MyVar1 = globalVar1
End Function




in the form, use:


globalVar1 = Me.fldPeriodFrom


and DO NOT DIM a variable with the name globalVar1 in the form
declaration, neither in the subroutine having that line of code!


You can, for debug purposes, modify your query like:



SELECT fldRef, fldSurname, fldDateOn, MyVar1(), MyVar2()
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));




and thus, see if the query get the right values, or not.


Hoping it may help,
Vanderghast, Access MVP


Tezza said:
I am using this in a query...

Between [forms]![frmReports].[fldPeriodFrom] And
[forms]![frmReports].[fldPeriodTo]

but i really want to assign the two fields to variables and use the
variables in the query. What's the correct format of the query criteria
to achieve this?

This is the variable coding on the button that runs the report (that is
fed by the query in question)

Dim strDateFrom As Date
Dim strDateTo As Date
strDateFrom = Me.fldPeriodFrom
strDateTo = Me.fldPeriodTo

I have just seen the post 'Global Var in queries' but i'm not yet
experienced in Function coding. So I hope that's not the only way to do
it.

TIA
ty
Hi,


You cannot refer to VBA variables inside a query. But you can use a
public function (in a standard module, not a class, not a form) that
will return your variable, from within your query.


-----start of a standard module---

Dim var1 As long
Dim var2 As long

Public Function MyVar1() AS Long
MyVar1=var1
End Function

Public Function MyVar2() AS Long
MyVar2=var2
End Function

----------end--------------------------------



In your SQL statement, use MyVar1() and MyVar2().


Hoping it may help,
Vanderghast, Access MVP

Well... I get no errors now but i get no records returned either. This is
what i have done...

Module1........

Option Compare Database

Dim var1 As Long
Dim var2 As Long

Public Function MyVar1() As Long
MyVar1 = var1
End Function

Public Function MyVar2() As Long
MyVar2 = var2
End Function
also tried assigning variables as Date

Button on Form (that runs query)...........

var1 = Me.fldPeriodFrom
var2 = Me.fldPeriodTo

Query criteria on date field.............

Between MyVar1() And MyVar2()

SQL View...
SELECT fldRef, fldSurname, fldDateOn
FROM tblProject
WHERE (((fldDateOn) Between MyVar1() And MyVar2()));

breakpoints show that var1 and var2 have the appropriate dates assigned,
but MyVar1 and MyVar2 = 0

What have i missed?

TIA
ty
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top