Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Save Record 1

Status
Not open for further replies.

lars7

Technical User
Aug 16, 2005
817
GB
Hi,
I'm trying to save,or not, to a table from a textbox via a msgbox but even if I click no it still saves whatever value is in the textbox to the table. Can anyone see where i'm going wrong:

Private Sub cmd2_Click()
Dim strMsg As String, strTitle As String
Dim intStyle As Integer
CurrentDb.Execute "UPDATE tbltruststaff SET AFCCode = '" & Me!Txt2 & "' WHERE Code = '" & Me!Txt1 & "'"
' To save display a message.
strMsg = "Would you like to save this record."
intStyle = vbYesNo
strTitle = "Save"
MsgBox strMsg, intStyle, strTitle
Select Case intStyle
Case vbYes
'do nothing
Case vbNo
Me.Undo
End Select
Me.Txt1.DefaultValue = ""
Me.Txt2.DefaultValue = ""
End Sub
 
You have executed the update unconditionally. The Undo will only change what's on the screen not what's in the database.

Move the CurrentDB.Execute line into the Case vbYes section so it only gets done if the user has selected yes.

You also need to save the response from the MsgBox call and select on that:

Code:
.
.
.
Dim intResponse as integer
intResponse = MsgBox(strMsg, intStyle, strTitle)
Select Case intResponse
    Case vbYes
        CurrentDB.Execute ....
    Case vbNo
.
.
.
End Select
.
.
.




Bob Boffin
 
Hi Bob,
Sorry i'm new to this. I tried this but it stopped at:

(strMsg,

This is the Full code I tried:

Private Sub cmd2_Click()
Dim intResponse As Integer
Dim strMsg As String, strTitle As String
Dim intStyle As Integer
intResponse = MsgBox(strMsg, intStyle, strTitle)
'To save display a message.
strMsg = "Would you like to save this record."
intStyle = vbYesNo
strTitle = "Save"
MsgBox strMsg, intStyle, strTitle
Select Case intResponse
Case vbYes
CurrentDb.Execute "UPDATE tbltruststaff SET AFCCode = '" & Me!Txt2 & "' WHERE Code = '" & Me!Txt1 & "'"
Case vbNo
'Me.Undo
End Select
Me.Txt1.DefaultValue = ""
Me.Txt2.DefaultValue = ""
End Sub
 
Hi Bob,
I finally figured out what you where meaning for me to do and got it working, thanks and here it is for anyone who might need it:


Private Sub cmd2_Click()
Dim intResponce As Integer
Dim strMsg As String, strTitle As String, strstyle As String
'To save display a message.
strMsg = "Would you like to save this record."
strstyle = vbYesNo
strTitle = "Save"
'MsgBox strMsg, strstyle, strTitle
Dim intResponse As Integer
intResponse = MsgBox(strMsg, strstyle, strTitle)
Select Case intResponse
Case vbYes
CurrentDb.Execute "UPDATE tbltruststaff SET AFCCode = '" & Me!Txt2 & "' WHERE Code = '" & Me!Txt1 & "'"
Case vbNo
End Select
Me.Txt1.DefaultValue = ""
Me.Txt2.DefaultValue = ""
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top