why put #?!

G

Guest

Hello

I am reviewing a book to learn some more vba for access and came accross the following code to get the list of selected item from a multi select list box
****
Private Sub lstCompanies_AfterUpdate(

' Print the list of selected items to the tex
' box txtSelected
Dim varItem As Varian
Dim strList As Strin

#If False The
' Here's the slow way to list the selected companies
' This code won't execute, since it's commente
' out the the #If False Then...#End I
Dim intI As Intege
With Me!lstCompanie
For intI = 0 To .ListCount -
If .Selected(intI) The
strList = strList & .Column(0, varItem) & vbCrL
End I
Next int
End Wit
#End I
' Here's the better way to do it
With Me!lstCompanie
If .MultiSelect = 0 The
Me!txtSelected = .Valu
Els
For Each varItem In .ItemsSelecte
strList = strList & .Column(0, varItem) & vbCrL
Next varIte
Me!txtSelected = strLis
End I
End Wit
End Su
****

why/what does the "#" do in the authors coding?

thank

Daniel
 
G

Graham R Seach

Daniel,

# is a conditional compiler directive. It is used to define the code that
will be compiled with the application. For the code to be compiled, the
expression must equal True.

In the case you've shown, the code between the #If and #End If statements
will never be compiled with the application, and will never execute, because
False will always equal False. It appears the author has used this technique
to show the readers how something can be done, without actually allowing it
to run. You'll notice the author states "Here's the slow way to list the
selected companies.", so (s)he is simply showing how to do it, whereas
further down (s)he states "Here's the better way to do it."

If you don't want to use that method, you can safely remove the entire
#If...#End If block, including the code between them.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Daniel said:
Hello,

I am reviewing a book to learn some more vba for access and came accross
the following code to get the list of selected item from a multi select list
box:
 
A

Albert D. Kallal

Gee, that is a cute trick, is it not?

I always been aware of conditional compiling..but never really found a use
for it.

Of course, with access 2000 and later, we have a option in the vb editor to
"comment" out code.

However, before access 2000, if you did not have that feature, then that
"trick" of using compiler directives is a rather good idea..and one I never
seen in ms-access.

So, while it is great idea...we now have the ability to highlight a block of
code..and comment it "in" or comment it "out".

This is means that the compiler idea is not much needed, but on the other
hand, for testing, and eliminating some code without have to
comment/un-comment out the code, that is still a useful trick...
 
G

Graham R Seach

Albert,

<<Gee, that is a cute trick, is it not?>>
Sure is! I've never seen it used before.

But I think the only useful part of the code is the [If False] part, which
allows you to write code for various scenarios without having to comment out
sections of code for one application, and uncomment them for another.

You can use the
If THISCASE Then
'Run this code
Else
'Run this other code
End If
construct, where THISCASE can be a public constant whose value you only need
change in one place, in order to get the code to execute.

Daniel: If you're still listening in, from which book did you get this?

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 
G

George Nicholson

Very useful when supporting multiple versions of Access. Depending on the
version being run, you can execute an Access 97 block of code but the
compiler will ignore "unknown", advanced AccessXP syntax in other blocks.
Everything is available, nothing is commented out. The compiler only "sees"
the code for its own applicable version.

You can leave legacy code in place while still supporting new and better
approaches.
--
George Nicholson

Remove 'Junk' from return address.


Graham R Seach said:
Albert,

<<Gee, that is a cute trick, is it not?>>
Sure is! I've never seen it used before.

But I think the only useful part of the code is the [If False] part, which
allows you to write code for various scenarios without having to comment out
sections of code for one application, and uncomment them for another.

You can use the
If THISCASE Then
'Run this code
Else
'Run this other code
End If
construct, where THISCASE can be a public constant whose value you only need
change in one place, in order to get the code to execute.

Daniel: If you're still listening in, from which book did you get this?

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Albert D. Kallal said:
Gee, that is a cute trick, is it not?

I always been aware of conditional compiling..but never really found a use
for it.

Of course, with access 2000 and later, we have a option in the vb editor to
"comment" out code.

However, before access 2000, if you did not have that feature, then that
"trick" of using compiler directives is a rather good idea..and one I never
seen in ms-access.

So, while it is great idea...we now have the ability to highlight a
block
of
code..and comment it "in" or comment it "out".

This is means that the compiler idea is not much needed, but on the other
hand, for testing, and eliminating some code without have to
comment/un-comment out the code, that is still a useful trick...


--
Albert D. Kallal (Access MVP)
Edmonton, Alberta Canada
(e-mail address removed)
http://www.attcanada.net/~kallal.msn
 
M

Marshall Barton

Graham said:
Albert,

<<Gee, that is a cute trick, is it not?>>
Sure is! I've never seen it used before.

But I think the only useful part of the code is the [If False] part, which
allows you to write code for various scenarios without having to comment out
sections of code for one application, and uncomment them for another.

You can use the
If THISCASE Then
'Run this code
Else
'Run this other code
End If
construct, where THISCASE can be a public constant whose value you only need
change in one place, in order to get the code to execute.

Daniel: If you're still listening in, from which book did you get this?


Hey Graham, RTFM <gdr>

In AXP Help look up #Const in the index and go from there.

The only big thing missing from the # statements is
#Function.
 
G

Graham R Seach

Marsh,

I already kew about #Const, but I was referring to instances when you wanted
to selectively run certain code, not selectively compile it. Just like in
..NET where you have a BooleanSwitch to turn Tracing on and off.

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html


Marshall Barton said:
Graham said:
Albert,

<<Gee, that is a cute trick, is it not?>>
Sure is! I've never seen it used before.

But I think the only useful part of the code is the [If False] part, which
allows you to write code for various scenarios without having to comment out
sections of code for one application, and uncomment them for another.

You can use the
If THISCASE Then
'Run this code
Else
'Run this other code
End If
construct, where THISCASE can be a public constant whose value you only need
change in one place, in order to get the code to execute.

Daniel: If you're still listening in, from which book did you get this?


Hey Graham, RTFM <gdr>

In AXP Help look up #Const in the index and go from there.

The only big thing missing from the # statements is
#Function.
 
M

Marshall Barton

Graham said:
Marsh,

I already kew about #Const, but I was referring to instances when you wanted
to selectively run certain code, not selectively compile it. Just like in
.NET where you have a BooleanSwitch to turn Tracing on and off.

I knew I was confused about something, Graham :-(
Sorry for the slur on your knowledge.
 
G

Graham R Seach

Marsh,

Of course, we all use If...Then constructs every day, but (and I know it
sounds silly), I'd just never thought to use a simple If <Const> construct
to selectively run code on an application-wide scale. I've used it in .NET,
but it just never occurred to me to use the same principle in Access. :)

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia

Microsoft Access 2003 VBA Programmer's Reference
http://www.wiley.com/WileyCDA/WileyTitle/productCd-0764559036.html
 

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