title: A generic VBscript function to connect to any database and execute any SQL query
body: <p>the function should just take connection string and a SQL query as input and it should connect to any database(SQL, ORACLE, SYBASE, MS ACCESS) and execute any query which i have passed as the parameters to the function.</p>
  
  <p>I have written the below function for that task, Can you please check this once and tell me is this correct or pls tell me if i am wrong anywhere.</p>
  
  #
  
  <pre><code>Public Function ConnectDB (strCon, strQuery)
  Set objConnection = CreateObject("ADODB.Connection")
  Set objRecordSet = CreateObject("ADODB.Recordset")
  objConnection.Open strCon
  
  objRecordSet.Open strQuery,objConnection 
  objRecordSet.MoveFirst
  Do Until objRecordset.EOF
  Msgbox "Number of records: " &amp; objRecordset.RecordCount 
  Msgbox objRecordset(0)
  Msgbox objRecordset(1)
      objRecordset.MoveNext
  Loop
  objRecordSet.Close
  objConnection.Close
  Set objConnection = Nothing
  Set objRecordSet = Nothing
  End Function
  </code></pre>
  
  #
  
  <pre><code>Call ConnectDB ("Provider = Microsoft.Jet.OLEDB.4.0; " &amp; _ 
              "Data Source = inventory.mdb","SELECT * FROM EMP ORDER BY EMPName")
  </code></pre>
  
  <p><strong>UPDATE:</strong><br>
  Thank you so much for the replies. </p>
  
  <p>Actually i have been asked to write a function which performs the task of connecting to any database and executing any query(given by user) in that connected database. </p>
  
  <p>I have started to learn VBScript and want to have indepth knowledge of writing functions. Ekkehard horner can you please tell me where can i read to get know all about routines(functions and sub procedure). Presently, i have only the basic idea on routines and i referred MSDN, where they have given only basic information. Please help me where to study more about routines. It is so difficult to write programs without knowing about them correctly.</p>
  
  <p>Hi Sanpaco, below is the class i wrote. Please check it once and let me know the corrections.
  I am very new to VBScript.Suggest me ways to improve my programming knowledge.</p>
  
  <pre><code>Class DBCommunicator
  Public Function DBConnect(StrCon)
  Option Eplicit
  Dim oConn
  set oConn = CreateObject("ADODB.Connection")
  oConn.Open Strcon
  Function DBConnect = True
  End Function
  
  Public Function QueryDB(StrQuery)
  Option Eplicit
  Dim oRst, oField
  set oRst = CreateObject("ADODB.recordset")
  oRst.Open "StrQuery", oConn
  Do Until oRst.EOF   
  For each oField in oRst.Fields        
  Print oField.Name &amp; " = " &amp; oField.Value    
  Next
  oRst.MoveNext
  loop
  
  Public Function DBdisConnect
  oRst.close
  oConn.close
  End Function
  End Class
  
  ########################################
  
  Option Explicit
  Dim strResult
  strResult=DBCommunicator.DBConnect("&lt;&lt;Connection String of any database User want to connect&gt;&gt;")
      If strResult&lt;&gt;True Then
          wscript.echo "DB Connection Failed"
  
      End If
  DBCommunicator.QueryDB("Select * from EMP")
  DBCommunicator.DBdisConnect
  </code></pre>