Forum Discussion

vikassardesai's avatar
vikassardesai
Occasional Contributor
5 years ago

Store CSV column data in an array VBScript

Is there any way to store the csv column data in an array? A sample code in vbscript will be helpful.

 

2 Replies

  • Hi vikassardesai ,

     

    please reffer the below code and modify as required.

    Function GetContentFromDB (strFileName, strSQLStatement)
    Dim objAdCon, objAdRs
    Set objAdCon = CreateObject(“ADODB.Connection”)
    objAdCon.Open “DRIVER={Microsoft Excel Driver (*.xls)};DBQ=”&strFileName & “;Readonly=True”
    If Err <> 0 Then
          Log.Error(“Error has occured while Creating Connection: ”)
          Set obj_UDF_getRecordset = Nothing
       Exit Function
    End If
    
    Set objAdRs = CreateObject(“ADODB.Recordset”)
    objAdRs.CursorLocation=3                        
    objAdRs.Open strSQLStatement, objAdCon, 1, 3
    While objAdRs.EOF=false	
       For i=0 to objAdRs.Fields.count
         Log.Message(objAdRs.fields(i))
    Next
    objAdRs.moveNext
    Wend
    If Err<>0 Then
          Log.Error(“Error occured”)
          Set obj_UDF_getRecordset = Nothing
         Exit Function
    End If
    Set objAdRs.ActiveConnection = Nothing
    objAdCon.Close
    Set objAdCon = Nothing
    End Function

    above function will take two parameters (filename with the path and query)

    below is the sample call method

    Set rsSampleRS= GetContentsFromDB(“C:\Documents and Settings\mohank\Desktop\Login.csv”, “Select [UserName] from [sheet1]”)

    please let me know if its working or not

     

    Kind Regards,

    Sathish Kumar K