0

I have a table named OT Hours which has the following column EmpId, Date, Hours.

I need to insert a constant value in the hour column for 12 months prior to the current date for 6 employees.

Can I use a for loop in the query? If yes, Please provide me with an example. As of now, I can do it in VBA as follows:

  Dim j As Integer
  For j = -11 To 0
    DoCmd.RunSQL "INSERT INTO tblOTHours (employeeNumber, theDate, HoursType, Position, hoursQuantity) VALUES ('" & S.sanitize(txtEmployeeNumber) & "',DateAdd('m'," & j & ",Format(Now(),'mm/dd/yyyy')),'OT1','" & cmb_position.value & "'," & Round(Val(rs("Avg")) / 12, 1) & ")"
  Next

Note: I am using MS Access. Can I do this function in the query itself?

1 Answer 1

1

It can be useful to have a numbers table that holds integers from 1 or 0 to a suitably high number. Your query could take advantage of this table like so:

"INSERT INTO tblOTHours (theDate, employeeNumber, HoursType, [Position], hoursQuantity) " _
& "SELECT DateAdd('m',Number,Date()), '" & S.sanitize(txtEmployeeNumber) & "','OT1','" _
& cmb_position.value & "'," & Round(Val(rs("Avg")) / 12, 1) _
& " FROM Numbers " _
& "WHERE Numbers.Number<11"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.