Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5953957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:54:03+00:00 2026-05-22T17:54:03+00:00

When is a Dim statement processed in the compiling of VBA code? Is there

  • 0

When is a Dim statement processed in the compiling of VBA code? Is there any efficiency gain if I do this?:

Dim oFileDiag As FileDialog

Set oFileDiag = Application.FileDialog(msoFileDialogFilePicker)                
If oFileDiag.Show = -1 Then

    '// Dim statement further down in the code...
    Dim ofdSelected As FileDialogSelectedItems
    Set ofdSelected = .SelectedItems

End If

As opposed to this?:

'// Dim statement at the beginning of the code...
Dim oFileDiag As FileDialog
Dim ofdSelected As FileDialogSelectedItems

Set oFileDiag = Application.FileDialog(msoFileDialogFilePicker)                
If oFileDiag.Show = -1 Then

    Set ofdSelected = .SelectedItems

End If
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:54:04+00:00Added an answer on May 22, 2026 at 5:54 pm

    As I understand it (I’ve been wrong before), there is no gain in efficiency in VB(A). Declaring any variable anywhere in the routine will use the same resources (though your variable won’t be available until below its declaration).

    The declaration of an object variable (Dim) only creates the reference, it does not instantiate the object until the Set. However, watch out for the opposite problem in this sort of construct:

    Dim rsFoo As New ADODB.Recordset
    
    'other statements
    
    With rsFoo
        .LockType = adBatchOptimistic
        'other statements
    End With
    

    This seems like it might be convenient, since you don’t have to do an explicit Set... = New.... But the gotcha is that every time you use rsFoo at runtime the code has to check whether or not it’s been instantiated. Much better this way:

    Dim rsFoo As ADODB.Recordset
    
    'other statements
    
    Set rsFoo = New ADODB.Recordset
    With rsFoo
        .LockType = adBatchOptimistic
        'other statements
    End With
    

    This is, to some extent, a “religious” difference, at least in languages where there isn’t any real difference in overhead. The most common practice in VB(A) is to declare all variables at the beginning of the routine, though there are some who argue that keeping the declaration as close as possible to the first use is “clearer” (not to those of us who are conditioned to expect them all at the beginning, it’s not…).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The code below returns error after the return statement Private Sub Command1_Click() Dim str
I've got some problems. I've got this code: Dim infoID As Integer = objCommand1.ExecuteScalar()
I have this LINQ statement Dim Demo = From d In DBDataTable.AsEnumerable _ Select
Is there any way to access the prepared statement as sent to the SQL
I've got an IF statement that validates data. Basically looks like this: Dim s
I have some code like this Dim conn As SqlConnection = New SqlConnection(server='h'; user
I've broken the code down to the smallest possible statement: Dim cn As System.Data.SQLite.SQLiteConnection
I have this code Dim str As String Dim myConn As SqlConnection = New
I have this code: Dim pathString As String = HttpContext.Current.Request.MapPath(Banking.mdb) Dim odbconBanking As New
The 'reader' within the if statement is showing Expression is not a method Dim

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.