How to store data from one text-box into another on button click

Zak14

Registered User.
Local time
Today, 16:52
Joined
Jun 27, 2014
Messages
166
In my form, when a button (cmdContactedToday) is clicked, a text field to the left (txtDateContacted) is updated with today's date.

However, before this happens, I want the the current date in txtDateContacted to get stored in another (hidden) text field (txtPreviousDate)

Then when another (undo) button (cmdUndoDate) is clicked, the date in txtDateContacted should get replaced with the date in txtPreviousDate.

I've made it as simple to understand as I could.
Thank you much!
 
Last edited:
Are you sure this is the only field that you would want to revert?
Do you know how to write any VBA?
 
What do you mean 'revert'. And no, I don't know how to code VBA, although I do understand it a little.
 
I thought I replied but obviously it didn't come through. Strange!

Since you can understand some VBA, put this in the Click event of the cmdContactedToday button:
Code:
Me.txtPreviousDate = Me.txtDateContacted
Me.txtDateContacted = Date()
In the other button use this code:
Code:
Me.txtDateContacted = Me.txtPreviousDate
This is very basic code, more conditions can be added. Let's keep it simple for now so go ahead and test it out.
 
It didn't work. I put the code you detailed into the "On Click" events of each command button, cmdContactedToday and cmdUndoDate respectively (using the Code Builder).

Btw, before I entered your code, I deleted the code I had on cmdContactedToday, because I recognized a repeat of a similar command in your code. My (deleted) code was this:
Code:
Me![txtDateContacted] = Date

The second line of your 1st code works fine, but if I use the first line of both your codes, and I click on the button, it comes up with a run-time error: "You can't assign a value to this object".

Thanks
 
Last edited:
click on the button, it comes up with a run-time error: "You can't assign a value to this object".

This will be because the textbox has a Control Source that is a formula. Only unbound controls or controls bound to a recordset field can be updated.
 
This will be because the textbox has a Control Source that is a formula. Only unbound controls or controls bound to a recordset field can be updated.

Sorry, forgive me for my ignorance.
How do you know if a textbox has a formula as its control source? How do you change that?
And which textbox are you referring to?
txtDateContacted is bound to a table and it needs to be that way.
txtPreviousDate is unbound.
 
If a textbox uses a formula it would have a ControlSource that starts with an equal sign.

If the textbox is unbound or bound to a field in a table-based RecordSource it should work unless it is Locked or not Enabled.
 
If a textbox uses a formula it would have a ControlSource that starts with an equal sign.

If the textbox is unbound or bound to a field in a table-based RecordSource it should work unless it is Locked or not Enabled.

I don't know which textbox you're talking about but neither of them have a formula as their control source by the looks of it. There's just the names of the fields in the control source property.
 
Open up the Property Sheet of txtDateContacted and tell us what you have in its Control Source property.
 
Open up the Property Sheet of txtDateContacted and tell us what you have in its Control Source property.

LastContactDate

That's it. That's the name of the field in the table btw
 
Okay ! Unbound controls, do not have a control source. So when the Form loads, they will not have a value in it. On the other hand, Bound controls have a control source, by which they will have a direct relationship to the field in the table, that the form is bound to. Unbound controls cannot store values. They can temporarily hold values, that is only until the life time of the form. Once the form is closed the control's value will be gone with it.

So to answer your question, the code provided by vbaInet should work, if not working. Show us what you have done with it, and we will try to figure out what you are doing wrong.
 
This is all the code in the form

Code:
Private Sub cmdContactedToday_Click()
If MsgBox("Did you contact this referrer today?", vbYesNo) = vbYes Then
    Me.txtDateContacted = Date
    Me.cmdUndoDate.Visible = True
    Me.cmdUndoDate.SetFocus
End If
End Sub

Private Sub cmdUndoDate_Click()
Me.cmdContactedToday.SetFocus
Me.cmdUndoDate.Visible = False
End Sub
Everything above works fine, but once I add the following codes, it gives an error when I click on the buttons.
Code:
Me.txtDateContacted = Me.txtPreviousDate
Code:
Me.txtPreviousDate = Me.txtDateContacted
What am I doing wrong?
 
Where are you adding the last two line? Show the actual code that is throwing the error, the code you provide does not reflect the changes vbaInet provided.
 
To save us time, just upload your db with some test data and we'll have a look.
 
Where are you adding the last two line? Show the actual code that is throwing the error, the code you provide does not reflect the changes vbaInet provided.

This is what it looks like after I add the 2 lines in, but it doesn't work.
Code:
Private Sub cmdContactedToday_Click()
If MsgBox("Did you contact this referrer today?", vbYesNo) = vbYes Then
    Me.txtPreviousDate = Me.txtDateContacted
    Me.txtDateContacted = Date
    Me.cmdUndoDate.Visible = True
    Me.cmdUndoDate.SetFocus
End If
End Sub

Private Sub cmdUndoDate_Click()
Me.txtDateContacted = Me.txtPreviousDate
Me.cmdContactedToday.SetFocus
Me.cmdUndoDate.Visible = False
End Sub
 
As per request of vbaInet, I couldn't upload the database as it's for work and got confidential info, but I made a sample one with the relevant table and form with the same settings and that one happened to work. I realised, the difference between the two databases was that txtPreviousDate was unbound in the one that worked, so I deleted the text in the control source of txtPreviousDate (it was an unnecessary name) AND IT WORKED!

Thanks a lot everyone!
 
Glad to hear! But you said the problem was with txtDateContacted and not txtPreviousDate.

So I'm guessing you didn't want to save what's in txtPreviousDate in the first place?
 
Glad to hear! But you said the problem was with txtDateContacted and not txtPreviousDate.

So I'm guessing you didn't want to save what's in txtPreviousDate in the first place?

At first I did want to save what's in txtPreviousDate so that closing the form doesn't delete data in there, but now, come to think of it, it's not really necessary; it's proves no benefits.
 

Users who are viewing this thread

Back
Top Bottom