Les paso el código que tengo para ese formulario. Y la conexión a la base de datos la hago por medio del control Ado y lo conecto por medio de las propiedades connectionstring y recourdsource.
Muchas gracias.
Código:
'Botón Guardar
Private Sub Command2_Click()
'Si alguno de los datos obligatorios está vacío, msgbox de aviso
If (Text1.Text = "") Or (Text2.Text = "") Or (Text3.Text = "") Or (Text4.Text = "") Then
MsgBox "Complete los datos obligatorios", vbCritical, "No se puede guardar"
'Si todos los datos obligatorios han sido ingresados, se procede a guardar
Else
ver = MsgBox("¿Está seguro que desea guardar el nuevo artículo?", vbYesNo, "Confirme")
If ver = vbYes Then
Adodc1.Recordset.Update
MsgBox "El artículo se ha guardado correctamente", vbInformation
Command3.Visible = True
Command2.Visible = False
Text1.Enabled = False
Text2.Enabled = False
Text3.Enabled = False
Text4.Enabled = False
Text5.Enabled = False
Else
Adodc1.Recordset.CancelUpdate
Text1 = ""
Text2 = ""
Text3 = ""
Text4 = ""
Text5 = ""
Text1.SetFocus
End If
End If
End Sub
'Botón INGRESAR OTRO ARTÍCULO
Private Sub Command3_Click()
Adodc1.Recordset.AddNew
Command3.Visible = False
Command2.Visible = True
Text1.Enabled = True
Text2.Enabled = True
Text3.Enabled = True
Text4.Enabled = True
Text5.Enabled = True
Text1.SetFocus
End Sub
'Botón Salir
Private Sub Command4_Click()
'cierra el formulario actual y abre el formulario de menú
Unload Me
Form3.Show
End Sub
'FORMULARIO
Private Sub Form_Load()
Adodc1.Recordset.AddNew
Adodc1.Visible = False
Command3.Visible = False
End Sub
'Evento sobre el textbox de Precio de Artículo
Private Sub Text3_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 ' Permite los núneros (0-9)
Case 8 ' Permite el caracter de retroceso
Case 46 ' Permite el caracter punto (.)
Case Else
KeyAscii = 0
Beep
MsgBox "Ingrese solo números y el" & Chr(13) & "caracter punto (.) si es un precio con decimales", vbCritical, "Error"
End Select
End Sub
Private Sub Text3_LostFocus()
Dim preciomenor As Double
Dim preciomayor As Double
Dim valorintro As Double
If Len(Trim(Text3.Text)) >= 1 Then
If IsNumeric(Text3) Then
valorintro = CDbl(Me.Text3.Text)
preciomenor = 0.1
preciomayor = 2000
If valorintro < preciomenor Then
Text3 = ""
Text3.SetFocus
MsgBox "No se pueden ingresar precios menores a $0.10", vbCritical, "Atención"
ElseIf valorintro > preciomayor Then
Text3 = ""
Text3.SetFocus
MsgBox "No se pueden ingresar precios mayores a $2000 ", vbCritical, "Atención"
End If
Else
MsgBox "Lo que ingresó no es un precio"
End If
Else
KeyAscii = 0
End If
End Sub
'VALIDACIÓN TEXT 4 (fECHA)
Private Sub Text4_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case 48 To 57 ' Permite los números (0-9)
Case 8 ' Permite el caracter de retroceso
Case 47 ' Permite caracter (/)
Case Else
KeyAscii = 0
Beep
MsgBox "Ingrese solo números y el caracter" & Chr(13) & "de barra diagonal separadora de fechas (/)", vbCritical, "Error"
End Select
End Sub
'Evento sobre el textbox de Fecha de Ingreso
Private Sub Text4_LostFocus()
Dim fechaini As Date
Dim fechatope As Date
'Si el textbox está lleno
If Len(Trim(Text4.Text)) >= 1 Then
'función que comprueba si el parámetro enviado corresponde o no con un valor convertible en Date.
If IsDate(Text4) Then
fechaini = "16/06/2006"
fechatope = Date
If Text4 < fechaini Then
MsgBox "No se puede ingresar una fecha anterior al 16/06/2006", vbInformation, "Atención"
Text4.SetFocus
Text4 = ""
ElseIf Text4 > fechatope Then
MsgBox "No se puede ingresar una fecha posterior a la de hoy", vbInformation, "Atención"
Text4.SetFocus
Text4 = ""
End If
Else
MsgBox "Dato ingresado no es fecha", vbInformation, "Atención"
Text4.SetFocus
Text4 = ""
End If
Else
KeyAscii = 0
End If
End Sub


