473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Passing parameters from a Form to a Macro?

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:
Expand|Select|Wrap|Line Numbers
  1. [Forms]![frmEmployee]![txtEmployeeLastName]
And then I run a macro to output the query to an XLS file.
The Output To file is:

Expand|Select|Wrap|Line Numbers
  1. ="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!
Jan 7 '11 #1

✓ answered by ADezii

You forgot one little item, the WHERE Clause in the SQL Statement, as indicated in Code Line #18:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstEmployees As DAO.Recordset
  3. Dim qdf As DAO.QueryDef
  4. Const conPATH As String = "C:\HOLD\"
  5.  
  6. Set MyDB = CurrentDb
  7. Set rstEmployees = MyDB.OpenRecordset("SELECT tblEmployee.empLastName FROM tblEmployee", _
  8.                         dbOpenForwardOnly)
  9.  
  10. Set qdf = CurrentDb.QueryDefs("qryEmployeeSurvey")
  11.  
  12. With rstEmployees
  13.   Do While Not .EOF         'Loop through each Employee
  14.     qdf.SQL = "Select[empLastName] & ',' & [empFirstName] AS EmployeeName,  " & _
  15.               "tblEmployee.empLastName, tblDept.empUid, tblDept.deptCampus AS [Campus (Building Location)],  " & _
  16.               "tblDept.deptBin AS Bin " & _
  17.               "FROM tblDept INNER JOIN tblEmployee ON tblDept.uid = tblEmployee.uid " & _
  18.               "WHERE tblEmployee.empLastName = '" & ![empLastName] & "' " & _
  19.               "ORDER BY tblEmployee.empLastName;"
  20.  
  21.     DoCmd.OutputTo acOutputQuery, "qryEmployeeSurvey", acFormatXLS, conPATH & _
  22.                                      ![empLastName] & "_Survey.xls", False
  23.       .MoveNext
  24.   Loop
  25. End With
  26.  
  27. rstEmployees.Close
  28. Set rstEmployees = Nothing

13 2594
ADezii
8,834 Expert 8TB
Post the SQL for the Query as well as its Name.
Jan 7 '11 #2
qryEmployeeSurvey

Expand|Select|Wrap|Line Numbers
  1. SELECT tblEmployee.uid, [empFirstName] & ", " & [empLastName] AS Employee, tblEmployee.empUU1, tblEmployee.empUU2, tblEmployee.empUU3, tblEmployee.empUU4, tblEmployee.empUU5, tblEmployee.empGender
  2. FROM tblEmployee
  3. WHERE (((tblEmployee.empLastName)=[Forms]![frmEmployee]![txtLastName]));
Jan 7 '11 #3
ADezii
8,834 Expert 8TB
The General Idea is to:
  1. Create a Recordset based on all Employee Last Names from the tblEmployee Table.
  2. Loop through these Last Names, and for each Last Name, modify the SQL Property of the qryEmployeeSurvey Query.
  3. Output each Query in turn using the Current Last Name as the File Name.
  4. Code Definition:
    Expand|Select|Wrap|Line Numbers
    1. Dim MyDB As DAO.Database
    2. Dim rstEmployees As DAO.Recordset
    3. Dim qdf As DAO.QueryDef
    4.  
    5. Set MyDB = CurrentDb
    6. Set rstEmployees = MyDB.OpenRecordset("SELECT tblEmployee.empLastName FROM tblEmployee", _
    7.                         dbOpenForwardOnly)
    8.  
    9. Set qdf = CurrentDb.QueryDefs("qryEmployeeSurvey")
    10.  
    11. With rstEmployees
    12.   Do While Not .EOF         'Loop through each Employee
    13.     qdf.SQL = "SELECT tblEmployee.uid, [empFirstName] & ', ' & [empLastName] AS Employee, " & _
    14.               "tblEmployee.empUU1, tblEmployee.empUU2, tblEmployee.empUU3, tblEmployee.empUU4, " & _
    15.               "tblEmployee.empUU5, tblEmployee.empGender FROM tblEmployee " & _
    16.               "WHERE tblEmployee.empLastName = '" & ![empLastName] & "';"
    17.     'Pseudo Code ==> Output qryEmployeeSurvey to "C:\Documents and Settings\arthur\Desktop\Survey_1011\" & _
    18.                      '![empLastName] & "_Survey.xls"
    19.       .MoveNext
    20.   Loop
    21. End With
    22.  
    23. rstEmployees.Close
    24. Set rstEmployees = Nothing
Jan 7 '11 #4
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:

Expand|Select|Wrap|Line Numbers
  1. DoCmd.OutputTo ("C:\Documents and Settings\arthur\Desktop\Survey_1011\[empLastName]& '_survey.xls' & ")
Jan 10 '11 #5
ADezii
8,834 Expert 8TB
Pay particular attention to Code Lines 4, 18, and 19.
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstEmployees As DAO.Recordset
  3. Dim qdf As DAO.QueryDef
  4. Const conPATH As String = "C:\Documents and Settings\arthur\Desktop\Survey_1011\"
  5.  
  6. Set MyDB = CurrentDb
  7. Set rstEmployees = MyDB.OpenRecordset("SELECT tblEmployee.empLastName FROM tblEmployee", _
  8.                         dbOpenForwardOnly)
  9.  
  10. Set qdf = CurrentDb.QueryDefs("qryEmployeeSurvey")
  11.  
  12. With rstEmployees
  13.   Do While Not .EOF         'Loop through each Employee
  14.     qdf.SQL = "SELECT tblEmployee.uid, [empFirstName] & ', ' & [empLastName] AS Employee, " & _
  15.               "tblEmployee.empUU1, tblEmployee.empUU2, tblEmployee.empUU3, tblEmployee.empUU4, " & _
  16.               "tblEmployee.empUU5, tblEmployee.empGender FROM tblEmployee " & _
  17.               "WHERE tblEmployee.empLastName = '" & ![empLastName] & "';"
  18.       DoCmd.OutputTo acOutputQuery, "qryEmployeeSurvey", acFormatXLS, conPATH & _
  19.                                      ![empLastName] & "_Survey.xls", False
  20.       .MoveNext
  21.   Loop
  22. End With
  23.  
  24. rstEmployees.Close
  25. Set rstEmployees = Nothing
Jan 10 '11 #6
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?

Expand|Select|Wrap|Line Numbers
  1. SELECT [empLastName] & ',' & [empFirstName] AS EmployeeName, tblEmployee.empLastName, tblDept.empUid, tblDept.deptCampus AS [Campus (Building Location)], tblDept.deptBin AS Bin
  2. FROM tblDept INNER JOIN tblEmployee ON tblDept.uid = tblEmployee.uid
  3. ORDER BY tblEmployee.empLastName;
Jan 10 '11 #7
ADezii
8,834 Expert 8TB
  1. What is the Error you are getting?
  2. Try removing Parenthesis from the Expression:
  3. Instead of
    Expand|Select|Wrap|Line Numbers
    1. tblDept.deptCampus AS [Campus (Building Location)], 
  4. try:
    Expand|Select|Wrap|Line Numbers
    1. tblDept.deptCampus AS [Campus Building Location], 
Jan 10 '11 #8
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.

Jan 10 '11 #9
ADezii
8,834 Expert 8TB
The Code appears to be sound, can you Upload the DB?
Jan 10 '11 #10
sure thing.
thanks!
Attached Files
File Type: zip empDB.zip (29.4 KB, 85 views)
Jan 10 '11 #11
ADezii
8,834 Expert 8TB
You forgot one little item, the WHERE Clause in the SQL Statement, as indicated in Code Line #18:
Expand|Select|Wrap|Line Numbers
  1. Dim MyDB As DAO.Database
  2. Dim rstEmployees As DAO.Recordset
  3. Dim qdf As DAO.QueryDef
  4. Const conPATH As String = "C:\HOLD\"
  5.  
  6. Set MyDB = CurrentDb
  7. Set rstEmployees = MyDB.OpenRecordset("SELECT tblEmployee.empLastName FROM tblEmployee", _
  8.                         dbOpenForwardOnly)
  9.  
  10. Set qdf = CurrentDb.QueryDefs("qryEmployeeSurvey")
  11.  
  12. With rstEmployees
  13.   Do While Not .EOF         'Loop through each Employee
  14.     qdf.SQL = "Select[empLastName] & ',' & [empFirstName] AS EmployeeName,  " & _
  15.               "tblEmployee.empLastName, tblDept.empUid, tblDept.deptCampus AS [Campus (Building Location)],  " & _
  16.               "tblDept.deptBin AS Bin " & _
  17.               "FROM tblDept INNER JOIN tblEmployee ON tblDept.uid = tblEmployee.uid " & _
  18.               "WHERE tblEmployee.empLastName = '" & ![empLastName] & "' " & _
  19.               "ORDER BY tblEmployee.empLastName;"
  20.  
  21.     DoCmd.OutputTo acOutputQuery, "qryEmployeeSurvey", acFormatXLS, conPATH & _
  22.                                      ![empLastName] & "_Survey.xls", False
  23.       .MoveNext
  24.   Loop
  25. End With
  26.  
  27. rstEmployees.Close
  28. Set rstEmployees = Nothing
Jan 10 '11 #12
Bam!

That was it.
And you did say:
"Pay particular attention to Code Lines 4, 18, and 19"

Thanks for all of you help!
Jan 11 '11 #13
ADezii
8,834 Expert 8TB
You are quite welcome.
Jan 11 '11 #14

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
7
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. ...
1
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...
4
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...
0
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...
4
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" ...
4
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...
2
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...
4
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...
6
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...
0
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,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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$) { } ...
0
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...
1
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...
0
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...
0
marktang
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,...
0
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...
0
Oralloy
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,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.