Guardar cambio en tiempo de ejecución para una etiqueta

Sé como se cambia con vba access el texto de una etiqueta en tiempo de ejecución, pero se puede guardar ese cambio de forma definitiva, ¿qué al cerrar y abrir el formulario se mantenga el cambio?

1 respuesta

Respuesta
1

Lo puede hacer de 2 formas:

1. Creando un archivo de texto con el nombre de la etiqueta. Y al abrir el formulario leer el archivo para reemplazar el texto de la etiqueta.

2. Guardar en una tabla el nombre de la etiqueta, esto implica:

a- Borrar el contenido si hay etiqueta

b. Adicionar el nuevo texto

c. Leer el texto de la tabla y asignarlo a la etiqueta al abrir el formulario

Ó

a. Verificar si hay texto y

    b. Si hay hacer un UPDATE.

     c. Leer el texto de la tabla y asignarlo a la etiqueta al abrir el formulario

Le preparé la primera opción: Consta de un formulario y un módulo que contiene 2 funciones

FORMULARIO

Observe tengo el texto por defecto "HOLA AMIGO", hago clic en el botón "Cambiar Titulo Etiqueta" y obtengo:

Ingreso para este ejemplo la palabra TODOEXPERTOS y hago clic en Aceptar. Ahora vuelvo a abrir el formulario y obtengo:

CÓDIGO DEL BOTON Cambiar Titulo Etiquieta

Private Sub btnCambiar_Click()
 Dim strTexto As String
 strTexto = InputBox("Escriba el texto", "TEXTO ETIQUEA")
 If Len(strTexto) > 0 Then
   Call crea_texto(strTexto)
 End If
End Sub

CÓDIGO DEL EVENTO AL ABRIR DEL FORMULARIO

Private Sub Form_Open(Cancel As Integer)
'Recupero el nombre de la etiqueta
Dim strTxt As String
strTxt = RecuperaEtiqueta
If strTxt <> "" Then
  Me.lblEtiqueta.Caption = strTxt
End If
End Sub

CÓDIGO DE LAS FUNCIONES

Public Function crea_texto(strTexto As String)
    Dim fso As Object
    Dim archivo As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set archivo = fso.CreateTextFile(CurrentProject.Path & "\etiqueta.txt", True, True)
    archivo.WriteLine strTexto
    archivo.Close
    Set fso = Nothing
    Set archivo = Nothing
End Function
Public Function RecuperaEtiqueta() As String
    On Error GoTo existefile_Error
    Dim fs, f, S, ts
    Dim strFile As String
    strFile = CurrentProject.Path & "\etiqueta.txt"
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFile(strFile)
    Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault)
    S = ts.ReadLine
    ts.Close
    RecuperaEtiqueta = LTrim(S)
    Set fs = Nothing
    Set f = Nothing
existefile_Error_Exit:
    Exit Function
existefile_Error:
    If Err.Number = 53 Then    ' No existe el archivo
        MsgBox "No existe el archivo etiqueta.txt en la carpeta del programa ...", vbCritical, "Le informo"
    End If
    Resume existefile_Error_Exit
End Function

No puse control de errores en la primera función, esa se la dejo. Si quiere el ejemplo lo puede solicitar a [email protected], favor en el asunto anotar la consulta.

¡Gracias! de corazón, voy a estudiar tus variantes para luego probar, creo no debo tener problemas. Gracias desde la Cuba linda!! 

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas