[COLOR="Navy"]Public[/COLOR] [COLOR="Navy"]Function[/COLOR] InsertMachineHoursRecord() [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Boolean[/COLOR]
[COLOR="Navy"]Dim[/COLOR] SaveTime [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Date[/COLOR]
[COLOR="Navy"]Dim[/COLOR] db [COLOR="Navy"]As[/COLOR] DAO.Database
[COLOR="Navy"]Dim[/COLOR] rs [COLOR="Navy"]As[/COLOR] DAO.Recordset
[COLOR="SeaGreen"]'//Variables used to hold field values[/COLOR]
[COLOR="Navy"]Dim[/COLOR] strEmployee = Me.cboEmployee.Value
[COLOR="Navy"]Dim[/COLOR] dtmEventDate = Me.txtDate.Value
[COLOR="Navy"]Dim[/COLOR] lngEventTypeID = Me.cboType.Value
[COLOR="Navy"]Dim[/COLOR] dtmStartTime = me.txtStartTime.Value
[COLOR="Navy"]Dim[/COLOR] dtmEndTime = me.txtEndTime.Value
[COLOR="Navy"]Dim[/COLOR] strMachineNumber = Me.txtMachine.Value
[COLOR="Navy"]Dim[/COLOR] strDescription = Me.txtDescription.Value
[COLOR="SeaGreen"]'//Database Location[/COLOR]
[COLOR="Navy"]Const[/COLOR] DB_LOCATION = "C:\SomeFolder\SomeDatabase.mdb"
[COLOR="SeaGreen"]'//If errors occur the function will exit with a return value of false (insertion failed)[/COLOR]
[COLOR="Navy"]On[/COLOR] [COLOR="Navy"]Error[/COLOR] [COLOR="Navy"]GoTo[/COLOR] ErrHandler:
[COLOR="SeaGreen"]'//Get Field Values from Form and store in variables[/COLOR]
strEmployee = Me.cboEmployee.Value
dtmEventDate = Me.txtDate.Value
lngEventTypeID = Me.cboType.Value
dtmStartTime = Me.txtStartTime.Value
dtmEndTime = Me.txtEndTime.Value
strMachineNumber = Me.txtMachine.Value
strDescription = Me.txtDescription.Value
[COLOR="SeaGreen"]'//Recommended: Validate all values here before inserting into DB[/COLOR]
[COLOR="SeaGreen"]'// -- Check that data is of right type and meets business rules[/COLOR]
[COLOR="SeaGreen"]'//Table has a datecreated/datemodified timestamp for each record[/COLOR]
SaveTime = Now
[COLOR="SeaGreen"]'//Open Database[/COLOR]
[COLOR="Navy"]If[/COLOR] db [COLOR="Navy"]Is[/COLOR] [COLOR="Navy"]Nothing[/COLOR] [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]Set[/COLOR] db = DAO.Workspaces(0).OpenDatabase(DB_LOCATION)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="SeaGreen"]'//Open Table[/COLOR]
[COLOR="Navy"]If[/COLOR] rs [COLOR="Navy"]Is[/COLOR] [COLOR="Navy"]Nothing[/COLOR] [COLOR="Navy"]Then[/COLOR]
[COLOR="Navy"]Set[/COLOR] rs = db.OpenRecordset("MachineRepairHours", dbOpenDynaset)
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]If[/COLOR]
[COLOR="SeaGreen"]'//Create a new record[/COLOR]
[COLOR="Navy"]With[/COLOR] rs
.AddNew
![Employee] = strEmployee
![EventDate] = dtmEventDate
![EventTypeID] = lngEventTypeID
![StartTime] = dtmStartTime
![StopTime] = dtmStopTime
![HoursWorked] = ((dtmStopTime - dtmStartTime) * 24)
![MachineNumber] = strMachineNumber
![DateModified] = SaveTime
![Description] = strDescription
[COLOR="SeaGreen"]'//Insert Record into Database[/COLOR]
.Update
InsertMachineHoursRecord = True [COLOR="SeaGreen"]'//SUCCESSFUL INSERTION[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="SeaGreen"]'//Note that we use recordset in this example, but equally effective[/COLOR]
[COLOR="SeaGreen"]'// is to create an update query command text and simply run the update query:[/COLOR]
[COLOR="SeaGreen"]'// (INSERT INTO Table (Field1, Field2) VALUES (Value1, Value2);[/COLOR]
[COLOR="SeaGreen"]'//Make sure we have closed the database[/COLOR]
My_Exit:
rs.Close
[COLOR="Navy"]Set[/COLOR] rs = [COLOR="Navy"]Nothing[/COLOR]
db.Close
[COLOR="Navy"]Set[/COLOR] db = [COLOR="Navy"]Nothing[/COLOR]
[COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Function[/COLOR]
ErrHandler:
MsgBox Err.Description
[COLOR="Navy"]Resume[/COLOR] My_Exit
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]