Update Macro Not Saving

cacouncill94

New member
Local time
Today, 00:46
Joined
Aug 6, 2013
Messages
1
I have been hammering out the issues of this form for about a week now and have almost come to a solution, but I have hit a brick wall. I have a large form with multiple functions, one of the functions is to edit a subform that houses a list of codes and other various pieces of data. When I click the edit button it auto fills the boxes with the selected data. The function works when I click the update button but when I click the save button it does not actually save the data. The code is:

This code populates the fields with the corresponding data to be edited. This code IS NOT the issue.

Code:
Private Sub cmdEdit_Click()
'check whether there exists data in list
If Not (Me.TableSub.Form.Recordset.EOF And Me.TableSub.Form.Recordset.BOF) Then
'Get data to text box control
With Me.TableSub.Form.Recordset
    Me.text_key = .Fields("KW")
    Me.txt_code = .Fields("Code")
    Me.combo_source = .Fields("Source")
    'Store id of student in tag of text id in case id is modified
    Me.txt_code.Tag = .Fields("Code")
    'Change caption of button add to Update
    Me.cmdAdd.Caption = "Update"
    'disable button edit
    Me.cmdEdit.Enabled = False
End With
End If
End Sub

The following code is meant to save the changes made to the code, it also serves as the primary save code for if I am adding a new record. THIS CODE IS THE ISSUE.

Code:
Private Sub cmdAdd_Click()
'when we click on button Add there are two options
'1. For insert
'2. For Update
If Me.txt_code.Tag & "" = "" Then
    'this is for insert new
    'add data to table
    CurrentDb.Execute "INSERT INTO KWTable(KW, Source, Code) " & _
        " VALUES('" & Me.text_key & "','" & Me.combo_source & "','" & _
        Me.txt_code & "')"

Else
'otherwise (Tag of txtID store the id of student to be modified)
CurrentDb.Execute "UPDATE KWTable " & _
" SET KW='" & Me.text_key & "'" & _
", Code='" & Me.txt_code & "'" & _
", Source='" & Me.combo_source & "'" & _
" WHERE KW='" & Me.text_key & "'"
End If
'clear form
cmdClear_Click
'refresh data in list on form
TableSub.Form.Requery

End Sub
 
Hello,
Why didn't you use the same SQL syntaxe UPDATE in your right code to EDIT as :

Code:
Private Sub cmdEdit_Click() 
'check whether there exists data in list 
If Not (Me.TableSub.Form.Recordset.EOF And Me.TableSub.Form.Recordset.BOF) Then 
'Get data to text box control 
'otherwise (Tag of txtID store the id of student to be modified)      
     CurrentDb.Execute "UPDATE KWTable " _
     & " SET KW='" & Me.text_key & "'" & _      ", Code='" & Me.txt_code & "'" _
     &      ", Source='" & Me.combo_source & "'" _
     & " WHERE KW='" & Me.text_key & "'"
'Change caption of button add to Update     
     Me.cmdAdd.Caption = "Update"     
'disable button edit     
     Me.cmdEdit.Enabled = False
End If 
End Sub
You have to use methods EDIT and UPDATE when you use DAO.Recordset.
I wish it helps you.
 

Users who are viewing this thread

Back
Top Bottom