FilterForm via VBA

D

Don

I know I am close, I just don't have the quotes in the
right spot....

I have a button in which I want to apply a form filter
the Field I want to filter is called AssignedTo. I want
to filter it based upon a variable called ITTech stored in
the database

Me.Filter = "AssignedTo=" & ITTEch
'Me.Filter = "AssigedTo = 'ITTech'"
Me.FilterOn = True

I've tried single quotes, but then doesn't that use ITTech
as literal text (which of course I don't want)

Also....

Can I filter on two fields in code easily? Say,
AssignedTo = ITTech and Status <> "Resolved" ?

Thanks in Advance for any insight. I am enjoying learning
VBA, but am beginning to realize the importance of exact
syntax.

Don
 
R

Rick Brandt

Don said:
I know I am close, I just don't have the quotes in the
right spot....

I have a button in which I want to apply a form filter
the Field I want to filter is called AssignedTo. I want
to filter it based upon a variable called ITTech stored in
the database

Me.Filter = "AssignedTo=" & ITTEch
'Me.Filter = "AssigedTo = 'ITTech'"
Me.FilterOn = True

I've tried single quotes, but then doesn't that use ITTech
as literal text (which of course I don't want)

If ITTEch is a text field...

Me.Filter = "AssignedTo='" & ITTEch & "'"
Me.FilterOn = True

Also....

Can I filter on two fields in code easily? Say,
AssignedTo = ITTech and Status <> "Resolved" ?

Me.Filter = "AssignedTo='" & ITTEch & "' AND Status = 'Resolved'"
Me.FilterOn = True
 
J

Jim Allensworth

I know I am close, I just don't have the quotes in the
right spot....

I have a button in which I want to apply a form filter
the Field I want to filter is called AssignedTo. I want
to filter it based upon a variable called ITTech stored in
the database

Me.Filter = "AssignedTo=" & ITTEch
'Me.Filter = "AssigedTo = 'ITTech'"
Me.FilterOn = True

I've tried single quotes, but then doesn't that use ITTech
as literal text (which of course I don't want)

Also....

Can I filter on two fields in code easily? Say,
AssignedTo = ITTech and Status <> "Resolved" ?

Thanks in Advance for any insight. I am enjoying learning
VBA, but am beginning to realize the importance of exact
syntax.

Don
About the variable: By stored in the database do you mean in the
module? And what is the scope of the variable? Is it module level or
procedure level? And what is it's type?

Assuming the variable is of type text:
Me.Filter = "AssignedTo='" & ITTEch & "'"

Note I've put single quotes before the second double quote and between
the last 2 double quotes.

Yes you can use multiple fields for filtering. Think of it as an SQL
WHERE clause without the WHERE:

Me.Filter = "AssignedTo='" & ITTEch & "' AND Status <> 'Resolved'"

Note the single quotes again.

- Jim
 
D

Don Cardoza

Jim...

if it matters, ITTech is a Public variable I sotred in a module called Misc

Does this matter?
 
D

Don Cardoza

Forgive my silly question....

Is there a rule a thumb where I should use single quotes vs. When I use
double?
 
J

Jim Allensworth

It only matters in terms of scope. A public variable is in scope for
the instance of the application. Unless a runtime error occurs - that
might cause it to be reset. But, no, type is most important.
 
J

Jim Allensworth

I don't know of a single rule of thumb. To keep in mind: some strings
can include single or double quotes - O'Conner or 10' 45" - for
example.

You could use a Chr(34) instead to wrap your string values. Which
handles single quotes fine.

To manage single and double quotes you can wrap them in a constant for
double, double quotes. Like...

const cQuote="""" 'Thats two quotes

Me.Filter = "AssignedTo=" & cQuote & ITTEch & cQuote

- Jim
 

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