PDA

View Full Version : ADO again, just a tweak



MUSASHI
11-24-2006, 04:48 PM
Ken helped me with a version of this, but, wondered if I could make another run at it, hopefully with Ken.

I want to execute this module from a worksheet, form button, command button etc..

The temporary worksheet, is just that, it holds the recordset for a moment, then the original sheet, where it is executed from, receives the rst, ie an "update".

With column G, I want to define the status of the employees (5000 plus) as "Current" if the following conditions exist:

a- Termination Date = ""

OR

b- Rehire Date > Termination Date.

Again, with the original worksheet WSOrig

Any help is appreciated.
Thanks
Musashi

Sub GetTimEmp()
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Dim WSOrig As Worksheet
Dim WSTemp As Worksheet
Dim sSQL As String
Dim FinalRow As Long
Set WSOrig = ActiveSheet
'Build SQL String..
sSQL = "SELECT MASTER_PRM_EMPLOYEE.Employee, Employee_Name, Hire_Date, Rehire_Date, Termination_Date, Custom_Sort FROM MASTER_PRM_EMPLOYEE"

Set cnn = New ADODB.Connection
cnn.Open ("Provider=MSDASQL.1;DRIVER={Timberline Data};DBQ=J:\NEW_PSG;UID=dxxx;PWD=mxxx;")

MsgBox "You Are Updating the Employee Data"
Set rst = New ADODB.Recordset
rst.Open sSQL, cnn, adOpenDynamic
' Create the report in a new worksheet
Set WSTemp = Worksheets.Add
'Add Headings
Range("A1:G1").Value = Array("Employee", "Employee_Name", "Hire_Date", "Rehire_Date", "Termination_Date", "Custom_Sort", "Status")
'Copy From Recordset to sheet
Range("A2").CopyFromRecordset rst
'Close the connections
rst.Close
Set rst = Nothing
cnn.Close
Set cnn = Nothing
'Format the report
FinalRow = Range("A65536").End(xlUp).Row
'If No Records, then shut it down..
If FinalRow = 1 Then
Application.DisplayAlerts = False
WSTemp.Delete
Application.DisplayAlerts = True
WSOrig.Activate
MsgBox "There are no records"
Exit Sub

End If

Application.DisplayAlerts = False
WSTemp.Delete
Application.DisplayAlerts = True
'Sort the range by last name ascending....
'keeps bombing too, so I removed it.. this will be my next question....
End Sub

Bob Phillips
11-24-2006, 04:53 PM
Sorry for not being Ken.



sSQL = "SELECT Employee, Employee_Name, Hire_Date, Rehire_Date, " & _
" Termination_Date, Custom_Sort FROM MASTER_PRM_EMPLOYEE " & _
"WHERE Termination_Date = '' OR " & _
" Rehire_Date > Termination_Date"

MUSASHI
11-24-2006, 05:16 PM
thanks, I have tried to enhance the sSQL, but it keeps blowing up....cursor, lock, etc issue, it seems, so I moved on to just being able to select an adjacent range and insert a formula where there is corresponding data, have RC formula that works great to do what I want, but the code it is used in, adds a temp ws everytime the routine runs...versus inserting it into the sheet it is executed from...

thanks

XLGibbs
11-24-2006, 07:17 PM
thanks, I have tried to enhance the sSQL, but it keeps blowing up....cursor, lock, etc issue, it seems, so I moved on to just being able to select an adjacent range and insert a formula where there is corresponding data, have RC formula that works great to do what I want, but the code it is used in, adds a temp ws everytime the routine runs...versus inserting it into the sheet it is executed from...

thanks

Not sure i follow completely, but if you want the sSQL to flag the cases where Termination_Date = '' or Rehire_Date > Termination_Date you could modify the SQL

sSQL = "SELECT Employee, Employee_Name, Hire_Date, " & _
"Rehire_Date, Termination_Date, Custom_Sort. " & _
"Case When Termination_Date = '' or Rehire_Date > Termination_Date then 1 else 0 end as DateFlag" & _
" FROM MASTER_PRM_EMPLOYEE"

Ken Puls
11-24-2006, 11:32 PM
Sorry for not being Ken.

Okay, I'm sorry. I'm laughing too hard from this quote! I'll try and compose myself and come back tomorrow. :rotlaugh:

Bob Phillips
11-25-2006, 04:58 AM
Okay, I'm sorry. I'm laughing too hard from this quote! :rotlaugh:

It's not meant to be funny, I am stuffy remember!