Defineing variables

  • Thread starter Thread starter Mike
  • Start date Start date
M

Mike

I inherited a very complex database from another developer
who is no longer with my company. On a specific report,
he had it so that if a specific field does not contain any
data, it would not print the label, but if it did contain
data, it would print the label and the information it
contains. The Control Source for the field looks as
follows.

="Voice : " & [Voice]

There is some VBA attributed to the report with code that
looks like:

If IsNull(Voice.Value) Then
LblVoice.Visible = False
Else
LblVoice.Visible = True
End If

If IsNull(Fax.Value) Then
LblFax.Visible = False
Else
LblFax.Visible = True
End If

This is telling the report that if the value is null, do
not print the label "Voice", but if there is a value, to
print "Voice: 703-555-1234"

I am trying to do this for other fields (e.g. POCName) on
the report using the same process. I try to replicate the
code to my own use, to look like for example:

If IsNull(POCName.Value) Then
LblPOCName.Visible = False
Else
LblPOCName.Visible = True
End If

However, I get the error code "Compile error: Variable not
defined" and the "POCName" is highlighted. I am not very
proficient with VBA, but where do I define the variable?
Using the "Find" function, I hunted through the code of
the entire project for where the original developer may
have defined the variables for "Voice" or "Fax", but I can
not find them anywhere. I assume there is a a Dim
statement somewhere, but where?!

Any assistance would be appreciated.

m.
 
However, I get the error code "Compile error: Variable not
defined" and the "POCName" is highlighted. I am not very
proficient with VBA, but where do I define the variable?


You don't define the variable. The code is supposed to use the name of the
text box, and the name of the label that you *placed* on the form.

So, no, you don't have to define the variable, but you must use either an
existing variable, or the name of a control on the form. (in your case, you
need to use the same name as what the text box control is named on the
form).

You probably should not use the default name that ms-access gives (like
text22 etc). so, highlight the text box in design mode, and you can thus see
the name of the control in the properties tab.
 
are you sure the original developer used variables?
it looks like "Voice" is a field in the report's
RecordSource. it looks like "LblVoice" is the name of a
control on the report. my guess is the same goes for "Fax"
and "LblFax".
so, is there a field in the RecordSource called "POCName"?
and is there a control on the report called "LblPOCName"?
if not, try changing your code to reference the actual
field's name and the actual control's name that you're
trying to manipulate.
 
Mike said:
I inherited a very complex database from another developer
who is no longer with my company. On a specific report,
he had it so that if a specific field does not contain any
data, it would not print the label, but if it did contain
data, it would print the label and the information it
contains. The Control Source for the field looks as
follows.

="Voice : " & [Voice]

There is some VBA attributed to the report with code that
looks like:

If IsNull(Voice.Value) Then
LblVoice.Visible = False
Else
LblVoice.Visible = True
End If

If IsNull(Fax.Value) Then
LblFax.Visible = False
Else
LblFax.Visible = True
End If

This is telling the report that if the value is null, do
not print the label "Voice", but if there is a value, to
print "Voice: 703-555-1234"

I am trying to do this for other fields (e.g. POCName) on
the report using the same process. I try to replicate the
code to my own use, to look like for example:

If IsNull(POCName.Value) Then
LblPOCName.Visible = False
Else
LblPOCName.Visible = True
End If

However, I get the error code "Compile error: Variable not
defined" and the "POCName" is highlighted. I am not very
proficient with VBA, but where do I define the variable?
Using the "Find" function, I hunted through the code of
the entire project for where the original developer may
have defined the variables for "Voice" or "Fax", but I can
not find them anywhere. I assume there is a a Dim
statement somewhere, but where?!

No, no such Dim statement. The giveaway is that variables
do not have a Value property. Voice will be the name of the
text box control that is bound to the Voice field in the
report's record source query. If that's confusing, it's a
good reason to give the control a different name than the
field it's bound to (I would have named the text box
txtVoice). I would then write that code:

If IsNull(Me.txtVoice.Value) Then
. . .

Using Me removes the potential ambiguity with a variable of
the same name.

Let's take this logic a little further. Since the If
condition expression, IsNull(Me.txtVoice.Value), is either
True or False, and you want to set the Visible property to
either True or False, the code for Voice could be reduced to
one line:

Me.LblVoice.Visible = Not IsNull(Me.txtVoice.Value)

Another small point you should be aware of is that if you
make a text box invisible, its attached label will also
become invisible. So, if your labels are attached to the
text box, you wouldn't need to know/change the names of the
labels too.

Me.txtVoice.Visible = Not IsNull(Me.txtVoice.Value)

This might save you a line of code when your text box has a
border you don't want to see.
 
Back
Top