|
|
Question : When form open, creates blank file in table
|
|
Hi, I have a small database that tracks employee hours. I have a form (frmHourLog) for data entry that writes to a table (tblEmployee_Hours). If I open the form and simply close it without adding any data, a blank record is created in the table. The table has an autonumber field (EntryID). Any ideas what is going on? Thanks in advance. Here is the code for the form:
Option Compare Database
Private Sub cboCapitalizationMaintenanceOptions_BeforeUpdate(Cancel As Integer)
'Open notes forms If (Me![cboCapitalizationMaintenanceOptions] = "Production") Then DoCmd.OpenForm "frmCapitalizationProductionMaintenanceNotes", acNormal End If If (Me![cboCapitalizationMaintenanceOptions] = "Other") Then DoCmd.OpenForm "frmCapitalizationOtherMaintenanceNotes", acNormal End If If (Me![cboCapitalizationMaintenanceOptions] = "New Rolls") Then DoCmd.OpenForm "frmRollSize", acNormal End If If (Me![cboCapitalizationMaintenanceOptions] = "Roll Maintenance") Then DoCmd.OpenForm "frmRollMaintNotes", acNormal End If
End Sub
Private Sub cboEmployeeName_Click()
Me!lstEnteredHours.Requery End Sub
Private Sub cboEnterDate_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
ocxCalendar.Visible = True ocxCalendar.SetFocus If Not IsNull(cboEnterDate) Then ocxCalendar.Value = cboEnterDate.Value Else ocxCalendar.Value = Date End If
End Sub
Private Sub cboExpenseMaintenanceOptions_Change()
'Open notes forms If (Me![cboExpenseMaintenanceOptions] = "Production") Then DoCmd.OpenForm "frmExpenseProductionMaintenanceNotes", acNormal End If If (Me![cboExpenseMaintenanceOptions] = "Other") Then DoCmd.OpenForm "frmExpenseOtherMaintenanceNotes", acNormal End If End Sub
Private Sub cmbDelete_Click()
strSQL = "Delete * from tblEmployee_Hours WHERE tblEmployee_Hours.EntryID = " & lstEnteredHours.Column(6) CurrentDb.Execute strSQL Me!lstEnteredHours.Requery End Sub
Private Sub cmbEnterHour_Click() If IsNull(Me![cboEnterDate]) Then MsgBox "Please enter date." End If If IsNull(Me![cboEmployeeName]) Then MsgBox "Please enter name." End If
On Error GoTo Err_cmbEnterHour_Click If Not IsNull(Me![cboExpenseHours] And Me![cboCapitalizationHours]) Then DoCmd.GoToRecord , , acNewRec Else Me.Undo End If
Exit_cmbEnterHour_Click: Exit Sub Err_cmbEnterHour_Click: MsgBox Err.Description Resume Exit_cmbEnterHour_Click End Sub
Private Sub cmbExit_Click()
'If Me.Dirty Then 'If MsgBox("Do you want to save this record?", vbYesNo + vbQuestion, "Record has changed") = vbYes Then 'Me.Dirty = False 'save the record 'Else 'Me.Undo 'undo the record 'End If 'DoCmd.Close acForm, Me.Name 'End If DoCmd.Close End Sub
Private Sub Form_Current()
'Me!cboEnterDate = Date End Sub
Private Sub ocxCalendar_Click()
cboEnterDate.Value = ocxCalendar.Value cboEnterDate.SetFocus ocxCalendar.Visible = False End Sub
|
Answer : When form open, creates blank file in table
|
|
OK - let's cover all angles...
Delete the Exit/formclose button. (add a new standard one from the toolbox if you want to)
Add a new button to save the record.
In the Save button click event procedure you will need to use this code, not the code the wizard gives you.
SaveOK = true me.dirty = false
At the top of the module below OPtionExplicit you want a module variable: Dim SaveOK as Boolean
In the Form_beforeupdate event procedure:
if saveok = true then ' save button pressed so save the record saveok = false exit sub ' to allow the save to complete 'but if save button not pressed then task user elseIf MsgBox("Do you want to save this record?", vbYesNo + vbQuestion, "Record has changed") = vbYes Then exit sub Else Me.Undo 'undo the record End If
|
|
|
|
|