PDA

View Full Version : Solved: Command button issues, please help.



detowne
03-07-2010, 02:55 PM
Ok there are 2 things I'm trying to do and can't figure out.

Question 1: I set the form's "Allow Edits" property to "No" so users can not edit the form. I created a button on the form that changes the "Allow Edits" property to True thus allowing users to edit. My problem is that I need the same button to turn edits back off when clicked again and back on when clicked after that and so on. Anyone have any ideas as to how I can accomplish this?

Question 2: I am making a command button that will auto fill "city" "state" "zip" and "country" with predetermined values. I am making a yes/no message box that pops if there is already data in those text box's that asks the user if they really want to change those text box's to their own input. Here's my problem, I have everything coded except the "no" button on the message box. How do I allow the user to click "no" and have the text box disregard what the user input and go back to what was there to begin with?

Thank you in advance for the help!!

CreganTur
03-08-2010, 08:07 AM
Question 1: I set the form's "Allow Edits" property to "No" so users can not edit the form. I created a button on the form that changes the "Allow Edits" property to True thus allowing users to edit. My problem is that I need the same button to turn edits back off when clicked again and back on when clicked after that and so on. Anyone have any ideas as to how I can accomplish this?

It's very simple. First, you need to check and see what the current value of the AllowEdits property is, and then make your change accordingly:

If Me.AllowEdits = false then
Me.AllowEdits = true
elseif Me.AllowEdits = true then
Me.AllowEdits = false
end if

You should probably change the caption of your button to match what it's going to do:

If Me.AllowEdits = false then
Me.AllowEdits = true
Me.ButtonName.Caption = "Turn Off Edits"
elseif Me.AllowEdits = true then
Me.AllowEdits = false
Me.ButtonName.Caption = "Turn On Edits"
end if


Question 2: I am making a command button that will auto fill "city" "state" "zip" and "country" with predetermined values. I am making a yes/no message box that pops if there is already data in those text box's that asks the user if they really want to change those text box's to their own input. Here's my problem, I have everything coded except the "no" button on the message box. How do I allow the user to click "no" and have the text box disregard what the user input and go back to what was there to begin with?

Easiest way I can think of is to just undo any changes on the form:

If Me.Dirty Then
DoCmd.RunCommand acCmdUndo
Else
Exit Sub
End If
I check to see if it's dirty before running the Undo command. Otherwise, you'll get an error message because there's nothing to undo.

HTH:thumb

detowne
03-08-2010, 04:27 PM
First off thank you very much for trying to help me out. I still can't get either to work however. I had already tried the code you provided me with for the edit button and it will turn the edits on but not back off. I read somewhere you need to set the button up so it saves the form after the edit is made so the form goes back to the default setting which in this case Allow Edits = No. I just can't figure out how to do that.

I still can't get the "No" button in my vbYes/No box to return the textbox to the value it had prior to the user making a change.

I posted again because I wanted to re-word my first question and didn't realize until after that you can edit posts. I'm new to forum posting. :)

Any other ideas as to how I can make either of these work?

detowne
03-08-2010, 05:47 PM
Private Sub cmdAutoCSZC_Click()
Dim ans As Integer

City.SetFocus

If City.Text = "" Then
City.Value = "Boston"
Else
ans = MsgBox("Do you want to replace the current Address information?", _
vbYesNo, "Replace Address?")
If ans = vbYes Then
City.Value = City.Text
'ElseIf ans = vbNo Then
'This is where I am stuck. I need the no button to put whatever text was in
'the text box prior to the user changing it. Is there a variable I can define
'to save the text box and recall it if the user clicks no?

End If
End If


State_Province.SetFocus


If State_Province.Text = "" Then
State_Province.Value = "MA"
Else
MsgBox "Do you want to replace the current Address information?", _
vbYesNo, "Replace Address?"
If vbYes Then
State_Province.Value = State_Province.Text
'ElseIf vbNo Then

End If
End If

ZIP_Postal_Code.SetFocus


If ZIP_Postal_Code.Text = "" Then
ZIP_Postal_Code.Value = "99999"
Else
MsgBox "Do you want to replace the current Address information?", _
vbYesNo, "Replace Address?"
If vbYes Then
ZIP_Postal_Code.Value = ZIP_Postal_Code.Text
'ElseIf vbNo Then

End If
End If

Country_Region.SetFocus


If Country_Region.Text = "" Then
Country_Region.Value = "USA"
Else
MsgBox "Do you want to replace the current Address information?", _
vbYesNo, "Replace Address?"
If vbYes Then
Country_Region.Value = Country_Region.Text
'ElseIf vbNo Then

End If
End If

End Sub

CreganTur
03-09-2010, 07:51 AM
Does this not do what you want?

If City.Text = "" Then
City.Value = "Boston"
Else
ans = MsgBox("Do you want to replace the current Address information?", _
vbYesNo, "Replace Address?")
If ans = vbYes Then
City.Value = City.Text
ElseIf ans = vbNo Then


If Me.Dirty Then
DoCmd.RunCommand acCmdUndo
Else
Exit Sub
End If
End If
End If

detowne
03-09-2010, 04:03 PM
Thanks for that code but I think I may have mislead you. There are other text box's on the form and if I make changes to those box's then run the code and click "no" it undoes everything on the form not just the text box that currently has the focus. I need the button to just undo the text box that has the focus not every text box on the form. Any ideas?

Here's the code for all of the text box's:

Private Sub cmdAutoCSZC_Click()
Dim ans As Integer

City.SetFocus
If City.Text = "" Then
City.Value = "Boston"
Else
ans = MsgBox("Do you want to replace the current Address information?", _
vbYesNo, "Replace Address?")
If ans = vbYes Then
City.Value = City.Text
ElseIf ans = vbNo Then

If Me.Dirty Then
DoCmd.RunCommand acCmdUndo
Else
Exit Sub
End If

End If
End If

State_Province.SetFocus
If State_Province.Text = "" Then
State_Province.Value = "MA"
Else
ans = MsgBox("Do you want to replace the current Address information?", _
vbYesNo, "Replace Address?")
If ans = vbYes Then
State_Province.Value = State_Province.Text
ElseIf ans = vbNo Then

If Me.Dirty Then
DoCmd.RunCommand acCmdUndo
Else
Exit Sub
End If

End If
End If

ZIP_Postal_Code.SetFocus
If ZIP_Postal_Code.Text = "" Then
ZIP_Postal_Code.Value = "99999"
Else
ans = MsgBox("Do you want to replace the current Address information?", _
vbYesNo, "Replace Address?")
If ans = vbYes Then
ZIP_Postal_Code.Value = ZIP_Postal_Code.Text
ElseIf ans = vbNo Then

If Me.Dirty Then
DoCmd.RunCommand acCmdUndo
Else
Exit Sub
End If

End If
End If

Country_Region.SetFocus
If Country_Region.Text = "" Then
Country_Region.Value = "USA"
Else
ans = MsgBox("Do you want to replace the current Address information?", _
vbYesNo, "Replace Address?")
If ans = vbYes Then
Country_Region.Value = Country_Region.Text
ElseIf ans = vbNo Then

If Me.Dirty Then
DoCmd.RunCommand acCmdUndo
Else
Exit Sub
End If

End If
End If


End Sub
This code will turn edits on, then when I make an edit and click the button again (which should turn edits back off) it doesn't work. I am still able to make edits. Do I have to make the button save the form so the default setting, allowing no edits, is back in effect? If so, how do I code that? Thanks again.

Here is my current code:

Private Sub cmdEdit_Click()

If Form.AllowEdits = False Then
Form.AllowEdits = True
cmdEdit.Caption = "Editing is ON"
ElseIf Form.AllowEdits = True Then
Form.AllowEdits = False
cmdEdit.Caption = "Editing is OFF"

End If
End Sub
Thank you very much for all of your help :)

CreganTur
03-10-2010, 06:07 AM
I need the button to just undo the text box that has the focus not every text box on the form. Any ideas?

The only thing I can come up with is going to take some extra work. Either remove the navigation buttosn or, if your users need them, create your own custom mavigation buttons. As a part of each button's click event, have it call a procedure after the form moves to a different record that fills some module elvel variables with the value of each text box- that will give you the default beginning values that you want.

To save changes to the current record you can use:
RunCommand acCmdSaveRecord
Right after that runs you'll need to run the procedure to fill the module level variables with the textbox values again to capture the new saved values.


This code will turn edits on, then when I make an edit and click the button again (which should turn edits back off) it doesn't work. I am still able to make edits. Do I have to make the button save the form so the default setting, allowing no edits, is back in effect? If so, how do I code that? Thanks again.

I don't know this for a fact, but I would imagine that the form won't allow you to turn Edits to False if the form is dirty- try saving the changes first and then setting it back to no edits.

detowne
03-10-2010, 12:22 PM
I don't know this for a fact, but I would imagine that the form won't allow you to turn Edits to False if the form is dirty- try saving the changes first and then setting it back to no edits.

Thank you very much this is the code that ended up working. I was able to figure it out thanks to you, thanks again.

Private Sub cmdEdit_Click()

If Form.AllowEdits = False Then
Form.AllowEdits = True
cmdEdit.Caption = "Editing is ON"
Else
If Form.AllowEdits = True Then
cmdEdit.Caption = "Editing if OFF"
If Me.Dirty Then
RunCommand acCmdSaveRecord
Form.AllowEdits = False

End If
End If

End If
End Sub

CreganTur
03-10-2010, 02:02 PM
Glad I could help :thumb

If this issue is completely solved, you could help us out by marking it as solved: click Thread tools -> Mark As Solved at the top of this thread. If not, let us know what else you need.