Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Enterprise
Expertise: Intermediate
May 6, 1998

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 Sub


Sub GetAuthors(byref vArray as variant)
Dim con As New ADODB.Connection
Dim 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.Close
End 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.
Jerry Brunning
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date