Results 1 to 8 of 8
  1. #1
    msaccessdev is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    13

    Unhappy Variable Declaration in MS Access 2013 VBA

    Hi There,

    Hope all doing good.



    what do we mean by declaring few variables like below in the VBA window.

    1) I am confused why do we declare variables with $ or & symbols ? does it really have specific property or meaning ?
    Option Compare Database


    Private ptoC$, tempRS As Recordset, dbTest$, allNEW As Boolean



    2) How can we leave the second declaration with out telling it "Integer" or "String" etc... and MS Access accepts it very well and executing fine ?
    Private empTeam&, exeRack&, eType$, msgType$


    please guide me.

    Thanks,
    MS access

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,550
    Stop using antiquated variable declarations with $ or &. (from the 70s)

    Spell it out...
    dbTest as string
    ptoC as currency

  3. #3
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Unlike other programming languages you need to explicitly declare your variables in VBA, each variable. So including several variables in a single line will not carry the first data type to the subsequent variables.

    Dim sMyString as String, sOtherString, sLastString

    will not work

    sOtherString and sLastString will default to type Variant unless you explicitly declare the variable

    Dim sMyString as String, sOtherString as String, sLastString as String

    or
    Dim sMyString as String
    Dim sOtherString as String
    Dim sLastString as String

  4. #4
    msaccessdev is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    13
    Hi Thanks for your suggestion,

    but, my questions was

    1) unlike the normal declaration of the variables, declaring them with extra characters like & or $ makes any difference in the code ?

    ItsMe:
    2) since you said declaring like the "Dim sMyString as String, sOtherString, sLastString" wont work. but, i dont have any problem in the code and application is running fine without throwing any errors. my previous team member declared the variables like that before i join around 3 months back in MS Access 2013 and i am trying to edit the code and seen these declarations.

    thanks

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    Not explicitly specifying type will work - the variable will default to Variant. Variant allows the variable to hold any kind of data. Variants use more memory.

    I have seen the $ used but never used myself. As ranman stated this just might be old but still valid syntax.

    Just as the + character will work to concatenate text because it is carry over from old BASIC but & is preferred concatenation operator in Access and VBA.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Quote Originally Posted by msaccessdev View Post
    ...
    ItsMe:
    2) since you said declaring like the "Dim sMyString as String, sOtherString, sLastString" wont work. but, i dont have any problem in the code and application is running fine without throwing any errors. my previous team member declared the variables like that before i join around 3 months back in MS Access 2013 and i am trying to edit the code and seen these declarations.
    It will default to Variant type, which will not truncate most of the other types. So it may work but may not work the way you intended when writing the declaration.

    As far as using the special characters, I see it on occasion and it seems to still work. I know I have a couple modules that still have it, where I did not bother to change it. If your IDE compiles it and you, along with others, understand it, I suppose there is not any harm in it. I know I do a double take when I see it and ask myself what the heck.

  7. #7
    msaccessdev is offline Novice
    Windows 8 Access 2013
    Join Date
    Jun 2014
    Posts
    13
    Great, thank you all for your insights and suggestions.

    its really helpful.

    thank you.

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    In olden days, before mice and WIFI and Windows, the Variables in BASIC could be declared by simply adding a Special Character to the end of the name; that was all you needed to do to declare a Variable. The $ at the end of

    MyVariable$

    told BASIC that MyVariable was a String Variable.

    You'll see it in really old, legacy code (I've got a bunch of that, but it's on floppy discs, and I have no floppy discs drives) but it won't work in Access, without being Dim'd.

    It is generally considered very bad practice to use any Special Characters, with the exception of the Underline, in the name of anything in Access VBA. You might get away with for a while, maybe even a long while, but sooner or later it'll jump up and bite you where you won't like to be bitten! And tracking down the problem, when it occurs, can be a real bear!

    Linq ;0)>

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 04-07-2014, 10:01 AM
  2. Outlook 2013 + Access 2013 + HTML
    By Yann63 in forum Programming
    Replies: 2
    Last Post: 11-26-2013, 02:39 PM
  3. Access 2013 Web App with Sharepoint 2013
    By miguel.escobar in forum Access
    Replies: 7
    Last Post: 06-17-2013, 09:03 AM
  4. Replies: 0
    Last Post: 08-10-2011, 11:59 AM
  5. Replies: 2
    Last Post: 03-18-2010, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums