PDA

View Full Version : Recordset Error



Paleo
02-27-2005, 06:01 PM
This is the code:

Option Compare Database
Option Explicit
Dim rs As New ADODB.Recordset
Dim cnn As New ADODB.Connection


Private Sub btnAlterar_Click()
On Error GoTo trataerro
If Me!btnAlterar.Caption = "&Alterar" Then
Me!txtCodigo.SetFocus
Me!btnAlterar.Caption = "&Grava"
Exit Sub
End If
If Me!btnAlterar.Caption = "&Grava" Then
grava_rec
rs.Update
Me!btnAlterar.Caption = "&Alterar"
MsgBox "Altera??o gravada com sucesso !"
Resume
End Sub


Private Sub btnAnterior_Click()
rs.MovePrevious
If rs.BOF Then
Me!btnPrimeiro.Enabled = False
Me!btnAnterior.Enabled = False
rs.MoveNext
Exit Sub
End If
load_rec
End Sub

Private Sub btnBuscar_Click()
Dim resposta As String
Dim marca As Variant
resposta = InputBox("Informe o nome do autor a localizar ", _
"Localizar autor")
If resposta <> "" Then
marca = rs.Bookmark
Else
Exit Sub
End If
rs.Find "NomeAutor=" & resposta
If rs.EOF Then
MsgBox "N?o encontrei o autor " & resposta
rs.Bookmark = marca
Exit Sub
End If
load_rec
End Sub

Private Sub btnExcluir_Click()
If (MsgBox("Deseja excluir - " & rs.Fields("NomeAutor"), _
vbYesNoCancel) = vbYes) Then
rs.Delete
rs.MovePrevious
If rs.BOF Then
Me!btnPrimeiro.Enabled = False
Me!btnAnterior.Enabled = False
Exit Sub
Else
load_rec
End If
End If
End Sub

Private Sub btnIncluir_Click()
On Error GoTo traterro
If Me!btnIncluir.Caption = "&Inclui" Then
rs.AddNew
clear_ctrls
Me!txtCodigo.SetFocus
Me!btnIncluir.Caption = "&Grava"
Exit Sub
End If
If Me!btnIncluir.Caption = "&Grava" Then
grava_rec
rs.Update
Me!btnIncluir.Caption = "&Inclui"
marca = rs.Bookmark
load_rec
MsgBox "O registro foi gravado com sucesso !"
End If
btnbusca_fim:
Exit Sub
trataerro:
MsgBox "Erro = " & Err.Number & " - " & Err.Description
Resume btnbusca_fim
End Sub

Private Sub btnPrimeiro_Click()
rs.MoveFirst
load_rec
End Sub

Private Sub btnProximo_Click()
rs.MoveNext
If rs.EOF Then
Me!btnProximo.Enabled = False
Me!btnUltimo.Enabled = False
Exit Sub
End If
load_rec
End Sub

Private Sub btnUltimo_Click()
rs.MoveLast
load_rec
End Sub

Private Sub Form_Load()

'Abre a conex?o
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = _
C:\SerieWeb\asp\CADLIVRO.MDB"
'Abre o Recordset
rs.CursorLocation = adUseClient
rs.Open "SELECT * FROM Livros", cnn, adOpenKeyset, adLockOptimistic
load_rec
Me!txtHoras.Text = Format(Time(), "hh:mm:ss")

End Sub

Private Sub load_rec()
'Me!txtID.Text = "" & rs.Fields("id")
Me!txtCodigo.Text = "" & rs.Fields("Codigo")
Me!txtCategoria.Text = "" & rs.Fields("Categoria")
Me!txtAutor.Text = "" & rs.Fields("NomeAutor")
Me!txtLivro.Text = "" & rs.Fields("NomeLivro")
Me!txtPosicao.Text = rs.AbsolutePosition
Me!txtTotal.Text = rs.RecordCount
End Sub

Private Sub grava_rec()
'rs.Fields("id") = "" & txtID
rs.Fields("Codigo") = "" & Me!txtCodigo.Text
rs.Fields("Categoria") = "" & Me!txtCategoria.Text
rs.Fields("NomeAutor") = "" & Me!txtAutor.Text
rs.Fields("NomeLivro") = "" & Me!txtLivro.Text
End Sub
Private Sub clear_ctrls()
Me!txtID.Text = ""
Me!txtCodigo.Text = ""
Me!txtCategoria.Text = ""
Me!txtAutor.Text = ""
Me!txtLivro.Text = ""
End Sub


It returns an error telling operation not allowed on a closed object.

Any ideas?:banghead:

Jacob Hilderbrand
02-27-2005, 06:41 PM
Open DB

Your Code Here

Close DB

Paleo
02-27-2005, 10:08 PM
Hi Jake,

its in there. The Sub Form_Load opens the DB.

flavo
02-28-2005, 06:09 AM
Try this

Private Sub Form_Load()
Dim cnn as new adodb.connection
dim rs as new adodb.recordset
'Abre a conex?o
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = _
C:\SerieWeb\asp\CADLIVRO.MDB"
'Abre o Recordset
rs.CursorLocation = adUseClient
rs.Open "SELECT * FROM Livros", cnn, adOpenKeyset, adLockOptimistic
load_rec
Me!txtHoras.Text = Format(Time(), "hh:mm:ss")

End Sub

Also, just out of prefrence i would declare your varibles like

private cnn as adodb.connection

and make sure you clean up after yourself! (ie close connection and recordset and set them to nothing!!!)

Dave

Paleo
02-28-2005, 06:23 AM
Hi Dave,

I will try this, thanks!