Hello,
I have an form (which uses unbound fields) I use to Add, Update and edit records in a table. I got the Add and delete buttons to work, however I am having problems with the Edit/Save button. Example: there is a data field which may or may not be left blank. If you are editing another field other than the date field(Hire Date) and leave the data field blank, it does not update. If I do put a date in that field it works. Here is my code below:
Private Sub cmdSave_Click()
If IsNull(Me!CboFind) Then
MsgBox "Is this a new addition? If so, use the Add button below to save this record."
Me!txtSalesnum.SetFocus
CboFind.Requery
Exit Sub
End If
'**** Save Changes ****
Dim db As DAO.Database
Dim strSQL As String
Dim cmt As Variant
Dim dte As Variant
Dim Name As String
DoCmd.SetWarnings False
Set db = CurrentDb
If Len(txtSalesnum.Value) < 1 Then
MsgBox "Please Enter the Salesburgh No."
Exit Sub
End If
If Len(txtIDNum.Value) < 1 Then
MsgBox "Please Enter the Card No."
Exit Sub
End If
If Len(txtExpDte.Value) = "" Then
MsgBox "Please Enter the Expire Date"
Exit Sub
End If
If txtHireDte.Value = "" Then
dte = ""
Else
dte = txtHireDte.Value
End If
If txtComments.Value = "" Then
cmt = " "
Else
cmt = txtComments.Value
End If
strSQL = "UPDATE tblCard_ID "
strSQL = strSQL & "SET ID_Number= '" & Me!txtIDNum & "'"
'strSQL = strSQL & ", Date= #" & Me.txtDte & "#"
strSQL = strSQL & ", Salesburgh_Card_No= " & Me!txtSalesnum & ""
strSQL = strSQL & ", Employee= '" & Me!txtname & "'"
'strSQL = strSQL & ", Employee= '" & Me!txtname & "'"
strSQL = strSQL & ", Hire_Date= '" & Me!txtHireDte & "'"
strSQL = strSQL & ", Expire_Date= '" & Me!txtExpDte & "'"
strSQL = strSQL & ", Temp_Sevice= '" & Me!CboTemp & "'"
strSQL = strSQL & ", Comments= """ & Me!txtComments & """"
strSQL = strSQL & " WHERE Salesburgh_Card_No=" & [CboFind] & ""
Debug.Print strSQL
db.Execute strSQL
Me!CboFind.Requery
strSQL = db.RecordsAffected
MsgBox txtSalesnum.Value & " Card No. Has Been Edited"
setDefaults
Set db = Nothing
txtSalesnum.SetFocus
End Sub
I had to make the date fields strings or else the Update query kept giving an error message.
Thanks for any assistance
I have an form (which uses unbound fields) I use to Add, Update and edit records in a table. I got the Add and delete buttons to work, however I am having problems with the Edit/Save button. Example: there is a data field which may or may not be left blank. If you are editing another field other than the date field(Hire Date) and leave the data field blank, it does not update. If I do put a date in that field it works. Here is my code below:
Private Sub cmdSave_Click()
If IsNull(Me!CboFind) Then
MsgBox "Is this a new addition? If so, use the Add button below to save this record."
Me!txtSalesnum.SetFocus
CboFind.Requery
Exit Sub
End If
'**** Save Changes ****
Dim db As DAO.Database
Dim strSQL As String
Dim cmt As Variant
Dim dte As Variant
Dim Name As String
DoCmd.SetWarnings False
Set db = CurrentDb
If Len(txtSalesnum.Value) < 1 Then
MsgBox "Please Enter the Salesburgh No."
Exit Sub
End If
If Len(txtIDNum.Value) < 1 Then
MsgBox "Please Enter the Card No."
Exit Sub
End If
If Len(txtExpDte.Value) = "" Then
MsgBox "Please Enter the Expire Date"
Exit Sub
End If
If txtHireDte.Value = "" Then
dte = ""
Else
dte = txtHireDte.Value
End If
If txtComments.Value = "" Then
cmt = " "
Else
cmt = txtComments.Value
End If
strSQL = "UPDATE tblCard_ID "
strSQL = strSQL & "SET ID_Number= '" & Me!txtIDNum & "'"
'strSQL = strSQL & ", Date= #" & Me.txtDte & "#"
strSQL = strSQL & ", Salesburgh_Card_No= " & Me!txtSalesnum & ""
strSQL = strSQL & ", Employee= '" & Me!txtname & "'"
'strSQL = strSQL & ", Employee= '" & Me!txtname & "'"
strSQL = strSQL & ", Hire_Date= '" & Me!txtHireDte & "'"
strSQL = strSQL & ", Expire_Date= '" & Me!txtExpDte & "'"
strSQL = strSQL & ", Temp_Sevice= '" & Me!CboTemp & "'"
strSQL = strSQL & ", Comments= """ & Me!txtComments & """"
strSQL = strSQL & " WHERE Salesburgh_Card_No=" & [CboFind] & ""
Debug.Print strSQL
db.Execute strSQL
Me!CboFind.Requery
strSQL = db.RecordsAffected
MsgBox txtSalesnum.Value & " Card No. Has Been Edited"
setDefaults
Set db = Nothing
txtSalesnum.SetFocus
End Sub
I had to make the date fields strings or else the Update query kept giving an error message.
Thanks for any assistance