Ok...let's see if I can explain this properly.
I have a form.
On the form there is a dropdown box to select employee last name.
I then have a button to show a qry, passing the employee name as the criteria: - [Forms]![frmEmployee]![txtEmployeeLastName]
And then I run a macro to output the query to an XLS file.
The Output To file is: - ="C:\Documents and Settings\arthur\Desktop\Survey_1011\" & InputBox("Enter the employee name that's in the file name:") & "_Survey.xls"
I'm trying to pass the Employee Name to the macro, so as to avoid the prompt. I have to have an employeeName_Survey.xls for each employee.
Now I know this is probably a God-Awful way of going about this and that I am making some seasoned programmers cringe, but it's all I came up with.
I initially did it manually...opened the query, sorted by employee name, then ran the outPut macro....I'm trying to streamline this a bit as I have a boatload of employees to dump this survey out for.
Thanks in advance!
You forgot one little item, the WHERE Clause in the SQL Statement, as indicated in Code Line #18: - Dim MyDB As DAO.Database
-
Dim rstEmployees As DAO.Recordset
-
Dim qdf As DAO.QueryDef
-
Const conPATH As String = "C:\HOLD\"
-
-
Set MyDB = CurrentDb
-
Set rstEmployees = MyDB.OpenRecordset("SELECT tblEmployee.empLastName FROM tblEmployee", _
-
dbOpenForwardOnly)
-
-
Set qdf = CurrentDb.QueryDefs("qryEmployeeSurvey")
-
-
With rstEmployees
-
Do While Not .EOF 'Loop through each Employee
-
qdf.SQL = "Select[empLastName] & ',' & [empFirstName] AS EmployeeName, " & _
-
"tblEmployee.empLastName, tblDept.empUid, tblDept.deptCampus AS [Campus (Building Location)], " & _
-
"tblDept.deptBin AS Bin " & _
-
"FROM tblDept INNER JOIN tblEmployee ON tblDept.uid = tblEmployee.uid " & _
-
"WHERE tblEmployee.empLastName = '" & ![empLastName] & "' " & _
-
"ORDER BY tblEmployee.empLastName;"
-
-
DoCmd.OutputTo acOutputQuery, "qryEmployeeSurvey", acFormatXLS, conPATH & _
-
![empLastName] & "_Survey.xls", False
-
.MoveNext
-
Loop
-
End With
-
-
rstEmployees.Close
-
Set rstEmployees = Nothing
13 2594
Post the SQL for the Query as well as its Name.
qryEmployeeSurvey - SELECT tblEmployee.uid, [empFirstName] & ", " & [empLastName] AS Employee, tblEmployee.empUU1, tblEmployee.empUU2, tblEmployee.empUU3, tblEmployee.empUU4, tblEmployee.empUU5, tblEmployee.empGender
-
FROM tblEmployee
-
WHERE (((tblEmployee.empLastName)=[Forms]![frmEmployee]![txtLastName]));
The General Idea is to: - Create a Recordset based on all Employee Last Names from the tblEmployee Table.
- Loop through these Last Names, and for each Last Name, modify the SQL Property of the qryEmployeeSurvey Query.
- Output each Query in turn using the Current Last Name as the File Name.
- Code Definition:
-
Dim MyDB As DAO.Database
-
Dim rstEmployees As DAO.Recordset
-
Dim qdf As DAO.QueryDef
-
-
Set MyDB = CurrentDb
-
Set rstEmployees = MyDB.OpenRecordset("SELECT tblEmployee.empLastName FROM tblEmployee", _
-
dbOpenForwardOnly)
-
-
Set qdf = CurrentDb.QueryDefs("qryEmployeeSurvey")
-
-
With rstEmployees
-
Do While Not .EOF 'Loop through each Employee
-
qdf.SQL = "SELECT tblEmployee.uid, [empFirstName] & ', ' & [empLastName] AS Employee, " & _
-
"tblEmployee.empUU1, tblEmployee.empUU2, tblEmployee.empUU3, tblEmployee.empUU4, " & _
-
"tblEmployee.empUU5, tblEmployee.empGender FROM tblEmployee " & _
-
"WHERE tblEmployee.empLastName = '" & ![empLastName] & "';"
-
'Pseudo Code ==> Output qryEmployeeSurvey to "C:\Documents and Settings\arthur\Desktop\Survey_1011\" & _
-
'![empLastName] & "_Survey.xls"
-
.MoveNext
-
Loop
-
End With
-
-
rstEmployees.Close
-
Set rstEmployees = Nothing
Thank you Adezii, I do believe this did work, but I am curious on how to make the "'Pseudo Code ==>" work though?
I am trying this, unsuccessfully: - DoCmd.OutputTo ("C:\Documents and Settings\arthur\Desktop\Survey_1011\[empLastName]& '_survey.xls' & ")
Pay particular attention to Code Lines 4, 18, and 19. - Dim MyDB As DAO.Database
-
Dim rstEmployees As DAO.Recordset
-
Dim qdf As DAO.QueryDef
-
Const conPATH As String = "C:\Documents and Settings\arthur\Desktop\Survey_1011\"
-
-
Set MyDB = CurrentDb
-
Set rstEmployees = MyDB.OpenRecordset("SELECT tblEmployee.empLastName FROM tblEmployee", _
-
dbOpenForwardOnly)
-
-
Set qdf = CurrentDb.QueryDefs("qryEmployeeSurvey")
-
-
With rstEmployees
-
Do While Not .EOF 'Loop through each Employee
-
qdf.SQL = "SELECT tblEmployee.uid, [empFirstName] & ', ' & [empLastName] AS Employee, " & _
-
"tblEmployee.empUU1, tblEmployee.empUU2, tblEmployee.empUU3, tblEmployee.empUU4, " & _
-
"tblEmployee.empUU5, tblEmployee.empGender FROM tblEmployee " & _
-
"WHERE tblEmployee.empLastName = '" & ![empLastName] & "';"
-
DoCmd.OutputTo acOutputQuery, "qryEmployeeSurvey", acFormatXLS, conPATH & _
-
![empLastName] & "_Survey.xls", False
-
.MoveNext
-
Loop
-
End With
-
-
rstEmployees.Close
-
Set rstEmployees = Nothing
Beautiful!
This did work perfectly...last problem and I will be out of your hair!
I did join two tables for the query and replaced with this code and getting error? - SELECT [empLastName] & ',' & [empFirstName] AS EmployeeName, tblEmployee.empLastName, tblDept.empUid, tblDept.deptCampus AS [Campus (Building Location)], tblDept.deptBin AS Bin
-
FROM tblDept INNER JOIN tblEmployee ON tblDept.uid = tblEmployee.uid
-
ORDER BY tblEmployee.empLastName;
- What is the Error you are getting?
- Try removing Parenthesis from the Expression:
- Instead of
- tblDept.deptCampus AS [Campus (Building Location)],
- try:
- tblDept.deptCampus AS [Campus Building Location],
That did it!
Now instead of only have the record for each person in the xls file, it's putting the records for all people in the file.
so, employee1_survey.xls contains all the data across for employee1, employee2, employee3...
employee1_survey.xls should only show data for this employee.
See screenshot.
The Code appears to be sound, can you Upload the DB?
You forgot one little item, the WHERE Clause in the SQL Statement, as indicated in Code Line #18: - Dim MyDB As DAO.Database
-
Dim rstEmployees As DAO.Recordset
-
Dim qdf As DAO.QueryDef
-
Const conPATH As String = "C:\HOLD\"
-
-
Set MyDB = CurrentDb
-
Set rstEmployees = MyDB.OpenRecordset("SELECT tblEmployee.empLastName FROM tblEmployee", _
-
dbOpenForwardOnly)
-
-
Set qdf = CurrentDb.QueryDefs("qryEmployeeSurvey")
-
-
With rstEmployees
-
Do While Not .EOF 'Loop through each Employee
-
qdf.SQL = "Select[empLastName] & ',' & [empFirstName] AS EmployeeName, " & _
-
"tblEmployee.empLastName, tblDept.empUid, tblDept.deptCampus AS [Campus (Building Location)], " & _
-
"tblDept.deptBin AS Bin " & _
-
"FROM tblDept INNER JOIN tblEmployee ON tblDept.uid = tblEmployee.uid " & _
-
"WHERE tblEmployee.empLastName = '" & ![empLastName] & "' " & _
-
"ORDER BY tblEmployee.empLastName;"
-
-
DoCmd.OutputTo acOutputQuery, "qryEmployeeSurvey", acFormatXLS, conPATH & _
-
![empLastName] & "_Survey.xls", False
-
.MoveNext
-
Loop
-
End With
-
-
rstEmployees.Close
-
Set rstEmployees = Nothing
Bam!
That was it.
And you did say:
"Pay particular attention to Code Lines 4, 18, and 19"
Thanks for all of you help!
Sign in to post your reply or Sign up for a free account.
Similar topics
by: zlatko |
last post by:
There is a form in an Access Project (.adp, Access front end with SQL
Server) for entering data into a table for temporary storing. Then, by
clicking a botton, several action stored procedures...
|
by: Pavils Jurjans |
last post by:
Hallo,
I have been programming for restricted environments where Internet Explorer
is a standard, so I haven't stumbled upon this problem until now, when I
need to write a DOM-compatible code.
...
|
by: Maria |
last post by:
Hello!
I am new to Crystal reports an I have problems passing parameters form
outside to Crystal report an creating a report with data from more
than one table
This is the problem:
I have to...
|
by: Mike Dinnis |
last post by:
Hi,
I've been working through a number of turorials to try to learn more about
retrieving data from a SQL database. I think i've mastered techniques where
i create a sql string in the page and...
|
by: Neelima Godugu |
last post by:
Hi All,
I have developed a windows forms user control, which I am going to host in
Internet Explorer..
I am familiar with the security settings requirement inorder to do the
above.
I have...
|
by: David Freeman |
last post by:
Hi There!
I'm just wondering if there's a way to pass parameters (as if you were
passing parameters to a ASCX web control) when calling an ASPX page?
e.g.
MyDetailsPage.UserName = "david"
...
|
by: MicroMoth |
last post by:
Hi,
I'm trying to write a update method, in which when the user clicks the
update button the update method is passed 10 form fields. Then a update SQL
is run to update the database.
My question...
|
by: Nab |
last post by:
I have just tried to pass parameters to a procedure in VB 2005 and realised
that you only need to pass the input parameter. The output parameter's value
will be returned without the need to pass it...
|
by: =?Utf-8?B?UGF1bA==?= |
last post by:
Hi, just wondering if anyone can provide a brief example of passing
parameters from one webpage to another, C# VS2005? I need to pass several
selected values of dropdown list boxes to a secondary...
|
by: sarahaman |
last post by:
hi all
i am new in oracle!
i made one simple form with report and i made one print button
with trigger.(when-button-pressed)
when i press the print button so its giving me whole data,i do not...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |