Re: Form variable in query

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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" <tezza@xxxxxxxx> wrote in message
news:%23sxxG$pqGHA.4492@xxxxxxxxxxxxxxxxxxxxxxx
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" <spencer@xxxxxxxxx> wrote in message
news:O65AkqoqGHA.4912@xxxxxxxxxxxxxxxxxxxxxxx
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" <tezza@xxxxxxxx> wrote in message
news:%2301AdjoqGHA.928@xxxxxxxxxxxxxxxxxxxxxxx
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







.



Relevant Pages

  • Re: Not memorizing an output of a function
    ... variables var1 and var2. ... but MLint should warn the user to control this manually. ... I would probably also explain, with a short comment, why you're suppressing M-Lint ...
    (comp.soft-sys.matlab)
  • Re: [PHP] string as file
    ... This is an example of $var1 and $var2. ... I think that neither you nor Greg understands what I'm looking for. ... Use regular expressions or straight string replacements - that's the best way to implement mail-merge type behaviour. ...
    (php.general)
  • Re: Newbie Question
    ... but I am not all that familiar with most of cobol. ... var2 pic 9 ... MOVE VAR2 TO VAR1 is executed, what is actually stored in var1. ... loop through it digit by digit, ...
    (comp.lang.cobol)
  • Re: Newbie Question
    ... var2 pic 9 ... MOVE VAR2 TO VAR1 is executed, what is actually stored in var1. ... loop through it digit by digit, ... Converting the string into a long might get you into problems though. ...
    (comp.lang.cobol)
  • Re: Not memorizing an output of a function
    ... variables var1 and var2. ... code, i.e. in [stuff2]. ... my function which defines the ODE system takes the ...
    (comp.soft-sys.matlab)