Visual Basic Foro

Programación => Bases de Datos => Mensaje iniciado por: omarinho23 en Junio 01, 2010, 06:13:28 pm

Título: [DUDA] La operacion no esta permitida si el objeto esta cerrado
Publicado por: omarinho23 en Junio 01, 2010, 06:13:28 pm
BUENAS CON TODOS ME SALE ESTE ERROR PERO NO ENCUENTRO ONDE AGREGAR ALGO PARA Q MODIFIQUE NORMAL
Código: [Seleccionar]
Private Sub cmdSave_Click()
   
    Select Case ACCION
    Case editar_registro
[color=yellow]        CNN.Execute "UPDATE Clientes set razonsocial = '" & Text1(1) & "', direccion = '" & Text1(2) & "', nºdeplaca = '" & Text1(3) & "', marca = '" & Text1(4) & "', modelo = '" & Text1(5) & "', carroceria = '" & Text1(6) & "', colores = '" & Text1(7) & "', nºcilindros = '" & Text1(8) & "', pesoneto = '" & Text1(9) & "', cargautil = '" & Text1(10) & "', pesobruto = '" & Text1(11) & "', longitud = '" & Text1(12) & "', ancho = '" & Text1(13) & "', altura = '" & Text1(14) & "', clase = '" & Text1(15) & _
                                     "', añofabrica = '" & Text1(16) & "', combustible = '" & Text1(17) & "', nºejes = '" & Text1(18) & "', nºdemotor = '" & Text1(19) & "', nºdeserieochasis = '" & Text1(20) & "', nºpasajeros = '" & Text1(21) & "', nºasientos = '" & Text1(22) & "', nºruedas = '" & Text1(23) & "', formula = '" & Text1(24) & "', categoria = '" & Text1(25) & "', potencia = '" & Text1(26) & "', detalle1 = '" & Text1(27) & "', detalle2 = '" & Text1(28) & "', fecha = '" & Text1(29) & "', modificacion = '" & Text1(30) & "', montaje = '" & Text1(31) & "', fabricacion = '" & Text1(32) & _
                                     "' where Cod = " & IdRegistro & ""[/color]
    End Select
   
    rs.Requery
   
    Call CargarListView(FrmPrincipalClau.LV, rs)

    DoEvents
    Unload Me
    Set frmImpresionCIT3 = Nothing
Exit Sub
ErrorSub:
MsgBox Err.Description

End Sub

ahi es donde me sale el error ... alguna solucion por favor es urgente
Título: Re:[DUDA] La operacion no esta permitida si el objeto esta cerrado
Publicado por: omarinho23 en Junio 01, 2010, 06:16:52 pm
BUENAS CON TODOS ME SALE ESTE ERROR PERO NO ENCUENTRO ONDE AGREGAR ALGO PARA Q MODIFIQUE NORMAL
Código: [Seleccionar]
Private Sub cmdSave_Click()
   
    Select Case ACCION
    Case editar_registro
error---        CNN.Execute "UPDATE Clientes set razonsocial = '" & Text1(1) & "', direccion = '" & Text1(2) & "', nºdeplaca = '" & Text1(3) & "', marca = '" & Text1(4) & "', modelo = '" & Text1(5) & "', carroceria = '" & Text1(6) & "', colores = '" & Text1(7) & "', nºcilindros = '" & Text1(8) & "', pesoneto = '" & Text1(9) & "', cargautil = '" & Text1(10) & "', pesobruto = '" & Text1(11) & "', longitud = '" & Text1(12) & "', ancho = '" & Text1(13) & "', altura = '" & Text1(14) & "', clase = '" & Text1(15) & _
error---                                     "', añofabrica = '" & Text1(16) & "', combustible = '" & Text1(17) & "', nºejes = '" & Text1(18) & "', nºdemotor = '" & Text1(19) & "', nºdeserieochasis = '" & Text1(20) & "', nºpasajeros = '" & Text1(21) & "', nºasientos = '" & Text1(22) & "', nºruedas = '" & Text1(23) & "', formula = '" & Text1(24) & "', categoria = '" & Text1(25) & "', potencia = '" & Text1(26) & "', detalle1 = '" & Text1(27) & "', detalle2 = '" & Text1(28) & "', fecha = '" & Text1(29) & "', modificacion = '" & Text1(30) & "', montaje = '" & Text1(31) & "', fabricacion = '" & Text1(32) & _
error---                                     "' where Cod = " & IdRegistro & ""[/color]
    End Select
   
    rs.Requery
   
    Call CargarListView(FrmPrincipalClau.LV, rs)

    DoEvents
    Unload Me
    Set frmImpresionCIT3 = Nothing
Exit Sub
ErrorSub:
MsgBox Err.Description

End Sub

ahi es donde me sale el error ... alguna solucion por favor es urgente
Título: Re:[DUDA] La operacion no esta permitida si el objeto esta cerrado
Publicado por: Ever Cerna en Junio 01, 2010, 11:42:02 pm
Creo que alli esta el error te faltaron algunas comillas simples. Remplaza solo esa linea.

Código: (vb) [Seleccionar]
"' where Cod = '" & IdRegistro & "'"
Salu2.
Título: Re:[DUDA] La operacion no esta permitida si el objeto esta cerrado
Publicado por: omarinho23 en Junio 02, 2010, 11:56:44 am
lo puse y me sigue saliendo error 3704 La operacion no esta permitida si el objeto esta cerrado
Título: Re:[DUDA] La operacion no esta permitida si el objeto esta cerrado
Publicado por: ssccaann43 en Junio 02, 2010, 12:22:01 pm
Citar
La operacion no esta permitida si el objeto esta cerrado

Debes aperturar el Recordset...!
Título: Re:[DUDA] La operacion no esta permitida si el objeto esta cerrado
Publicado por: omarinho23 en Junio 02, 2010, 12:24:34 pm
Option Explicit

Enum EACCION
    AGREGAR_REGISTRO = 0
    editar_registro = 1
End Enum
Dim CNN As New ADODB.Connection
Public ACCION As EACCION
Dim rs As ADODB.Recordset
Public IdRegistro


ya esta echo
Título: Re:[DUDA] La operacion no esta permitida si el objeto esta cerrado
Publicado por: ssccaann43 en Junio 02, 2010, 12:44:47 pm
No me gusta la manera en que haces las cosas...!

1era recomendación: Declara PUBLIC en un modulo bas el CNN as New ADODB.Connection

Seguido de esto, en el MAIN de tú proyect, aperturas la conexión:
Código: (vb) [Seleccionar]
Sub Main()
Dim StringCnn as String
StringCnn="Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=TUBDENSQL;Data Source=SERVER"
CNN.CursorLocation = adUseServer
CNN.ConnectionString = StringCnn
CNN.CommandTimeout = 0
End Sub

2da recomendación: Crear una función para aperturar las tablas y pasarlas a cualquier Recordset que declaremos...

en el modulo bas

Código: (vb) [Seleccionar]
Public Sub ABRIR_TABLA(Xrecordset As ADODB.Recordset, xselect As String, NWConnection As ADODB.Connection, tipocursor As Integer, Optional MuestraError As Boolean)
  On Error GoTo basedato
 
 
  If Xrecordset.State = adStateOpen Then Xrecordset.Close

     Xrecordset.ActiveConnection = NWConnection
     Xrecordset.CursorType = adOpenDynamic
     Xrecordset.CursorLocation = tipocursor
     Xrecordset.LockType = adLockOptimistic
     Xrecordset.Open xselect
     Exit Sub

  On Error GoTo 0
 
basedato:
 If MuestraError = False Then
    If MsgBox("Problema de Base de Datos " + Err.Description + " error numero " + str(Err.Number), vbCritical +  vbRetryCancel, "Atencion") = vbRetry Then
       Resume
    End If
 Else
   Resume Next
 End If
End Sub

Ahora bien, Cómo hacemos para ejecutar un select, un update, un delete?

R=SIMPLE...

Ejecutar un DELETE

Código: (vb) [Seleccionar]
Private Sub BotonEliminar_Click()
Dim RsEliminar As New ADODB.Recordset
ABRIR_TABLA RsEliminar, "delete from tu_tabla where codigo= ' " & Tu_Codigo & " ' ", CNN, 2
End Sub

Ejecutar un Update

Código: (vb) [Seleccionar]
Private Sub BotonUpdate_Click()
Dim RsActualizar As New ADODB.Recordset
OPENTABLA RsActualizar, "update Tu_Tabla set campo=' " & Descripcion & " ' where codigo=' " & Tu_Codigo & " ' ", CNN, 2
End Sub

Un insert (Yo lo hago de esta manera)

Código: (vb) [Seleccionar]
Private Sub BotonGuardar_Click()
   Dim RSupdate As New ADODB.Recordset
      ABRIR_TABLA RSupdate, "SELECT * FROM tu_tabla", CNN, 2
      If RSupdate.State = 1 Then
            RSupdate.AddNew
            RSupdate!Campo1 = txtCampo1.Text
            RSupdate!Campo2 = txtCampo2.Text
            RSupdate!Campo3 = txtCampo3.Text
            RSupdate!Campo4 = txtCampo4.Text
            RSupdate.Update
      End If
End Sub

Puedes actualizar incluso de la siguiente manera...

Código: (vb) [Seleccionar]
Private Sub BotonActualizar2_Click()
   Dim RSupdate As New ADODB.Recordset
      ABRIR_TABLA RSupdate, "SELECT * FROM tu_tabla where codigo=' " & Tu_Codigo & " ' ", CNN, 2
      If RSupdate.State = 1 Then
         If RSupdate.EOF = False Then
            RSupdate!Campo3 = txtCampo3.Text
            RSupdate!Campo4 = txtCampo4.Text
            RSupdate.Update
        End If
      End If
End Sub

Espero comprendas y puedas darle un excelente uso...! Saludos
Título: Re:[DUDA] La operacion no esta permitida si el objeto esta cerrado
Publicado por: YAcosta en Junio 02, 2010, 01:55:45 pm
La propuesta de Miguel es ordenada y permite un mejor mantenimiento, yo sigo esa linea.