• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA: How to update a ADODB recordset?

inddon

Member
Hello There,

I would like to know how to update a populated recordset using ADODB connection.

Would like to know the following:
1. how to update the field 'Hiredate' in the recordset to a format of "DD-Mmm-YYYY"
2. The field 'Hiredate' is displayed in column G3. Without updating the above (1), how can one format the column G3,G4.... so that its format is set to "DD-Mmm-YYYY". I did column format to "DD-Mmm-YYYY", but it overwrites to its default DD MM YY.

Below is the code for your reference:

Code:
Private Sub CommandButton2_Click()
  Dim c As New ADODB.Connection
  Dim RecordSet As New ADODB.RecordSet
  Dim cmd As New ADODB.Command
  Dim param1 As New ADODB.Parameter
  Dim param2 As New ADODB.Parameter

  Dim objErr As ADODB.Error
  Dim Message, Title, Default, DeptValue
   
  Dim ExcelRange As Range
  Dim ws As Worksheet
   
  c.ConnectionString = "Provider=OraOLEDB.Oracle;" & _
  "Data Source=(DESCRIPTION=(CID=GTU_APP)(ADDRESS_LIST=" & _
  "(ADDRESS=(PROTOCOL=TCP)(HOST=LocalHost)(PORT=1521)))" & _
  "(CONNECT_DATA=(SERVICE_NAME=XE)(SERVER=DEDICATED)));" & _
  "User Id=System;Password=oracle;"
  c.Open
  Set cmd.ActiveConnection = c
  Set RecordSet = CreateObject("ADODB.Recordset")
 
  DeptValue = 30
   
  On Error GoTo err_test

  ' set parameters
  Set param1 = cmd.CreateParameter("param1", adSmallInt, adParamInput, , DeptValue)
  cmd.Parameters.Append param1
  Set param2 = cmd.CreateParameter("param2", adSmallInt, adParamOutput)
  cmd.Parameters.Append param2
   
  ' Enable PLSQLRSet property
  cmd.Properties("PLSQLRSet") = True
   
  'Columns of table EMP: EMPNO,ENAME,JOB, MGR, HIREDATE, SAL, COMM, DEPTNO
  cmd.CommandText = "{CALL Employees.GetEmpRecords(?, ?)}"
  Set RecordSet = cmd.Execute
   
  'Disable PLSQLRSet property
  cmd.Properties("PLSQLRSet") = False
   
  Set ws = ActiveWorkbook.Sheets("Sheet1")
  Set ExcelRange = ws.Range("C3")
   
  'RecordSet.Fields("Hiredate").Value = Format(RecordSet.Fields("Hiredate").Value, "DD-MMM-YYY")
  'RecordSet.Update

  ExcelRange.CopyFromRecordset RecordSet
  RecordSet.Close

  Exit Sub

err_test:
  MsgBox Error$
  For Each objErr In c.Errors
  MsgBox objErr.Description
  Next
  c.Errors.Clear
  Resume Next
End Sub

Thank you for your help

Regards
Don
 
Back
Top