Using Dim Statement on Variant Arrays

When passing a variant array by reference, make sure to use Dim to declare the statement before using Redim.

Example:

 Private Sub Command1_Click()Dim x as integer        ReDim v(2) As Variant    v(0) = "RIN"    v(1) = "A"		    	    Call GetAuthors(v)        Debug.print v(0,0)End SubSub GetAuthors(byref vArray as variant)Dim con As New ADODB.ConnectionDim rs As New ADODB.Recordset    con.ConnectionString = "Driver={SQL Server};Database=Pubs;Server=(local);UID=sa"    con.Open        rs.Source = "select * from AUTHORS where au_lname like '" & vArray(0) & "%'" and " & _ au_fname like '" & vArray(1) & "%'"    Set rs.ActiveConnection = con    rs.Open    vArray = rs.GetRows			'An Error occurs here    rs.Close    con.CloseEnd Sub

Executing this code raises “Run-time error #458 Variable uses an automation type not supported in Visual Basic.” Note that this error is particularly difficult to debug because the compiler will not generate an exception for using Redim without a Dim, event if “Option Explicit” is specified. Declaring the variant with a Dim statement before the Redim will prevent this error from occurring.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

The Latest

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a

chrome os developer mode

How to Turn on Chrome OS Developer Mode

Google’s Chrome OS is a popular operating system that is widely used on Chromebooks and other devices. While it is designed to be simple and user-friendly, there are times when users may want to access additional features and functionality. One way to do this is by turning on Chrome OS