Hey everyone. I just started learning in Access (came over from Excel a few weeks ago). I'm in the process of developing a database for my firm. It'll include detailed client information, user log-ins, allow users to make billing entries, etc.
I am getting stuck on this one form that I'm building. It is a very simple form that is supposed to update a job description and rate. Basically the user should be able to select the job they want to edit from a combo box, it brings up the description and rate for that job, they click edit, and they can make the change. They should also be able to add new records by clicking an "add new" button.
For the most part it is working, but I'm trying to build in fail-safes for when the users don't work it perfectly. Currently when I click on "add new" and then close the form, I'm getting the error message that "Access can't save the record at this time." Have played with using different events like form_unload or form_beforeclose but it's not working.
This is the all the code I have in this form's module currently -
Code:
Private Sub EditJobRefreshBtn_Click() 'refresh/requery manual button
Me.Refresh
Me.JobDescriptionBx.Requery
Me.JobDescriptionBx.SetFocus
'lock fields when moving to new record
Me.EditJobTxt.BackColor = RGB(245, 245, 245)
Me.EditJobTxt.Locked = True
Me.EditHourlyRateTxt.BackColor = RGB(245, 245, 245)
Me.EditHourlyRateTxt.Locked = True
End Sub
Private Sub Form_AfterInsert() 'refresh/requery after new record is added
'(so that job list is updated)
Me.Refresh
Me.JobDescriptionBx.Requery
End Sub
Private Sub Form_Current() 'runs when form is opened or goes to a different record
Me.JobDescriptionBx.SetFocus
'lock fields when moving to new record
Me.EditJobTxt.BackColor = RGB(245, 245, 245)
Me.EditJobTxt.Locked = True
Me.EditHourlyRateTxt.BackColor = RGB(245, 245, 245)
Me.EditHourlyRateTxt.Locked = True
End Sub
Private Sub Form_BeforeUpdate(Cancel As Integer) 'triggered when user changes a value
'if job description is blank and it's a new record, cancel entry (no save)
If Nz(Me.EditJobTxt, "") = "" Then
If Me.NewRecord Then
Me.Undo
Cancel = True
Exit Sub
'if job description is blank on a existing record, require description
Else
MsgBox "Job description is required. Record will not be saved.", vbExclamation, "Missing information"
Cancel = True
Me.EditJobTxt.SetFocus
Exit Sub
End If
End If
'If rate is null or blank, make it 0
If IsNull(Me!EditHourlyRateTxt) Or Me!EditHourlyRateTxt = "" Then
Me.EditHourlyRateTxt = 0
End If
End Sub
Private Sub AddJobBtn_Click() 'adds new record
'go to new record
DoCmd.GoToRecord , , acNewRec
'unlock fields
Me.EditJobTxt.BackColor = RGB(255, 255, 255)
Me.EditJobTxt.Locked = False
Me.EditHourlyRateTxt.BackColor = RGB(255, 255, 255)
Me.EditHourlyRateTxt.Locked = False
Me.EditHourlyRateTxt.Value = ""
Me.EditJobTxt.Value = ""
Me.EditJobTxt.SetFocus
End Sub
Private Sub EditJobBtn_Click() 'enables editing
Me.EditJobTxt.BackColor = RGB(255, 255, 255)
Me.EditJobTxt.Locked = False
Me.EditHourlyRateTxt.BackColor = RGB(255, 255, 255)
Me.EditHourlyRateTxt.Locked = False
Me.EditJobTxt.SetFocus
End Sub
Private Sub JobDescriptionBx_AfterUpdate() 'updates the job description and rate fields based on user's selection in combo box
If Not Me.NewRecord Then
Dim rs As DAO.Recordset
Set rs = Me.Recordset.Clone
rs.FindFirst "JobID = " & Me.JobDescriptionBx
If Not rs.NoMatch Then Me.Bookmark = rs.Bookmark
End If
Me.EditJobTxt.BackColor = RGB(245, 245, 245)
Me.EditJobTxt.Locked = True
Me.EditHourlyRateTxt.BackColor = RGB(245, 245, 245)
Me.EditHourlyRateTxt.Locked = True
End Sub
Can anyone help me figure out what's going wrong? Would really appreciate it, thank you!