3

I am working in a module in access trying to initialize variables. For some reason the declaration:

Dim ModName As String = "modWindowsFileSystem" 

creates a compiling error at the '=' with the error message "Expected:end of statement". I have looked up the format on mutiple websites that all agree with my syntax here is a trustworthy one: http://msdn.microsoft.com/en-us/library/7ee5a7s1.aspx Another website suggested I declare the variable within a function such as

Sub AssignValueString()
   Dim Modname As String
   Modname = "modWindowsFileSystem"
End Sub

but that seems unnecessary. One issue that may be considered is I have saved the module and named it modWindowsFileSystem. I am not sure if this will conflict somehow with initializing that variable.

1
  • 2
    Unfortunately, VBA and VB.NET are very similar but not entirely the same. That syntax is correct for VB, but not VBA. Commented Jun 4, 2014 at 13:46

1 Answer 1

4

VBA is a little different from VB. The msdn link you posted is actually for VB, which allows for inline declarations and assignment. VBA won't allow this, unfortunately.

So, you can either split them, like your example:

Dim Modname As String
Modname = "modWindowsFileSystem"

Or, you can place the continuation character : to achieve this on one line.

Dim Modname As String : Modname = "modWindowsFileSystem"

As far as the assignment for a module's variables, you can declare global variables outside of a function, but you would need to set their value within a function, like an init() function. Constants (labelled with Const) can be declared and assigned outside of a function.

Sign up to request clarification or add additional context in comments.

3 Comments

Do you know if I have to initialize the variable within a subfunction?
For modules, you can declare global variables outside of a function, but you would need to set their value within a function, like an init() function. Constants (labelled with Const) can be declared and assigned outside of a function.
@armstrhb +1 your last comment should really be part of your answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.