Export query result to an excel file (1 Viewer)

wop_nuno

New member
Local time
Today, 21:17
Joined
Mar 10, 2009
Messages
7
Hi everyone,

I'm new to access, so i'm sorry if i don't get things ate first time.

I have a form with a lot of fields which the user will choose which ones will be part of the "select".

The result of this select i would like to export to an excel file.
Can anyone tell me how can i do this?

But apparently DoCmd.TransferSpreadsheet doesn't work with a variable, but with tables or queries.

Can anyone help me?

My code is :

Private Sub Bt_ExecPesq_Click()
On Error GoTo Err_Bt_ExecPesq_Click

strSQL = Null
strSQL0 = Null
strSQL1 = Null
strSQL2 = Null
strSQL21 = Null
strSQL22 = Null
strSQL3 = Null
strSQL4 = Null
strSQL5 = Null
strSQL6 = Null
strSQL7 = Null
strSQL8 = Null

'Candidaturas Activas/Inactivas ou Todas
If Me.CxCandidaturasActivas.Value = -1 Then
strSQL0 = "Activo=1"
End If
'Nome do Candidato - strSQL1
If Me.CxNomeCandidato.Value = -1 Then

'Que contenha
If Me.CxNomeContenha.Value = -1 Then
If IsNull(Me.txtNomeContenha.Value) Then
MsgBox "Erro!..." & Chr(13) & "A variavel 'Que Contenha' não pode ser vazia!"
Exit Sub
End If
strSQL1 = "NomeCandidato LIKE '%" & LTrim(RTrim(Me.txtNomeContenha.Value)) & "%'"
End If
'Exactamente
If Me.CxNomeExacto.Value = -1 Then
If IsNull(Me.CxNomeExacto.Value) Then
MsgBox "Erro!..." & Chr(13) & "A variavel 'Exactamente' não pode ser vazia!"
Exit Sub
End If
strSQL1 = "ID=" & Me.CbNomeExacto.Value
End If
End If

'Habilitações Literarias - strSQL2
If Me.CxHabilitacoesLiterarias.Value = -1 Then

'Campos Cursos
If Me.CxCurso.Value = -1 Then
If IsNull(Me.CbCurso.Value) Then
MsgBox "Erro!..." & Chr(13) & "Tem que selcionar o Curso na ComboBox"
Exit Sub
End If
strSQL21 = "IDCurso1=" & Me.CbCurso.Value
strSQL22 = "IDCurso2=" & Me.CbCurso.Value

End If
'Campos Grau
If Me.CxGrau.Value = -1 Then
If IsNull(Me.CbGrau.Value) Then
MsgBox "Erro!..." & Chr(13) & "Tem que selcionar o Grua na ComboBox"
Exit Sub
End If

If IsNull(strSQL21) Then
strSQL21 = "IDGrau1=" & Me.CbGrau.Value
Else
strSQL21 = strSQL21 & " AND IDGrau1=" & Me.CbGrau.Value
End If

If IsNull(strSQL22) Then
strSQL22 = "IDGrau2=" & Me.CbGrau.Value
Else
strSQL22 = strSQL22 & " AND IDGrau2=" & Me.CbGrau.Value
End If

End If
'Campos Escolas
If Me.CxEscola.Value = -1 Then
If IsNull(Me.CbEscola.Value) Then
MsgBox "Erro!..." & Chr(13) & "Tem que selcionar o Estabelecimento de Ensino na ComboBox"
Exit Sub
End If
If IsNull(strSQL21) Then
strSQL21 = "IDEscola1=" & Me.CbEscola.Value
Else
strSQL21 = strSQL21 & " AND IDEscola1=" & Me.CbEscola.Value
End If

If IsNull(strSQL22) Then
strSQL22 = "IDEscola2=" & Me.CbEscola.Value
Else
strSQL22 = strSQL22 & " AND IDEscola2=" & Me.CbEscola.Value
End If

End If
'Ultimas Habilitações

'MsgBox Me.CxUltimasHabilitacoes.Value
If Me.CxUltimasHabilitacoes.Value = -1 Then
strSQL2 = "(" & strSQL21 & ")"

ElseIf Me.CxUltimasHabilitacoes.Value = 0 Then
'Tem que procurar nos dois conjuntos de campos de habiliataçoes

strSQL2 = "(" & strSQL21 & ") OR (" & strSQL22 & ")"
End If

End If
'Situação de Trabalho - strSQL3
If Me.CXSituacaoTrabalho.Value = -1 Then
If Me.SitEmprego.Value = 0 Then
MsgBox "Erro!..." & Chr(13) & "Tem que selcionar uma das varias Situações de Trabalho"
Exit Sub
End If
strSQL3 = "SitActualEmprego=" & Me.SitEmprego.Value
End If

'Tipo de candidatura - strSQL8
If Me.cx_tipo_cand.Value = -1 Then

If Me.tipo_candidatura.Value = 0 Then
MsgBox "Erro!..." & Chr(13) & "Tem que selecionar um dos tipos de candidatura."
Exit Sub

' caso em que a candidatura foi por "concurso"
ElseIf Me.tipo_candidatura.Value = 1 Then
strSQL8 = "Concurso=" & Me.tipo_candidatura.Value


' caso em que a candidatura foi feita de forma "expontânea"
ElseIf Me.tipo_candidatura.Value = 2 Then
' como existe uma coluna que define se foi expontanea ou não, é necessário
'fazer a pesquisa nessa coluna, mas como o valor passado do "form" é 2
'utiliza-se variável auxiliar com valor "1", para se procura na coluna referida
valor_expontanea = 1
strSQL8 = "CandidaturaExpontanea=" & valor_expontanea

End If

End If

'Carta de Condução - strSQL5
If Me.CxCartaConducao.Value = -1 Then
strSQL5 = "CartaConducao=1"
'Else
' strSQL5 = "CartaConducao=0"
End If

'Sexo - strSQL6
If Me.CxSexo.Value = -1 Then
strSQL6 = "IDSexo=" & Me.CbSexo.Value
End If

'Idade - strSQl7
If Me.CxIdade.Value = -1 Then

AnoActual = Date
auxAno = Right(AnoActual, 4)



If Me.CxIdadeAte.Value = -1 Then
auxAno = Format("31-12-" & auxAno - Me.txtIdadeAte.Value, "YYYY-MM-DD")
strSQL7 = "DataNascimento <= CONVERT(DATETIME,'" & auxAno & " 00:00:00', 102)"
End If
If Me.CxIdadeEntre.Value = -1 Then

auxAnoIni = Format("31-12-" & auxAno - 1 - Me.TxtIdadeEntreIni.Value, "YYYY-MM-DD")
auxAnoFin = Format("31-12-" & auxAno - 1 - Me.TxtIdadeEntreFin.Value, "YYYY-MM-DD")
strSQL7 = "DataNascimento Between CONVERT(DATETIME,'" & auxAnoFin & " 00:00:00', 102) AND CONVERT(DATETIME,'" & auxAnoIni & " 00:00:00', 102)"

End If
End If

'Construção da string WHERE do SQL
If Not IsNull(strSQL0) Then strSQL = "(" & strSQL0 & ")"
If Not IsNull(strSQL1) Then
If IsNull(strSQL) Then
strSQL = "(" & strSQL1 & ")"
Else
strSQL = strSQL & " AND (" & strSQL1 & ")"
End If
End If
If Not IsNull(strSQL2) Then
If IsNull(strSQL) Then
strSQL = "(" & strSQL2 & ")"
Else
strSQL = strSQL & " AND (" & strSQL2 & ")"
End If
End If
If Not IsNull(strSQL3) Then
If IsNull(strSQL) Then
strSQL = "(" & strSQL3 & ")"
Else
strSQL = strSQL & " AND (" & strSQL3 & ")"
End If
End If
'If Not IsNull(strSQL4) Then
'If IsNull(strSQL) Then
' strSQL = "(" & strSQL4 & ")"
' Else
' strSQL = strSQL & " AND (" & strSQL4 & ")"
' End If
If Not IsNull(strSQL5) Then
If IsNull(strSQL) Then
strSQL = "(" & strSQL5 & ")"
Else
strSQL = strSQL & " AND (" & strSQL5 & ")"
End If
End If
If Not IsNull(strSQL6) Then
If IsNull(strSQL) Then
strSQL = "(" & strSQL6 & ")"
Else
strSQL = strSQL & " AND (" & strSQL6 & ")"
End If
End If
If Not IsNull(strSQL7) Then
If IsNull(strSQL) Then
strSQL = "(" & strSQL7 & ")"
Else
strSQL = strSQL & " AND (" & strSQL7 & ")"
End If
End If

If Not IsNull(strSQL8) Then
If IsNull(strSQL) Then
strSQL = "(" & strSQL8 & ")"
Else
strSQL = strSQL & " AND (" & strSQL8 & ")"
End If
End If

strSQL_Rep = strSQL

strSQL = "(" & strSQL & ")"

strSQL = "SELECT ID, NomeCandidato, DataEntrada FROM FichaCandidatura WHERE " & strSQL
'MsgBox strSQL
'ME.ResulFichaCandidaturaSub.
' .RecordSource = strSQL
'.Form.RecordSource = strSQL
Me.ResulFichaCandidaturaSub.Form.RecordSource = strSQL
' = strSQL
Me.ResulFichaCandidaturaSub.Form.Requery




Const FILE_PATH As String = "C:\My documents"
Dim FULLPath As String
strFullPath = FILE_PATH

DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel7, strSQL, strFullPath & "Test.xls", False
'MsgBox ("Export Complete")





Exit_Bt_ExecPesq_Click:
Exit Sub

Err_Bt_ExecPesq_Click:
MsgBox Err.Description
Resume Exit_Bt_ExecPesq_Click





End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 17:17
Joined
Feb 19, 2002
Messages
43,408
The transferspreadsheet can't take an sql string. it should however take the name of a querydef.

PS - in the future when posting code, please make sure that it is posted as code so that it retains its formatting.
 

boblarson

Smeghead
Local time
Today, 14:17
Joined
Jan 12, 2001
Messages
32,059
PS - in the future when posting code, please make sure that it is posted as code so that it retains its formatting.

(I agree with Pat) And here's how you do that:
 

wop_nuno

New member
Local time
Today, 21:17
Joined
Mar 10, 2009
Messages
7
Thanks guys for the reply.

I'm using a Access 2003 (has an ADP) connected to SQL Server 2005.

I've tried the SQL string, query and nothing works.
 

Users who are viewing this thread

Top Bottom