Private Sub btnCrear_Click()
On Error GoTo hay_error
Dim frmEmpleado As Form ' objeto formulario
Dim strName As String ' nombre del formulario
Dim ctl As Control ' objeto control
Dim btn As CommandButton ' objeto boton de comando
Dim frm As Access.AccessObject
Dim flag As Boolean
DoCmd.SetWarnings (False)
If IsNull(Me.ctlNombre) Then
MsgBox "Se requiere el nombre del formulario", vbInformation, "Cuidado..."
Me.ctlNombre.SetFocus
Exit Sub
End If
'Verifico si ya existe el formulario
For Each frm In Application.CurrentProject.AllForms
If Trim(Me.ctlNombre) = frm.Name Then
If MsgBox("El formulario " & Me.ctlNombre & " ya existe" & vbCrLf & _
"¿Lo reemplaza", vbOKCancel + vbQuestion + vbDefaultButton2, "Cuidado...") = vbCancel Then
flag = True
Exit For
End If
End If
Next frm
If flag Then
Exit Sub
End If
Set frmEmpleado = Application.CreateForm
With frmEmpleado
.Caption = "Empleado"
.Width = 5000
.Section(acDetail).Height = 2000
.NavigationButtons = False
.RecordSelectors = False
.AutoCenter = True
.PopUp = True
.Modal = True
.BorderStyle = 3
strName = frmEmpleado.Name
End With
' Incorporar cuadro de texto nombre del empleado
Set ctl = Application.CreateControl(strName, acTextBox, , "", "", 3000, 500, 2500, 300)
With ctl
'nombre del cuadro de texto
.Name = "txtNombre"
.BackColor = vbWhite
.ForeColor = vbBlack
.FontBold = True
End With
'Incorporar etiqueta nombre del empleado
Set ctl = Application.CreateControl(strName, acLabel, , "", "", 500, 500, 2500, 300)
With ctl
'nombre de la etiqueta
.Name = "lblNombre"
'titulo de la etiqueta
.Caption = "Apellido del empleado"
'Colores
.BackColor = vbWhite
.ForeColor = vbBlue
End With
'Creamos el botón
Set btn = CreateControl(strName, acCommandButton, , "", "", 6000, 1200, 1000, 400)
btn.Caption = "Cerrar" 'Titulo del botón
btn.OnClick = "=CerrarFormulario(""" & Me.ctlNombre & """)" 'Llamada a la función desde el botón de comando
'Guardamos el formulario
DoCmd.Save , Me.ctlNombre
'Abrimos el formulario creado
DoCmd.OpenForm Me.ctlNombre
'Cerramos este formulario
DoCmd.Close acForm, Me.Form.Name
hay_error_exit:
Exit Sub
hay_error:
If Err.Number < 0 Then 'Error de automatización
Exit Sub
Else
MsgBox "Ocurrió el error " & Err.Number, vbInformation, "Cuidado..."
Resume hay_error_exit
End If
End Sub