Sunteți pe pagina 1din 3

Public Function InsertMachineHoursRecord() As Boolean Dim SaveTime As Date Dim db As DAO.Database Dim rs As DAO.

Recordset '//Variables used to hold field values Dim strEmployee = Me.cboEmployee.Value Dim dtmEventDate = Me.txtDate.Value Dim lngEventTypeID = Me.cboType.Value Dim dtmStartTime = me.txtStartTime.Value Dim dtmEndTime = me.txtEndTime.Value Dim strMachineNumber = Me.txtMachine.Value Dim strDescription = Me.txtDescription.Value '//Database Location Const DB_LOCATION = "C:\SomeFolder\SomeDatabase.mdb" '//If errors occur the function will exit with a return value of false (insertion failed) On Error GoTo ErrHandler: '//Get Field Values from Form and store in variables 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 '//Recommended: Validate all values here before inserting into DB '// -- Check that data is of right type and meets business rules '//Table has a datecreated/datemodified timestamp for each record SaveTime = Now '//Open Database If db Is Nothing Then Set db = DAO.Workspaces(0).OpenDatabase(DB_LOCATION) End If '//Open Table If rs Is Nothing Then Set rs = db.OpenRecordset("MachineRepairHours", dbOpenDynaset) End If '//Create a new record With rs .AddNew ![Employee] = strEmployee ![EventDate] = dtmEventDate ![EventTypeID] = lngEventTypeID ![StartTime] = dtmStartTime ![StopTime] = dtmStopTime ![HoursWorked] = ((dtmStopTime - dtmStartTime) * 24) ![MachineNumber] = strMachineNumber ![DateModified] = SaveTime ![Description] = strDescription '//Insert Record into Database .Update InsertMachineHoursRecord = True '//SUCCESSFUL INSERTION

End With '//Note that we use recordset in this example, but equally effective '// is to create an update query command text and simply run the update query: '// (INSERT INTO Table (Field1, Field2) VALUES (Value1, Value2); '//Make sure we have closed the database My_Exit: rs.Close Set rs = Nothing db.Close Set db = Nothing Exit Function ErrHandler: MsgBox Err.Description Resume My_Exit End Sub

Sub LogOut() 'Add a reference to the Microsoft ActiveX Data 2.8 or later Object Library 'via the Tool | References... in the VB-editor Dim cnt As ADODB.Connection Dim rst As ADODB.Recordset Dim stDB As String Dim stCon As String 'Instantiate the ADO COM's objects. Set cnt = New ADODB.Connection Set rst = New ADODB.Recordset 'Pathway and name of the database stDB = "T:\Trad\Data\db_StopLoss.mdb" 'Create the connectionstring. stCon = "Provider=Microsoft.Ace.OLEDB.12.0; Persist Security Info = False;" & _ "Data Source=" & stDB & ";" 'Open the connection cnt.Open stCon ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''' 'Find and Update the record in Access With rst .Index = "PrimaryKey" .CursorLocation = adUseServer .Open "tbl_QuoteLog", cnt, 1, 3, adCmdTableDirect .Seek Range("CaseNum").Value If Not rst.EOF Then .Fields("UndWrite") = Sheets("LogOut").Range("b9").Value

.Fields("Status") = Sheets("LogOut").Range("b11").Value If Sheets("LogOut").Range("k2") = "p" Then .Fields("QtDeadDt") = Sheets("LogOut").Range("b12").Value End If .Fields("SpecDed") = Sheets("LogOut").Range("b13").Value .Fields("EmpNum") = Sheets("LogOut").Range("b14").Value .Update Else MsgBox "Log Out Failed" End If End With ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''' 'Close the recordset and close the connection. rst.Close cnt.Close Set cnt = Nothing Set rst = Nothing End Sub

S-ar putea să vă placă și