LetterFrack
New member
- Local time
- Yesterday, 22:47
- Joined
- Oct 22, 2004
- Messages
- 6
My database app using the following is working effectively but it is dog slow.
The app deals with assigning resources to projects.
The user selects a resource a project and the date for which the user is on the project. This is accomplished by having a drop down with a startdate and a drop down with an enddate on the form.
The code then loops through these dates , checking each date to ensure that the resource has not already been booked on the project on that date. If it has the previous record is updated.
If not the row is added.
This is really slow - can anyone advise me as to a better way of creating my code. Regards
Private Sub cmd_insert_Click()
Dim q
Dim db As DAO.Database
Dim strSQL As String
Set db = CurrentDb
Dim fdate As Date
Set db = CurrentDb()
Dim rst As Recordset
If cbo_resource.Value <> 0 And Cbo_project.Value <> 0 And cbo_role.Value <> 0 And Cbo_block <> 0 And cbo_percentage <> 0 And Cbo_startdate.Value <> "" And Cbo_enddate.Value <> "" Then
fdate = Cbo_startdate.Value
Do While fdate <= Cbo_enddate.Value
Set rst = db.OpenRecordset("Select * from resource_block where resource_id = " & cbo_resource.Value & " " _
& " and role_id = " & cbo_role.Value & " and project_id = " & Cbo_project.Value & " " _
& " and block_date >= #" & Format$(fdate, "mm/dd/yyyy") & "# ")
If rst.EOF = True Then
'insert these records which are not in this array
strSQL = "INSERT INTO Resource_Block ( Block_Date, Resource_ID, Project_Id, Role_ID, Block_ID, Percentage)" _
& " values(('" & fdate & "'),(" & cbo_resource.Value & "),(" & Cbo_project.Value & "), " _
& "(" & cbo_role.Value & "), " _
& " (" & Cbo_block.Value & "),('" & cbo_percentage.Value & "'))"
DoCmd.RunSQL strSQL
Else
strSQL = "UPDATE Resource_Block SET block_id = " & Cbo_block.Value & " , percentage='" & cbo_percentage.Value & "'" _
& " WHERE (resource_id=" & cbo_resource.Value & " and role_id = " & cbo_role.Value & " and project_id = " & Cbo_project.Value & " " _
& " and block_date = #" & Format$(fdate, "mm/dd/yyyy") & "# )"
DoCmd.RunSQL strSQL
End If
fdate = fdate + 1
Loop
Dim x
x = MsgBox("Booking Added")
Set db = Nothing
rst.Close ' When done we eliminate Recordset
Set rst = Nothing
Else
Dim qt
qt = MsgBox("You have not included all required fields", vbOKOnly)
End If
End Sub
The app deals with assigning resources to projects.
The user selects a resource a project and the date for which the user is on the project. This is accomplished by having a drop down with a startdate and a drop down with an enddate on the form.
The code then loops through these dates , checking each date to ensure that the resource has not already been booked on the project on that date. If it has the previous record is updated.
If not the row is added.
This is really slow - can anyone advise me as to a better way of creating my code. Regards
Private Sub cmd_insert_Click()
Dim q
Dim db As DAO.Database
Dim strSQL As String
Set db = CurrentDb
Dim fdate As Date
Set db = CurrentDb()
Dim rst As Recordset
If cbo_resource.Value <> 0 And Cbo_project.Value <> 0 And cbo_role.Value <> 0 And Cbo_block <> 0 And cbo_percentage <> 0 And Cbo_startdate.Value <> "" And Cbo_enddate.Value <> "" Then
fdate = Cbo_startdate.Value
Do While fdate <= Cbo_enddate.Value
Set rst = db.OpenRecordset("Select * from resource_block where resource_id = " & cbo_resource.Value & " " _
& " and role_id = " & cbo_role.Value & " and project_id = " & Cbo_project.Value & " " _
& " and block_date >= #" & Format$(fdate, "mm/dd/yyyy") & "# ")
If rst.EOF = True Then
'insert these records which are not in this array
strSQL = "INSERT INTO Resource_Block ( Block_Date, Resource_ID, Project_Id, Role_ID, Block_ID, Percentage)" _
& " values(('" & fdate & "'),(" & cbo_resource.Value & "),(" & Cbo_project.Value & "), " _
& "(" & cbo_role.Value & "), " _
& " (" & Cbo_block.Value & "),('" & cbo_percentage.Value & "'))"
DoCmd.RunSQL strSQL
Else
strSQL = "UPDATE Resource_Block SET block_id = " & Cbo_block.Value & " , percentage='" & cbo_percentage.Value & "'" _
& " WHERE (resource_id=" & cbo_resource.Value & " and role_id = " & cbo_role.Value & " and project_id = " & Cbo_project.Value & " " _
& " and block_date = #" & Format$(fdate, "mm/dd/yyyy") & "# )"
DoCmd.RunSQL strSQL
End If
fdate = fdate + 1
Loop
Dim x
x = MsgBox("Booking Added")
Set db = Nothing
rst.Close ' When done we eliminate Recordset
Set rst = Nothing
Else
Dim qt
qt = MsgBox("You have not included all required fields", vbOKOnly)
End If
End Sub