Reputation: 11
I have a macro to delete values from a table I would like to add the last time this macro run. I manage to find a way to input Date inside of it and save, but when I open Access again, it has the original value in it.
My code is as follows:
Private Sub Comando0_Click()
dataate = InputBox("Periodo a ser deletado até [DD/MM/AAAA]:")
If IsDate(dataate) Then
dataate = Format(CDate(dataate), "dd\/mm\/yyyy")
End If
Dim qdf As DAO.QueryDef
Dim count As Long
Set qdf = CurrentDb().QueryDefs("Teste 1")
With qdf
.Parameters("[prmDataAte]").Value = CDate(dataate)
.Execute dbFailOnError
count = .RecordsAffected
End With
MsgBox " " & count & " registros foram apagados da tbl_tabela_de_preco"
Me.teste3.Caption = Now
DoCmd.Save acForm, Me.Name
End Sub
So far I tried using textbox and labels and had encountered the same problem. And tried other codes that i found:
theFormName = Me.OpenArgs
Set theform = Forms.Item("Formulário1")
Me.teste3.Caption = Now
theform.teste2.Caption = "Dados excluídos até: " & dataate
DoCmd.Save acForm, Me.Name
DoCmd.Save , "Formulário1"
DoCmd.Close acForm, Me.Name, acSaveNo
Upvotes: 1
Views: 29
Reputation: 27644
Issues with your code:
If IsDate(dataate) Then
dataate = Format(CDate(dataate), "dd\/mm\/yyyy")
End If
This has no Else
or Exit Sub
clause, so the rest of the code will run always, even if no valid date was entered.
Me.teste3.Caption = Now
DoCmd.Save acForm, Me.Name
You can set control properties (like Caption) in Form View, but you cannot save them permanently. So the DoCmd.Save
has no effect here.
You would have to do this in Design View. But this is not a good way to do this.
Better: in Form_Load()
, read the date from the table where you saved it with the update query. Then set the caption with that date.
Upvotes: 1