I am using VBA macro to fetch data from oracle and getting the error
"Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record."
I am able to fetch the column names but data is not displayed
Following is the code:
Public fromdate As Date
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim prm1 As ADODB.Parameter
Dim prm2 As ADODB.Parameter
Dim cmdTrans As New ADODB.Command
Dim col As Integer
Dim row As Integer
Dim strConn As String
Public todate As Date
Public Sub Analyze()
''''Input date from user
UserForm1.Show
strConn = "User ID=UserID ; Password=pswd ; Data Source=dsn_name ; Provider=provider"
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.CursorLocation = adUseServer
conn.Open strConn
Set cmdTrans.ActiveConnection = conn
Set cmdTrans = New ADODB.Command
cmdTrans.CommandText = "SELECT d.STAT_ID,process_name,message_name,from_app,to_app,sum(count)from eai_stats_detail d,eai_stats_master m where d.stat_id=m.id and d.start_time between to_date(?,'dd-Mon-yyyy') and to_date(?,'dd-Mon-yyyy') group by stat_id,process_name,message_name,from_app,to_app"
cmdTrans.CommandType = adCmdText
cmdTrans.Prepared = True
Set prm1 = cmdTrans.CreateParameter("From_Date", adDate, adParamInput, 10, fromdate)
cmdTrans.Parameters.Append prm1
Set prm2 = cmdTrans.CreateParameter("To_Date", adDate, adParamInput, 10, todate)
cmdTrans.Parameters.Append prm2
rs.ActiveConnection = conn
cmdTrans.ActiveConnection = conn
Set rs = cmdTrans.Execute
col = 0
'First Row: names of columns
Do While col < rs.Fields.Count
Cells(1, col + 1) = rs.Fields(col).Name
col = col + 1
Loop
'Now actual data as fetched from select statement
row = 1
Do Until rs.EOF
row = row + 1
col = 0
While col < rs.Fields.Count
Cells(row, col + 1) = rs.Fields(col).Value
col = col + 1
Wend
row = row + 1
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Sub
UserForm is used to get date from user.Here is the code for form:
Private Sub Calendar1_Click()
TextBox1.Value = Calendar1.Value
End Sub
Private Sub Calendar3_Click()
TextBox2.Value = Calendar3.Value
End Sub
Public Sub GetData_Click()
Dim Start_date As Date
Dim End_date As Date
Start_date = Trim(TextBox1.Text)
End_date = Trim(TextBox2.Text)
fromdate = Format(Start_date, "yyyy/mm/dd")
todate = Format(End_date, "yyyy/mm/dd")
Me.Hide
End Sub
Private Sub UserForm_Initialize()
Set cmdTrans = New ADODB.Command
If IsDate(TextBox1.Value) Then
Calendar1.Value = DateValue(TextBox1.Value)
Else
Calendar1.Value = Date
End If
If IsDate(TextBox2.Value) Then
Calendar3.Value = DateValue(TextBox2.Value)
Else
Calendar3.Value = Date
End If
End Sub
Please suggest me the solution for this error.I have to submit tomorrow to my manager.
"Either BOF or EOF is True, or the current record has been deleted. Requested operation requires a current record."
I am able to fetch the column names but data is not displayed
Following is the code:
Public fromdate As Date
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
Dim prm1 As ADODB.Parameter
Dim prm2 As ADODB.Parameter
Dim cmdTrans As New ADODB.Command
Dim col As Integer
Dim row As Integer
Dim strConn As String
Public todate As Date
Public Sub Analyze()
''''Input date from user
UserForm1.Show
strConn = "User ID=UserID ; Password=pswd ; Data Source=dsn_name ; Provider=provider"
Set conn = New ADODB.Connection
Set rs = New ADODB.Recordset
conn.CursorLocation = adUseServer
conn.Open strConn
Set cmdTrans.ActiveConnection = conn
Set cmdTrans = New ADODB.Command
cmdTrans.CommandText = "SELECT d.STAT_ID,process_name,message_name,from_app,to_app,sum(count)from eai_stats_detail d,eai_stats_master m where d.stat_id=m.id and d.start_time between to_date(?,'dd-Mon-yyyy') and to_date(?,'dd-Mon-yyyy') group by stat_id,process_name,message_name,from_app,to_app"
cmdTrans.CommandType = adCmdText
cmdTrans.Prepared = True
Set prm1 = cmdTrans.CreateParameter("From_Date", adDate, adParamInput, 10, fromdate)
cmdTrans.Parameters.Append prm1
Set prm2 = cmdTrans.CreateParameter("To_Date", adDate, adParamInput, 10, todate)
cmdTrans.Parameters.Append prm2
rs.ActiveConnection = conn
cmdTrans.ActiveConnection = conn
Set rs = cmdTrans.Execute
col = 0
'First Row: names of columns
Do While col < rs.Fields.Count
Cells(1, col + 1) = rs.Fields(col).Name
col = col + 1
Loop
'Now actual data as fetched from select statement
row = 1
Do Until rs.EOF
row = row + 1
col = 0
While col < rs.Fields.Count
Cells(row, col + 1) = rs.Fields(col).Value
col = col + 1
Wend
row = row + 1
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
conn.Close
Set conn = Nothing
End Sub
UserForm is used to get date from user.Here is the code for form:
Private Sub Calendar1_Click()
TextBox1.Value = Calendar1.Value
End Sub
Private Sub Calendar3_Click()
TextBox2.Value = Calendar3.Value
End Sub
Public Sub GetData_Click()
Dim Start_date As Date
Dim End_date As Date
Start_date = Trim(TextBox1.Text)
End_date = Trim(TextBox2.Text)
fromdate = Format(Start_date, "yyyy/mm/dd")
todate = Format(End_date, "yyyy/mm/dd")
Me.Hide
End Sub
Private Sub UserForm_Initialize()
Set cmdTrans = New ADODB.Command
If IsDate(TextBox1.Value) Then
Calendar1.Value = DateValue(TextBox1.Value)
Else
Calendar1.Value = Date
End If
If IsDate(TextBox2.Value) Then
Calendar3.Value = DateValue(TextBox2.Value)
Else
Calendar3.Value = Date
End If
End Sub
Please suggest me the solution for this error.I have to submit tomorrow to my manager.