Comment Block Code

D

Dataman

Is there a way to comment out lines of code in a module using VBA?

Example:

If condition is true, comment lines 3 and 4
elseIf condition is false, Uncomment lines 3 and 4
endif

Thanks
 
J

Jeff Boyce

Are you asking about a way to dynamically alter the code? If so, why? (and
it sure sounds "dangerous"!)

Could you simply use your If...Then statements (or Select Case) to bypass
lines 3 & 4, as appropriate?

Regards

Jeff Boyce
Microsoft Office/Access MVP
 
R

ruralguy via AccessMonster.com

Are you talking about a Compile time action? #IF...#ELSE...#END IF
 
D

Dataman

I am creating a filtering function containing 100 possible criteria. I am
modifying the the queryDef using this function. The query will only accept
so many criteria before it quits working. In the example below, if the str2
variable is null, the string ""Like '*' Or (qryX.txtCnty) Is Null" is
returned. This shows up in the query grid as a criteria. If str2 is null
then I would like NO criteria to show up in the query. Here is a partial
code example.

If IsNull(frm!txtCountyHolder.Value) Then 'County
str1 = "Like '*' Or (qryX.txtCnty) Is Null"
Else
str1 = frm!txtCountyHolder.Value
End If

'-------------------------------------------
If IsNull(frm!txtCategoryHolder.Value) Then 'Category
str2 = "Like '*' Or (qryX.txtCat) Is Null"
Else
str2 = frm!txtCategoryHolder.Value
End If

1 strSQL = "SELECT qryX.PropertyID, qryX.txtCnty, qryX.txtCat "

2 strSQL = strSQL & "FROM qryX "

3 strSQL = strSQL & "Where (((qryX.txtCnty) " 'County

4 strSQL = strSQL & str1

5 strSQL = strSQL & ") AND ((qryX.txtCat) " 'Category

6 strSQL = strSQL & str2));
 
J

John Spencer

Try something along the lines of the following.


If IsNull(frm!txtCountyHolder.Value) Then 'County
str1 = ""
Else
str1 = " AND qryX.txtCounty = """ & frm!txtCountyHolder.Value & """"
End If

'-------------------------------------------
If IsNull(frm!txtCategoryHolder.Value) Then 'Category
str2 = ""
Else
str2 = " AND qryX.TxtCat = """ & frm!txtCategoryHolder.Value & """"
End If

1 strSQL = "SELECT qryX.PropertyID, qryX.txtCnty, qryX.txtCat "

2 strSQL = strSQL & "FROM qryX "

3 strSQL = strSQL & "Where TRUE " 'County

4 strSQL = strSQL & str1

5 strSQL = strSQL & str2

Actually I would use something more along the lines of
Dim StrWhere as String
Dim strSQL as String

IF IsNull(frm!txtCountyHolder) = False Then
strWhere = strWhere & " AND qryX.txtCounty = """ &
frm!txtCountyHolder.Value & """"
END IF

IF IsNull(frm!txtCategoryHolder) = False Then
strWhere = strWhere & " AND qryX.txtCat = """ &
frm!txtCategoryHolder.Value & """"
End If

'build the SQL statement
strSQL = "SELECT qryX.PropertyID, qryX.txtCnty, qryX.txtCat "
strSQL = strSQL & "FROM qryX "
'Strip of the leading "and" conjunction
strSQL = strSQL & Mid(StrWhere,5)

John Spencer
Access MVP 2002-2005, 2007-2008
The Hilltop Institute
University of Maryland Baltimore County
 
Top