|
Question : Count work days between two dates
|
|
Hi,
Trying to run a query from Access, within Excel to count the number of work days (mon - fri) between two dates. I have a function which does this fine, but can't run the query within Excel as it calls an Access function. SO, is it possible to do this calculation using the built in functions (using the build wizard in the field), if so how?
Many thanks
|
|
Answer : Count work days between two dates
|
|
You can make it seemless if you want....and make the code run automatically when you open the excel spreadsheet. I have added in a msgbox section to ask the user if they want to update values...just to double check thats what they want to do!
Open up vba editor (ALT + F11), on the left hand side of the screen in the treeview find ThisWorkBook and double click it.
Then on the right hand side of the sceen copy and paste the following:
Private Sub Workbook_Open()
Dim cnn As ADODB.Connection Dim strProvider As String Dim strDataSource As String Dim rs As ADODB.Recordset Dim strSQL as string
If MsgBox("Do you want to update values now?", vbQuestion + vbYesNo, "AutoUpdate") = vbNo Then 'Optional section Exit Sub 'Optional section End If 'Optional section
'Setup module variables strProvider = "Provider=Microsoft.Jet.OLEDB.4.0;" strDataSource = "Data Source=\\PathTo\YourDocument\DatabaseName.mdb;" strSQL = "SELECT * FROM tblName;" ' Change this to refelct your sql string
'Create the connection. Set cnn = New ADODB.Connection cnn.Open strProvider & strDataSource Set rs = New ADODB.Recordset 'Open recordset With rsSiteID Set .ActiveConnection = cnn .Source = strSQL .LockType = adLockOptimistic .CursorType = adOpenForwardOnly .Open End With
Sheets("Sheet1").select Range("A2").CopyFromRecordset rs
'Tidy up rs.Close Set rs = Nothing cnn.Close Set cnn = Nothing
|
|
|
|