another wierd one. declaring strings in module

bhlgroup

New member
Local time
Today, 08:47
Joined
Dec 7, 2011
Messages
5
when I am trying to declare some strings in my code it would appear the access doesnt seem to like my last declared string. I have to add a unused string at the end before it will move on. This only seems to happen if I set the last string to nothing.

heres a basic module for you to replicate:

Public Function test()
Dim test1, test2 As String
Set test1 = Nothing
Set test2 = Nothing
End Function

if you try step through this is will compain about the string "test2". Error message: Compile error. object required.

now if you change the code to:

Public Function test()
Dim test1, test2, test3 As String
Set test1 = Nothing
Set test2 = Nothing
End Function

it will then continue to work (as long as you never then try use test3)

anyone any ideas why this is doing this? I am running Access 2010 x64(office 2010 pro) or I am going about clearing the strings the wrong way.

Regards,

Michael
 
Are you just trying to clear the variable? I don't think you can set a string variable to Nothing (only objects can be set to Nothing IIRC).

I usually do something like test1 = "", but that just makes it a zero-length string I believe.
 
yes I'm making sure they are cleared. If I declare the extra unused string the code goes ahead and sets the strings to nothing. so it seems odd that it only fails if the last declared string is set to nothing.
 
Strange behavior indeed, not sure why it allows you to set a string to Nothing if you have an unused string variable.
 
Nothing strange about it since your variable test1 is declared as Variant and not as a string.

In VBA by omitting a type declaration a variable will be typecast as the default Variant type and variant can handle nulls etc.

JR
 
when I am trying to declare some strings in my code it would appear the access doesnt seem to like my last declared string. I have to add a unused string at the end before it will move on. This only seems to happen if I set the last string to nothing.

heres a basic module for you to replicate:

Public Function test()
Dim test1, test2 As String
Set test1 = Nothing
Set test2 = Nothing
End Function

if you try step through this is will compain about the string "test2". Error message: Compile error. object required.

now if you change the code to:

Public Function test()
Dim test1, test2, test3 As String
Set test1 = Nothing
Set test2 = Nothing
End Function

it will then continue to work (as long as you never then try use test3)

anyone any ideas why this is doing this? I am running Access 2010 x64(office 2010 pro) or I am going about clearing the strings the wrong way.

Regards,

Michael

Stings cannot be set to nothing, only objects/variants.

Dim test1, test2 as string results in test1 being a variant, and test2 being a string.

To get two string you would need to do,

Dim test1 as string, test2 as string.

Then both would fail trying to set them to nothing, try setting them to Empty instead.
 
ah ok, that would explain. I always thought that the whole line was declared as strings not just the one preceding the AS string.

thanks.
 
ah ok, that would explain. I always thought that the whole line was declared as strings not just the one preceding the AS string.

thanks.
That's a good way to learn the difference. :)

Dim test1, test2 As String

is equivalent to

Dim test1 As Variant, test2 As String

and Variants can "morph" into anything. ;)
 

Users who are viewing this thread

Back
Top Bottom