Results 1 to 5 of 5
  1. #1
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352

    Question Defining Variables, Which Variable Type and When?

    A general question here:
    When using Dim, how do I determine which is the best variable type to use?
    Boolean, date, currency are obvious & clear; but range, integer, string, object, long, &c. I get confused with.


    Additionally, what is the differences between Dim and Set? Some people I know use Set without declaring the variable first..and some not.

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    I normally use variant for everything. (no typename = variant) Dim vDate, vName, ...etc.
    BUT if I need specific things like currency then I set those.
    and some function REQUIRE a specific type , then I must set those.
    NOTE : sometimes comparing number you want the same type where the value is 1.3 and my variant is 1.3 , then for some reason, access can say THEY ARE NOT EQUAL.
    But for names, dates, i = i + 1 counters...i use variant.

    But setting everything variant doesnt hurt anything, doesnt slow it down.

    DIM VS SET:
    DIM is for numeric ,string values,

    SET is for OBJECTS.
    SET colNames as COLLECTION
    or SET rst = db.RECORDSET.

  3. #3
    nick404's Avatar
    nick404 is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    May 2015
    Location
    Wisconsin
    Posts
    352
    Is it necessary to define whatever you SET? I understand it is good practice, is there cases where it will not let you set something you have not defined? (outside of using Option Explicit).

  4. #4
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    I do, and use OPTION EXPLICIT.
    This prevents spelling errors and unknown variables.

  5. #5
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    When using Dim, how do I determine which is the best variable type to use?
    Dim is short for Dimension and is used in a Dim Statement for declaring variables and reserving space in the system's memory. It is a good idea to match your variable types with the data type you will be assigning your variable.
    https://msdn.microsoft.com/EN-US/lib.../jj692781.aspx

    It is a bad idea to dimension all of your variables as type Variant. First, you want to be able to read your code and understand what is going on. Also, you do not want to bloat your app with expensive 16 byte Variants when you can use a 2 byte Integer or a 4 byte Long. When you assign a string literal to a Variant type you are using over 22 bytes of memory. Then, there is the fact that you do not want to be casting types from one type to another. Access manages this very well and I allow Access to Cast types. But, you should be explicit with your variable types and not down cast your Variants into fields. I try to reserve the use of the Variant type for looping through arrays.

    Additionally, what is the differences between Dim and Set? Some people I know use Set without declaring the variable first..and some not.
    Dim is for a Dim Statement. The Set keyword is used for the instantiation of Objects. It assigns a reference to your Object variable. It can also be used for assignment of references to a property of a Class.

    Some programming languages will allow and almost encourage implicit declarations. For instance, not using a Dim statement and simply using
    strMyString = "Foo"

    ... is an implicit declaration vs. the following which is an explicit declaration.
    Dim strMyString as string
    strMyString = "Foo"

    So, this is allowed in VBA as well as other languages. IIRC. this will result in strMyString becoming a Variant type. Other programming languages may actually do a better job and match it up with type string. So, be explicit, I say.

    So you would not compare Set to Dim. Set is where you assign the reference to your variable. And you could do this Implicitly or Explicitly.
    Dim myObject as object
    set myObject = CreateObject(...)

    A fair comparison to the Set keyword would be the New Keyword. Use early binding in an example like the following and there is no need to use the Set keyword to instantiate your object in a separate line of code.
    Dim objOutlook As New Outlook.Application

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

Similar Threads

  1. Normalize Series of Variables into One Variable
    By cam2era in forum Programming
    Replies: 6
    Last Post: 06-22-2015, 12:10 PM
  2. Replies: 8
    Last Post: 10-08-2014, 06:08 PM
  3. Trouble defining variables - Access Module
    By David92595 in forum Modules
    Replies: 1
    Last Post: 11-29-2012, 07:09 PM
  4. data type variable constants
    By markjkubicki in forum Programming
    Replies: 2
    Last Post: 06-20-2012, 09:57 PM
  5. variable uses automation type not supported
    By eerkut in forum Programming
    Replies: 4
    Last Post: 02-15-2012, 03:21 PM

Tags for this Thread

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