HOW TO USE VARIABLES IN MACROS

E

ezil

As i migrated from ms-excel to access, access programming is new for me.
I need to use variables in many places i dont know how to use for e.g.

abc="ok" (abc is the variable i want to use in programme)
docmd.openrecord,"recordname",acreport,,filed1= abc

But while executing, it asks for abc value once again through parameter
value window which i already declared.
how to use variable in access i tried all the combination of & or "" or '
but it is not working
 
A

Albert D. Kallal

ezil said:
As i migrated from ms-excel to access, access programming is new for me.
I need to use variables in many places i dont know how to use for e.g.

abc="ok" (abc is the variable i want to use in programme)
docmd.openrecord,"recordname",acreport,,filed1= abc

But while executing, it asks for abc value once again through parameter
value window which i already declared.
how to use variable in access i tried all the combination of & or "" or '
but it is not working

Building expressions is one of the challenges you face in learning.

First of all, there not a openReocrd command. Lets assume however you mean
OpenReport.

You have pass the "where" clause of the open form a legitimate expression.
There is MANY ways you could do this.

You could go:

abc = "field1 = 'abc'"

DoCmd.OpenReport,acViewPreview,,abc

If have abc = "ok"

then
debug.print abc ---->gives ok

debug.print print (abc & "=" & " 'ok' ") --------> gives "abc = 'ok'

so, for where clause, you can/could stuff the WHOLE string right into the
variable and use:

abc = "field1 = 'ok' "
DoCmd.OpenReport,acViewPreview,,abc

However, if you go
abc = " 'ok'"
DoCmd.OpenReport,acViewPreview,,"field1 = " & abc

So, you can build that expression many ways, but at the end of the day, you
need a string, and for the filter or where clause, you need to enclose the
text with quotes. that way I used those single quotes. For NUMBER only
fields, you do not need those quotes.

So,

abc = "1234"
DoCmd.OpenReport,acViewPreview,,"InvoiceNumber = " & abc

or

abc = "InvoiceNumber = 1234 "
DoCmd.OpenReport,acViewPreview,,abc

Note how I built the resulting expression with a number that does not have
quotes around it.
 
T

Tom van Stiphout

On Sun, 1 Nov 2009 04:58:01 -0800, ezil

Use this:
docmd.openrecord,"recordname",acreport,,"filed1='" & abc & "'"
Note how I'm wrapping the VALUE of abc in single-quotes, required
since this is a text value.
Btw, there is no DoCmd.OpenRecord.

-Tom.
Microsoft Access MVP
 

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