devxlogo

Using Dim Statement on Variant Arrays

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.

See also  5 Tips to Manage a Remote Workforce
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist