Declaring variables
am 08.04.2008 21:50:14 von u31673
Hi,
Are the following lines of code the same, or is there a difference?
Dim strVariable1, strVariable2, strVariable3 as String
Dim strVariable1 as String
Dim strVariable2 as String
Dim strVariable3 as String
I was just wondering.
Thanks
Anthony
--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/Forums.aspx/databases-ms-ac cess/200804/1
Re: Declaring variables
am 08.04.2008 22:10:55 von fredg
On Tue, 08 Apr 2008 19:50:14 GMT, biganthony via AccessMonster.com
wrote:
> Hi,
>
> Are the following lines of code the same, or is there a difference?
>
> Dim strVariable1, strVariable2, strVariable3 as String
>
> Dim strVariable1 as String
> Dim strVariable2 as String
> Dim strVariable3 as String
>
> I was just wondering.
>
> Thanks
> Anthony
If you do not explicitly declare the type of a variable, VBA will
make it a Variant.
You should explicitly declare each variable as a string.
In your first example:
Dim strVariable1, strVariable2, strVariable3 as String
only strVariable3 is a String.
The others are Variants.
If you wish to declare variables as strings on one line you would use:
Dim strVariable1 as String, strVariable2 as String, strVariable3 as
String
--
Fred
Please respond only to this newsgroup.
I do not reply to personal e-mail
Re: Declaring variables
am 08.04.2008 22:27:14 von Salad
biganthony via AccessMonster.com wrote:
> Hi,
>
> Are the following lines of code the same, or is there a difference?
>
No.
You didn't provide a type for 1 & 2. The default is type Variant. So
you have 2 variants and one string.
Here's a test. Copy this code to a module. Then run. Date() was able
to be stuffed into each variable by type conversion. Not a real
problem. Now uncomment the lines for Passit and see if it runs.
Sub DimTest()
Dim strVar1, strVar2 As String, strVar3 As Date
strVar1 = Date
strVar2 = Date
strVar3 = Date
MsgBox strVar1 & " " & strVar2 & " " & strVar3
Debug.Print strVar1
Debug.Print strVar2
Debug.Print strVar3
'PassIt strVar1
'PassIt strVar2
'PassIt strVar3
End Sub
Sub PassIt(var As String)
MsgBox var
End Sub
Blindfold
http://www.youtube.com/watch?v=Iv5JXxME0js
> Dim strVariable1, strVariable2, strVariable3 as String
>
>
> Dim strVariable1 as String
> Dim strVariable2 as String
> Dim strVariable3 as String
>
>
> I was just wondering.
>
> Thanks
> Anthony
>
Re: Declaring variables
am 09.04.2008 04:59:54 von u31673
Thanks Salad and fred.
Andrew
Salad wrote:
>> Hi,
>>
>> Are the following lines of code the same, or is there a difference?
>
>No.
>
>You didn't provide a type for 1 & 2. The default is type Variant. So
>you have 2 variants and one string.
>
>Here's a test. Copy this code to a module. Then run. Date() was able
>to be stuffed into each variable by type conversion. Not a real
>problem. Now uncomment the lines for Passit and see if it runs.
>
>Sub DimTest()
> Dim strVar1, strVar2 As String, strVar3 As Date
> strVar1 = Date
> strVar2 = Date
> strVar3 = Date
> MsgBox strVar1 & " " & strVar2 & " " & strVar3
> Debug.Print strVar1
> Debug.Print strVar2
> Debug.Print strVar3
>
> 'PassIt strVar1
> 'PassIt strVar2
> 'PassIt strVar3
>
>End Sub
>Sub PassIt(var As String)
> MsgBox var
>End Sub
>
>Blindfold
>http://www.youtube.com/watch?v=Iv5JXxME0js
>
>> Dim strVariable1, strVariable2, strVariable3 as String
>>
>[quoted text clipped - 6 lines]
>> Thanks
>> Anthony
--
Message posted via AccessMonster.com
http://www.accessmonster.com/Uwe/Forums.aspx/databases-ms-ac cess/200804/1