Back again,
The issue still persists. follow is most of the code, if your game to
have a browse through and see if you can spot where i am going wrong.
NB: im still picking my way through access and vba, so my coding may be
rough round edges *wink* i take criticism well however, so feel free to
point out anywhere else im going wrong, or if there is a better more
efficient way to code anything i have here.
thanks again, and Enjoy:
Private Sub pbExport_Click()
On Error GoTo ERRORHandler
' ----------------------------------------- '
' Export Here '
' ----------------------------------------- '
Dim mySheet As Object
Dim currentValue As Variant
Dim CompareValue As Variant
Dim xlApp As Object
Dim xlWb As Object
Dim db As DAO.Database
Dim strWhere As String
Dim RsSql As String
Dim Rs As DAO.Recordset
Dim j As Integer
Dim i As Integer
Dim bValidate As Boolean
' validate
Call Validate(bValidate)
If bValidate = True Then
Set xlApp = CreateObject("Excel.Application")
'xlApp.workbooks.Add <- don't use this
Set xlWb = xlApp.workbooks.Add
Set mySheet = xlWb.sheets(1)
' my old code
' Set xlApp = CreateObject("Excel.Application")
' xlApp.workbooks.Add
' Set mySheet = xlApp.activeworkbook.sheets(1)
Set db = DBEngine.Workspaces(0).Databases(0)
Call CreateWhere(strWhere)
RsSql = "SELECT tblSOEResults.* FROM"
RsSql = RsSql & " (tblPerson INNER JOIN tblEqualOpportunities ON
tblPerson.SupportedPersonID=tblEqualOpportunities.SupportedPersonID) "
RsSql = RsSql & " INNER JOIN tblSOEResults ON
tblPerson.SupportedPersonID=tblSOEResults.SupportedPersonID"
RsSql = RsSql & " WHERE"
RsSql = RsSql & strWhere
Set Rs = db.OpenRecordset(RsSql, dbOpenDynaset)
j = 1
For i = 0 To Rs.Fields.count - 1
Call populateHeader(mySheet, currentValue, j, i)
Next i
j = 2
Do Until Rs.EOF
For i = 0 To Rs.Fields.count - 1
If i = 0 Then
currentValue = Rs.Fields(i).Value
If currentValue > 0 Then
currentValue = DLookup("[FirstName] & ' ' &
[MiddleName] & ' ' & [SurName]", "tblPerson", "[SupportedPersonID] = "
& currentValue & "")
mySheet.cells(j, i + 1).Value = currentValue
End If
ElseIf i = 1 Then
currentValue = Rs.Fields(i).Value
If currentValue > 0 Then
currentValue = DLookup("SOECategoryDescription",
"tblCategory", "[SOECategoryCode] = " & currentValue & "")
mySheet.cells(j, i + 1).Value = currentValue
End If
ElseIf i = 2 Then
currentValue = Rs.Fields(i).Value
If currentValue > 0 Then
currentValue = DLookup("SOEOutcomeDescription",
"tblSOEOutcome", "[SOEOutcomeCode] = '" & currentValue & "'")
mySheet.cells(j, i + 1).Value = currentValue
End If
ElseIf i = 3 Then
currentValue = Rs.Fields(i).Value
If currentValue > 0 Then
currentValue = DLookup("Description", "tblLookup",
"[Type] = 'Key' AND
Code:
= " & currentValue & "")
mySheet.cells(j, i + 1).Value = currentValue
End If
ElseIf i = 4 Then
currentValue = Rs.Fields(i).Value
If currentValue > 0 Then
currentValue = DLookup(" [tblLookup]![Description] ",
"[tblLookup]", "[Type] ='Key' AND [Code] = " & currentValue & "")
mySheet.cells(j, i + 1).Value = currentValue
End If
ElseIf i = 5 Then
currentValue = Rs.Fields(i).Value
If currentValue > 0 Then
currentValue = DLookup(" [tblLookup]![Description] ",
"[tblLookup]", "[Type] ='Key' AND [Code] = " & currentValue & "")
mySheet.cells(j, i + 1).Value = currentValue
End If
ElseIf i = 6 Then
currentValue = Rs.Fields(i).Value
If currentValue > 0 Then
currentValue = DLookup(" [tblLookup]![Description] ",
"[tblLookup]", "[Type] ='Key' AND [Code] = " & currentValue & "")
mySheet.cells(j, i + 1).Value = currentValue
End If
ElseIf i = 7 Then
currentValue = Rs.Fields(i).Value
If currentValue > 0 Then
currentValue = DLookup(" [tblLookup]![Description] ",
"[tblLookup]", "[Type] ='Key' AND [Code] = " & currentValue & "")
mySheet.cells(j, i + 1).Value = currentValue
End If
End If
Next i
Rs.MoveNext
j = j + 1
Loop
Dim strTemp As String
Set mySheet = Nothing
strTemp = "\"
xlWb.saveas Application.CurrentProject.Path & strTemp & txtFileName
xlWb.Close
DoEvents ' perhaps add this too?
Set xlWb = Nothing
xlApp.Quit
Set xlApp = Nothing
' old code
' strTemp = "\"
'
' mySheet.saveas Application.CurrentProject.Path & strTemp &
txtFileName
' xlApp.Quit
' Set mySheet = Nothing
End If
ERRORHandler:
Select Case Err.Number
Case 0
'nowt
Case 1004
MsgBox "You have cancelled the export."
Case Else
MsgBox Err.Number & "-" & Err.Description
End Select
End Sub
If you need to see any of the methods that are called, just ask, i can
post them also.
Sam