General VBA Synthax Question

  • Thread starter Thread starter QB
  • Start date Start date
Q

QB

I recently took over another developers db... and am confused with a couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for the $,
am I missing something?

Thank you,

QB
 
QB said:
I recently took over another developers db... and am confused with a couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric literal).
In a variable declaration, the type-declaration character can be used in
place of "As <type>"; for example, these two statements are equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first use of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so that all
variables must be declared.

All of this goes back to the days when Basic was a very limited language. I
think most people would agree that it is better practice to declare variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though, when you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String
He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work unless
there's either a control on the form ort a field in the form's recordsource
with that name.

It's generally considered bad practice to use special characters (like "$",
spaces, or other punctuation) in field and object names.
 
QB said:
I recently took over another developers db... and am confused with a couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric literal).
In a variable declaration, the type-declaration character can be used in
place of "As <type>"; for example, these two statements are equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first use of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so that all
variables must be declared.

All of this goes back to the days when Basic was a very limited language. I
think most people would agree that it is better practice to declare variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though, when you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String
He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work unless
there's either a control on the form ort a field in the form's recordsource
with that name.

It's generally considered bad practice to use special characters (like "$",
spaces, or other punctuation) in field and object names.
 
I recently took over another developers db... and am confused with a couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for the $,
am I missing something?

Thank you,

QB

These are optional datatype indicators. I% means that I is an Integer value;
txtTotalPrice$ means it's a string value. I prefer to explicitly Dim variables
with their datatype. The character codes are a holdover from old, old Basic
programming conventions. It took a bit of digging to find it, but in the VBA
help for Integer Datatype it says:

Integer variables are stored as 16-bit (2-byte) numbers ranging in value from
-32,768 to 32,767. The type-declaration character for Integer is the percent
sign (%).

The Help for "type-declaration" is pretty useless (it doesn't even enumerate
the type declaration characters, you need to go to each datatype in turn).
 
I recently took over another developers db... and am confused with a couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for the $,
am I missing something?

Thank you,

QB

These are optional datatype indicators. I% means that I is an Integer value;
txtTotalPrice$ means it's a string value. I prefer to explicitly Dim variables
with their datatype. The character codes are a holdover from old, old Basic
programming conventions. It took a bit of digging to find it, but in the VBA
help for Integer Datatype it says:

Integer variables are stored as 16-bit (2-byte) numbers ranging in value from
-32,768 to 32,767. The type-declaration character for Integer is the percent
sign (%).

The Help for "type-declaration" is pretty useless (it doesn't even enumerate
the type declaration characters, you need to go to each datatype in turn).
 
I assume this is also the case on particular VBA functions I have seen with a
suffixed type indicator?

ex:

var = Left$(....)

explicitly specifying that the Left() function should be returning a String?

If this is the case... under what circumstances would VBA.Left() NOT return
a string datatype? I see this fairly often (generally on Left, Right or Mid
functions, but my mind might be making this pattern up for me...)

I've always wondered about this, but trying to google What Does "$" mean in
VBA? doesn't work all that well.

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



Dirk Goldgar said:
QB said:
I recently took over another developers db... and am confused with a couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric literal).
In a variable declaration, the type-declaration character can be used in
place of "As <type>"; for example, these two statements are equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first use of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so that all
variables must be declared.

All of this goes back to the days when Basic was a very limited language. I
think most people would agree that it is better practice to declare variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though, when you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String
He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work unless
there's either a control on the form ort a field in the form's recordsource
with that name.

It's generally considered bad practice to use special characters (like "$",
spaces, or other punctuation) in field and object names.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
I assume this is also the case on particular VBA functions I have seen with a
suffixed type indicator?

ex:

var = Left$(....)

explicitly specifying that the Left() function should be returning a String?

If this is the case... under what circumstances would VBA.Left() NOT return
a string datatype? I see this fairly often (generally on Left, Right or Mid
functions, but my mind might be making this pattern up for me...)

I've always wondered about this, but trying to google What Does "$" mean in
VBA? doesn't work all that well.

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



Dirk Goldgar said:
QB said:
I recently took over another developers db... and am confused with a couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric literal).
In a variable declaration, the type-declaration character can be used in
place of "As <type>"; for example, these two statements are equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first use of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so that all
variables must be declared.

All of this goes back to the days when Basic was a very limited language. I
think most people would agree that it is better practice to declare variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though, when you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String
He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work unless
there's either a control on the form ort a field in the form's recordsource
with that name.

It's generally considered bad practice to use special characters (like "$",
spaces, or other punctuation) in field and object names.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
If the argument is a Variant it can hold a NULL value and thus, the result
will be NULL (not a string, not a string made of 0 character):


? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Since database can supply null as data (either the date is 'nullable',
either from an outer join), a VARIANT is generally better since non-variant
value-data-type cannot hold a null.



Vanderghast, Access MVP

Jack Leach said:
I assume this is also the case on particular VBA functions I have seen with
a
suffixed type indicator?

ex:

var = Left$(....)

explicitly specifying that the Left() function should be returning a
String?

If this is the case... under what circumstances would VBA.Left() NOT
return
a string datatype? I see this fairly often (generally on Left, Right or
Mid
functions, but my mind might be making this pattern up for me...)

I've always wondered about this, but trying to google What Does "$" mean
in
VBA? doesn't work all that well.

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



Dirk Goldgar said:
QB said:
I recently took over another developers db... and am confused with a
couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the
Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric literal).
In a variable declaration, the type-declaration character can be used in
place of "As <type>"; for example, these two statements are equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first use
of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so that
all
variables must be declared.

All of this goes back to the days when Basic was a very limited language.
I
think most people would agree that it is better practice to declare
variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though, when
you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String
He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for
the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource
named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work
unless
there's either a control on the form ort a field in the form's
recordsource
with that name.

It's generally considered bad practice to use special characters (like
"$",
spaces, or other punctuation) in field and object names.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
If the argument is a Variant it can hold a NULL value and thus, the result
will be NULL (not a string, not a string made of 0 character):


? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Since database can supply null as data (either the date is 'nullable',
either from an outer join), a VARIANT is generally better since non-variant
value-data-type cannot hold a null.



Vanderghast, Access MVP

Jack Leach said:
I assume this is also the case on particular VBA functions I have seen with
a
suffixed type indicator?

ex:

var = Left$(....)

explicitly specifying that the Left() function should be returning a
String?

If this is the case... under what circumstances would VBA.Left() NOT
return
a string datatype? I see this fairly often (generally on Left, Right or
Mid
functions, but my mind might be making this pattern up for me...)

I've always wondered about this, but trying to google What Does "$" mean
in
VBA? doesn't work all that well.

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



Dirk Goldgar said:
QB said:
I recently took over another developers db... and am confused with a
couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the
Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric literal).
In a variable declaration, the type-declaration character can be used in
place of "As <type>"; for example, these two statements are equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first use
of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so that
all
variables must be declared.

All of this goes back to the days when Basic was a very limited language.
I
think most people would agree that it is better practice to declare
variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though, when
you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String
He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for
the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource
named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work
unless
there's either a control on the form ort a field in the form's
recordsource
with that name.

It's generally considered bad practice to use special characters (like
"$",
spaces, or other punctuation) in field and object names.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Ahhh... so using the typedec suffix will essentially ensure that no other
datatype can be entered as an argument. I hadn't considered nulls.

Thanks for the clarification.
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



vanderghast said:
If the argument is a Variant it can hold a NULL value and thus, the result
will be NULL (not a string, not a string made of 0 character):


? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Since database can supply null as data (either the date is 'nullable',
either from an outer join), a VARIANT is generally better since non-variant
value-data-type cannot hold a null.



Vanderghast, Access MVP

Jack Leach said:
I assume this is also the case on particular VBA functions I have seen with
a
suffixed type indicator?

ex:

var = Left$(....)

explicitly specifying that the Left() function should be returning a
String?

If this is the case... under what circumstances would VBA.Left() NOT
return
a string datatype? I see this fairly often (generally on Left, Right or
Mid
functions, but my mind might be making this pattern up for me...)

I've always wondered about this, but trying to google What Does "$" mean
in
VBA? doesn't work all that well.

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



Dirk Goldgar said:
I recently took over another developers db... and am confused with a
couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the
Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric literal).
In a variable declaration, the type-declaration character can be used in
place of "As <type>"; for example, these two statements are equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first use
of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so that
all
variables must be declared.

All of this goes back to the days when Basic was a very limited language.
I
think most people would agree that it is better practice to declare
variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though, when
you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String

He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for
the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource
named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work
unless
there's either a control on the form ort a field in the form's
recordsource
with that name.

It's generally considered bad practice to use special characters (like
"$",
spaces, or other punctuation) in field and object names.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Ahhh... so using the typedec suffix will essentially ensure that no other
datatype can be entered as an argument. I hadn't considered nulls.

Thanks for the clarification.
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



vanderghast said:
If the argument is a Variant it can hold a NULL value and thus, the result
will be NULL (not a string, not a string made of 0 character):


? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Since database can supply null as data (either the date is 'nullable',
either from an outer join), a VARIANT is generally better since non-variant
value-data-type cannot hold a null.



Vanderghast, Access MVP

Jack Leach said:
I assume this is also the case on particular VBA functions I have seen with
a
suffixed type indicator?

ex:

var = Left$(....)

explicitly specifying that the Left() function should be returning a
String?

If this is the case... under what circumstances would VBA.Left() NOT
return
a string datatype? I see this fairly often (generally on Left, Right or
Mid
functions, but my mind might be making this pattern up for me...)

I've always wondered about this, but trying to google What Does "$" mean
in
VBA? doesn't work all that well.

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



Dirk Goldgar said:
I recently took over another developers db... and am confused with a
couple
synthax usages and am hoping someone could explain what he was doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the
Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric literal).
In a variable declaration, the type-declaration character can be used in
place of "As <type>"; for example, these two statements are equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first use
of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so that
all
variables must be declared.

All of this goes back to the days when Basic was a very limited language.
I
think most people would agree that it is better practice to declare
variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though, when
you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String

He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the specified
form, but can find one named 'txtTotalPrice'? Is there a reason for
the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource
named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work
unless
there's either a control on the form ort a field in the form's
recordsource
with that name.

It's generally considered bad practice to use special characters (like
"$",
spaces, or other punctuation) in field and object names.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
... with a minor precision: it is really about the RESULT, not the
argument(s). Left$ returns a string, and so, cannot return NULL, while Left
return a variant (string sub type ) which can return a string and that
result can be automatically cast to a string, as long as it is not a NULL:


Dim x AS string
x= Left("hello", 2) ' automatic cast performed
x= Left$("hello", 2) ' no cast required


Not exactly on the main point, but somehow related, note that


Dim x AS string
x= Left(null, 2)


produces a run time error, since the automatic cast failed, not the function
call itself, as it would with:



Dim x AS string
x=null



or, with more subtle:

Function Soso( y As string) As long
...
End Function

and from somewhere:

Dim u As long
u=Soso( Null )



will fail, since the argument, declared as a string in the function
definition, cannot hold the null whom the call to that function would have
to transfer that value.





Vanderghast, Access MVP



Jack Leach said:
? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Ahhh... so using the typedec suffix will essentially ensure that no other
datatype can be entered as an argument. I hadn't considered nulls.

Thanks for the clarification.
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



vanderghast said:
If the argument is a Variant it can hold a NULL value and thus, the
result
will be NULL (not a string, not a string made of 0 character):


? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Since database can supply null as data (either the date is 'nullable',
either from an outer join), a VARIANT is generally better since
non-variant
value-data-type cannot hold a null.



Vanderghast, Access MVP

Jack Leach said:
I assume this is also the case on particular VBA functions I have seen
with
a
suffixed type indicator?

ex:

var = Left$(....)

explicitly specifying that the Left() function should be returning a
String?

If this is the case... under what circumstances would VBA.Left() NOT
return
a string datatype? I see this fairly often (generally on Left, Right
or
Mid
functions, but my mind might be making this pattern up for me...)

I've always wondered about this, but trying to google What Does "$"
mean
in
VBA? doesn't work all that well.

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



:

I recently took over another developers db... and am confused with a
couple
synthax usages and am hoping someone could explain what he was
doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the
Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric
literal).
In a variable declaration, the type-declaration character can be used
in
place of "As <type>"; for example, these two statements are
equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first
use
of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so
that
all
variables must be declared.

All of this goes back to the days when Basic was a very limited
language.
I
think most people would agree that it is better practice to declare
variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though,
when
you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be
others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String

He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the
specified
form, but can find one named 'txtTotalPrice'? Is there a reason for
the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource
named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work
unless
there's either a control on the form ort a field in the form's
recordsource
with that name.

It's generally considered bad practice to use special characters (like
"$",
spaces, or other punctuation) in field and object names.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
... with a minor precision: it is really about the RESULT, not the
argument(s). Left$ returns a string, and so, cannot return NULL, while Left
return a variant (string sub type ) which can return a string and that
result can be automatically cast to a string, as long as it is not a NULL:


Dim x AS string
x= Left("hello", 2) ' automatic cast performed
x= Left$("hello", 2) ' no cast required


Not exactly on the main point, but somehow related, note that


Dim x AS string
x= Left(null, 2)


produces a run time error, since the automatic cast failed, not the function
call itself, as it would with:



Dim x AS string
x=null



or, with more subtle:

Function Soso( y As string) As long
...
End Function

and from somewhere:

Dim u As long
u=Soso( Null )



will fail, since the argument, declared as a string in the function
definition, cannot hold the null whom the call to that function would have
to transfer that value.





Vanderghast, Access MVP



Jack Leach said:
? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Ahhh... so using the typedec suffix will essentially ensure that no other
datatype can be entered as an argument. I hadn't considered nulls.

Thanks for the clarification.
--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



vanderghast said:
If the argument is a Variant it can hold a NULL value and thus, the
result
will be NULL (not a string, not a string made of 0 character):


? Left(null, 4)
null

? Left$(null, 4)
-- run time error


Since database can supply null as data (either the date is 'nullable',
either from an outer join), a VARIANT is generally better since
non-variant
value-data-type cannot hold a null.



Vanderghast, Access MVP

Jack Leach said:
I assume this is also the case on particular VBA functions I have seen
with
a
suffixed type indicator?

ex:

var = Left$(....)

explicitly specifying that the Left() function should be returning a
String?

If this is the case... under what circumstances would VBA.Left() NOT
return
a string datatype? I see this fairly often (generally on Left, Right
or
Mid
functions, but my mind might be making this pattern up for me...)

I've always wondered about this, but trying to google What Does "$"
mean
in
VBA? doesn't work all that well.

--
Jack Leach
www.tristatemachine.com

"I haven't failed, I've found ten thousand ways that don't work."
-Thomas Edison (1847-1931)



:

I recently took over another developers db... and am confused with a
couple
synthax usages and am hoping someone could explain what he was
doing.

he created loops similar to:

For i% = 0 To Forms.Count-1
...
Next i%

Why the %? Does it serve a purpose?

The "%" is a type-declaration character indicating that i is of the
Integer
data type. Various data types can be indicated by a type-declaration
characters tacked onto the end of a variable name (or a numeric
literal).
In a variable declaration, the type-declaration character can be used
in
place of "As <type>"; for example, these two statements are
equivalent:

Dim i As Integer
Dim i%

If you don't have Option Explicit set on in a module, then the first
use
of
a variable will define it, and so the data type may be specified by a
type-declaration character at that point:

For i% = 1 To 100

I strongly recommend that you *do* use Option Explicit, though, so
that
all
variables must be declared.

All of this goes back to the days when Basic was a very limited
language.
I
think most people would agree that it is better practice to declare
variable
types explicitly, as in

Dim I As Integer

The type-declaration characters do come in handy sometimes, though,
when
you
want to use a numeric literal and avoid any type conversion; e.g.,

Dim curAmount As Currency

curAmount = 100@ ' 100 dollars to start

Unfortunately, I can't remember off the top of my head what all the
type-delcaration characters are. I know these, but there may be
others:

% = Integer
& = Long
! = Single
# = Double
@ = Currency
$ = String

He also referred to certain variables with a $? For instance
Me.txtVal=DetailForm![txtTotalPrice$]

I am unable to locate a control named 'txtTotalPrice$' on the
specified
form, but can find one named 'txtTotalPrice'? Is there a reason for
the
$,
am I missing something?

Now that is odd. Is there perhaps a field in the form's recordsource
named
"txtTotalPrice$"? I wouldn't expect the reference you posted to work
unless
there's either a control on the form ort a field in the form's
recordsource
with that name.

It's generally considered bad practice to use special characters (like
"$",
spaces, or other punctuation) in field and object names.

--
Dirk Goldgar, MS Access MVP
www.datagnostics.com

(please reply to the newsgroup)
 
Back
Top