Hola a todos. En el código que a continuación expongo y en la línea “If rsPed.State Then…” de “Form_Unload” tengo el siguiente error: Error ‘91’ en tiempo de ejecución: Variable de tipo Object o la variable de bloque WITH no está establecida. Aclaraciones: 1.- En txtAñoINF_KeyPress y Sub txtNuPeINF que son los primeros TextBox no me da ese error, sin embargo en los siguientes txtAñoSEA y txtNuPeSEA es donde me da este error. 2.- Los TabIndex están ordenados, comenzando txtAñoINF con el “1”. El “0” lo tiene el DataGrid que no está visible. Código: Código:
Option Explicit
Dim cNumPedINF As String * 7
Dim CError As Integer 'Si vale 0 graba datos, si vale 1 no graba datos
Dim x As Variant
Private Sub Form_Load()
Set rsPed = New ADODB.Recordset
With rsPed
'Obliga a cerrar el RecordSet caso de estar abierto, para abrirlo seguidamente sin que de error
If .State Then .Close
.Open "SELECT * FROM PedidSEA", g_db, adOpenStatic, adLockOptimistic
If .RecordCount = 0 Then
x = MsgBox("Este fichero está todavía vacío.", vbInformation)
Unload Me
End If
End With
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Obliga a cerrar los RecordSet caso de estar abiertos
If rsPed.State Then rsPed.Close
'Libera toda la memoria y recursos del sistema asociados a los RecordSet
Set rsPed = Nothing
End Sub
Private Sub Barra_ButtonClick(ByVal Button As MSComctlLib.Button)
Select Case Button.Index
Case 1
Unload Me
End Select
End Sub
Private Sub txtAñoINF_GotFocus()
On Local Error Resume Next
txtAñoINF.Text = Mid(Str(Year(Date)), 4, 2)
Me.txtAñoINF.SelStart = 0
Me.txtAñoINF.SelLength = Len(Me.txtAñoINF.Text)
End Sub
Private Sub txtAñoINF_KeyPress(KeyAscii As Integer)
'Limitar solo entrada dígitos numéricos
If KeyAscii = 27 Then
Unload Me
ElseIf KeyAscii = 13 Then
KeyAscii = 0
SendKeys "{tab}"
ElseIf KeyAscii <> 8 Then
If (KeyAscii < vbKey0 Or KeyAscii > vbKey9) And KeyAscii <> vbKeySubtract Then
Beep
KeyAscii = 0
End If
End If
End Sub
Private Sub txtNuPeINF_GotFocus()
On Local Error Resume Next
Me.txtNuPeINF.SelStart = 0
Me.txtNuPeINF.SelLength = Len(Me.txtNuPeINF.Text)
End Sub
Private Sub txtNuPeINF_KeyPress(KeyAscii As Integer)
'Limitar solo entrada dígitos numéricos
If KeyAscii = 27 Then
Unload Me
ElseIf KeyAscii = 13 Then
KeyAscii = 0
SendKeys "{tab}"
ElseIf KeyAscii <> 8 Then
If (KeyAscii < vbKey0 Or KeyAscii > vbKey9) And KeyAscii <> vbKeySubtract Then
Beep
KeyAscii = 0
End If
End If
End Sub
Private Sub txtAñoSEA_GotFocus()
On Local Error Resume Next
txtAñoSEA.Text = Mid(Str(Year(Date)), 4, 2)
CError = 0
If Len(Trim(txtNuPeINF.Text)) <> 4 Then
MsgBox "Error. Número de pedido incompleto.", vbCritical
Else
cNumPedINF = txtAñoINF.Text + txtGuiINF.Text + txtNuPeINF.Text
'Cogemos los datos de una tabla creando un Recordset
Set rsPed = New ADODB.Recordset
strSQL = "SELECT * FROM PedidSEA WHERE Numero = '" & cNumPedINF & "'"
Set rsPed = g_db.Execute(strSQL)
'Comprobamos que exista
If rsPed.EOF Then
MsgBox "Error. El número de Pedido introducido no existe.", vbInformation
CError = 1
End If
rsPed.Close
Set rsPed = Nothing
If CError = 1 Then
txtNuPeINF.Text = ""
txtNuPeINF.SetFocus
End If
End If
Me.txtAñoSEA.SelStart = 0
Me.txtAñoSEA.SelLength = Len(Me.txtAñoSEA.Text)
End Sub
Private Sub txtAñoSEA_KeyPress(KeyAscii As Integer)
'Limitar solo entrada dígitos numéricos
If KeyAscii = 27 Then
Unload Me
ElseIf KeyAscii = 13 Then
KeyAscii = 0
SendKeys "{tab}"
ElseIf KeyAscii <> 8 Then
If (KeyAscii < vbKey0 Or KeyAscii > vbKey9) And KeyAscii <> vbKeySubtract Then
Beep
KeyAscii = 0
End If
End If
End Sub
Private Sub txtNuPeSEA_GotFocus()
On Local Error Resume Next
Me.txtNuPeSEA.SelStart = 0
Me.txtNuPeSEA.SelLength = Len(Me.txtNuPeSEA.Text)
End Sub
Private Sub txtNuPeSEA_KeyPress(KeyAscii As Integer)
'Limitar solo entrada dígitos numéricos
If KeyAscii = 27 Then
Unload Me
ElseIf KeyAscii = 13 Then
KeyAscii = 0
SendKeys "{tab}"
ElseIf KeyAscii <> 8 Then
If (KeyAscii < vbKey0 Or KeyAscii > vbKey9) And KeyAscii <> vbKeySubtract Then
Beep
KeyAscii = 0
End If
End If
End Sub