Jon Bredensteiner
asked on
Export data from Access to multiple Excel files using a filtered string from a drop down list on a form
Using Access 2007 in the Access 2003 format Export to Excel 2003 format with Excel 2007 installed
I have a form with a drop down field that is to be used as a filter. What I want to do is be able to select a Director's name from the drop down list, and then click a command button that exports the information to Excel. Capricon1 already created the code to export the data to multiple Excel files dependent on the Directors' names; the module is named "mod_WSAManagementVerified _ExportToM ultipleExc elFiles", and the code is located at the very bottom of https://www.experts-exchange.com/questions/22693236/Export-data-from-Access-to-multiple-Excel-files-parsed-by-a-specific-field-name-the-files-after-the-data.html
Now, I want to call the module mentioned above when the cmd button is clicked, and pass the string to the code; however, I'm guessing that the code will have to be rewritten to work like this, so I'll probably have to create a new module or something. If the drop down box is left blank (not selected) then I would like it to run the module above as it currently is; i.e. export all of the data parsed by the Directors' names into separate Excel files.
Thanks in advance for your help, Jon
Here is the code I am currently using for the filter and for the cmd button, but it currently doesn't work; and the code Capricon1 created is located at the link above :
Option Compare Database
Option Explicit
Dim strFilter As String
Private Sub cmdExport_click()
Dim rs As DAO.Recordset, sql1, ssql, db As DAO.Database, qd As DAO.QueryDef
Dim sWhere As String
sWhere = ""
Set db = DBEngine.Workspaces(0).Dat abases(0)
Set qd = db.QueryDefs("qry_DIMaster ")
If InStr(qd.sql, "Where") > 0 Then
sql1 = Left(qd.sql, InStr(qd.sql, "Where") - 1)
sWhere = Mid(qd.sql, InStr(qd.sql, "Where"))
sWhere = Left(sWhere, InStr(sWhere, ";") - 1)
Else
sql1 = Replace(qd.sql, ";", "")
End If
' Accounts for Null values returned by strFilter
If Len(strFilter) > 0 Then
If Len(sWhere) > 0 Then
sWhere = sWhere & " And " & strFilter
ssql = sql1 & " " & sWhere
Else
ssql = sql1 & " Where " & strFilter
End If
Else
ssql = Replace(qd.sql, ";", "")
End If
Set rs = CurrentDb.OpenRecordset(ss ql)
Dim xlObj As Object, xlFile As String
Dim Sheet As Object
xlFile = "C:\MyExcelFile.xls"
Set xlObj = CreateObject("Excel.Applic ation")
xlObj.Workbooks.Add
Set Sheet = xlObj.activeworkbook.Sheet s(1)
'This copies the headers
Dim iRow, iCol
iRow = 1
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
Next
'This copies just the data
Sheet.Range("A2").CopyFrom Recordset rs
xlObj.Visible = True
'xlObj.activeworkbook.Save As xlFile - Un-comment if you want to automatically save the file
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
rs.Close
Set rs = Nothing
End Sub
Private Sub Director_AfterUpdate()
strFilter = ""
If Me.Director <> "" And Not IsNull(Me.Director) Then
If strFilter = "" Then
strFilter = "[Director]= " & Chr(34) & Me.Director & Chr(34) & ""
Else
strFilter = strFilter & " and [Director]= " & Chr(34) & Me.Director & Chr(34) & ""
End If
End If
End Sub
I have a form with a drop down field that is to be used as a filter. What I want to do is be able to select a Director's name from the drop down list, and then click a command button that exports the information to Excel. Capricon1 already created the code to export the data to multiple Excel files dependent on the Directors' names; the module is named "mod_WSAManagementVerified
Now, I want to call the module mentioned above when the cmd button is clicked, and pass the string to the code; however, I'm guessing that the code will have to be rewritten to work like this, so I'll probably have to create a new module or something. If the drop down box is left blank (not selected) then I would like it to run the module above as it currently is; i.e. export all of the data parsed by the Directors' names into separate Excel files.
Thanks in advance for your help, Jon
Here is the code I am currently using for the filter and for the cmd button, but it currently doesn't work; and the code Capricon1 created is located at the link above :
Option Compare Database
Option Explicit
Dim strFilter As String
Private Sub cmdExport_click()
Dim rs As DAO.Recordset, sql1, ssql, db As DAO.Database, qd As DAO.QueryDef
Dim sWhere As String
sWhere = ""
Set db = DBEngine.Workspaces(0).Dat
Set qd = db.QueryDefs("qry_DIMaster
If InStr(qd.sql, "Where") > 0 Then
sql1 = Left(qd.sql, InStr(qd.sql, "Where") - 1)
sWhere = Mid(qd.sql, InStr(qd.sql, "Where"))
sWhere = Left(sWhere, InStr(sWhere, ";") - 1)
Else
sql1 = Replace(qd.sql, ";", "")
End If
' Accounts for Null values returned by strFilter
If Len(strFilter) > 0 Then
If Len(sWhere) > 0 Then
sWhere = sWhere & " And " & strFilter
ssql = sql1 & " " & sWhere
Else
ssql = sql1 & " Where " & strFilter
End If
Else
ssql = Replace(qd.sql, ";", "")
End If
Set rs = CurrentDb.OpenRecordset(ss
Dim xlObj As Object, xlFile As String
Dim Sheet As Object
xlFile = "C:\MyExcelFile.xls"
Set xlObj = CreateObject("Excel.Applic
xlObj.Workbooks.Add
Set Sheet = xlObj.activeworkbook.Sheet
'This copies the headers
Dim iRow, iCol
iRow = 1
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
Next
'This copies just the data
Sheet.Range("A2").CopyFrom
xlObj.Visible = True
'xlObj.activeworkbook.Save
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
rs.Close
Set rs = Nothing
End Sub
Private Sub Director_AfterUpdate()
strFilter = ""
If Me.Director <> "" And Not IsNull(Me.Director) Then
If strFilter = "" Then
strFilter = "[Director]= " & Chr(34) & Me.Director & Chr(34) & ""
Else
strFilter = strFilter & " and [Director]= " & Chr(34) & Me.Director & Chr(34) & ""
End If
End If
End Sub
ASKER
Sorry, I put Advanced when I was filling out the new question submission format, as I thought it meant I was asking for advanced help, not that I was advanced; I'm definitely not advanced with VB ;)
Ok, I just tried your suggestion, and if I filter by a Director's name it names the tab after the string selected in the filter; however, if I leave the filter blank it just returns one file with all of the information in it.
Here is the main thing I want to accomplish here:
***** If the filter is left blank I want it to run I want it to run the module named "mod_WSAManagementVerified _ExportToM ultipleExc elFiles"
Here are a couple of minor thing I would like the code to do:
*** Just as you did with the last question, I would like the file and sheet name to be named after the concatenated field in the query the data is being pulled from [FullName].[qry_WSAManagem entVerifie d_Export]
*** Also like the last question, I do not want it to return the concatenated field, as it is only there for file naming purposes.
Thanks in advance, Jon I mistakenly had the wrong query listed in my initial question for where the data is pulled; here is the correct code that works as described above:
Option Compare Database
Option Explicit
Dim strFilter As String
Private Sub cmdExport_click()
Dim rs As DAO.Recordset, sql1, ssql, db As DAO.Database, qd As DAO.QueryDef
Dim sWhere As String
sWhere = ""
Set db = DBEngine.Workspaces(0).Dat abases(0)
Set qd = db.QueryDefs("qry_WSAManag ementVerif ied_Export ")
If InStr(qd.sql, "Where") > 0 Then
sql1 = Left(qd.sql, InStr(qd.sql, "Where") - 1)
sWhere = Mid(qd.sql, InStr(qd.sql, "Where"))
sWhere = Left(sWhere, InStr(sWhere, ";") - 1)
Else
sql1 = Replace(qd.sql, ";", "")
End If
' Accounts for Null values returned by strFilter
If Len(strFilter) > 0 Then
If Len(sWhere) > 0 Then
sWhere = sWhere & " And " & strFilter
ssql = sql1 & " " & sWhere
Else
ssql = sql1 & " Where " & strFilter
End If
Else
ssql = Replace(qd.sql, ";", "")
End If
Set rs = CurrentDb.OpenRecordset(ss ql)
Dim xlObj As Object, xlFile As String
Dim Sheet As Object
xlFile = "C:\MyExcelFile.xls"
Set xlObj = CreateObject("Excel.Applic ation")
xlObj.Workbooks.Add
Set Sheet = xlObj.activeworkbook.Sheet s(1)
If Not IsNull(Me.Director) Then Sheet.Name = Me.Director '<< add this line
'This copies the headers
Dim iRow, iCol
iRow = 1
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
Next
'This copies just the data
Sheet.Range("A2").CopyFrom Recordset rs
xlObj.Visible = True
'xlObj.activeworkbook.Save As xlFile - Un-comment if you want to automatically save the file
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
rs.Close
Set rs = Nothing
End Sub
Ok, I just tried your suggestion, and if I filter by a Director's name it names the tab after the string selected in the filter; however, if I leave the filter blank it just returns one file with all of the information in it.
Here is the main thing I want to accomplish here:
***** If the filter is left blank I want it to run I want it to run the module named "mod_WSAManagementVerified
Here are a couple of minor thing I would like the code to do:
*** Just as you did with the last question, I would like the file and sheet name to be named after the concatenated field in the query the data is being pulled from [FullName].[qry_WSAManagem
*** Also like the last question, I do not want it to return the concatenated field, as it is only there for file naming purposes.
Thanks in advance, Jon I mistakenly had the wrong query listed in my initial question for where the data is pulled; here is the correct code that works as described above:
Option Compare Database
Option Explicit
Dim strFilter As String
Private Sub cmdExport_click()
Dim rs As DAO.Recordset, sql1, ssql, db As DAO.Database, qd As DAO.QueryDef
Dim sWhere As String
sWhere = ""
Set db = DBEngine.Workspaces(0).Dat
Set qd = db.QueryDefs("qry_WSAManag
If InStr(qd.sql, "Where") > 0 Then
sql1 = Left(qd.sql, InStr(qd.sql, "Where") - 1)
sWhere = Mid(qd.sql, InStr(qd.sql, "Where"))
sWhere = Left(sWhere, InStr(sWhere, ";") - 1)
Else
sql1 = Replace(qd.sql, ";", "")
End If
' Accounts for Null values returned by strFilter
If Len(strFilter) > 0 Then
If Len(sWhere) > 0 Then
sWhere = sWhere & " And " & strFilter
ssql = sql1 & " " & sWhere
Else
ssql = sql1 & " Where " & strFilter
End If
Else
ssql = Replace(qd.sql, ";", "")
End If
Set rs = CurrentDb.OpenRecordset(ss
Dim xlObj As Object, xlFile As String
Dim Sheet As Object
xlFile = "C:\MyExcelFile.xls"
Set xlObj = CreateObject("Excel.Applic
xlObj.Workbooks.Add
Set Sheet = xlObj.activeworkbook.Sheet
If Not IsNull(Me.Director) Then Sheet.Name = Me.Director '<< add this line
'This copies the headers
Dim iRow, iCol
iRow = 1
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
Next
'This copies just the data
Sheet.Range("A2").CopyFrom
xlObj.Visible = True
'xlObj.activeworkbook.Save
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
rs.Close
Set rs = Nothing
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great, it worked nicely when I ran it without selecting a Director's name; however, I get the following error:
Run-time error 3075 *** Syntax error (missing operator) in query expression '(((tbl_WSAManagementVerif ied.WSAFun ctionRequi red)="Yes" )) where [Director]= "Akers, Brien T" ***
and then the dubugger highlightes:
*** Set rs = CurrentDb.OpenRecordset(ss ql) ***
Below is the modified code I used; note the name of the query has changed. Thanks again for all your help, Jon
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''
Option Explicit
Dim strFilter As String
Private Sub Director_AfterUpdate()
strFilter = ""
If Me.Director <> "" And Not IsNull(Me.Director) Then
If strFilter = "" Then
strFilter = "[Director]= " & Chr(34) & Me.Director & Chr(34) & ""
Else
strFilter = strFilter & " and [Director]= " & Chr(34) & Me.Director & Chr(34) & ""
End If
End If
End Sub
Private Sub cmdExport_click()
If IsNull(Me.Director) Then
exp2XL2
Else
exp2XL
End If
End Sub
Sub exp2XL()
Dim rs As DAO.Recordset, sql1, ssql, db As DAO.Database, qd As DAO.QueryDef
Set db = DBEngine.Workspaces(0).Dat abases(0)
Set qd = db.QueryDefs("export_WSAMa nagementVe rified")
sql1 = Replace(qd.SQL, ";", "")
' Accounts for Null values returned by strFilter
If Len(strFilter) > 0 Then
ssql = sql1 & " where " & strFilter
Else
ssql = sql1
End If
Set rs = CurrentDb.OpenRecordset(ss ql)
Dim xlObj As Object, xlFile As String
Dim Sheet As Object
xlFile = "C:\Users\Jon\Work\WSA\Mon thly Verification Excel Files\Test\MyExcelFile.xls "
Set xlObj = CreateObject("Excel.Applic ation")
xlObj.Workbooks.Add
Set Sheet = xlObj.activeworkbook.workS heets(1)
Sheet.Name = Me.Director
'This copies the headers
Dim iRow, iCol
iRow = 1
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
Next
'This copies just the data
Sheet.Range("A2").CopyFrom Recordset rs
xlObj.Visible = True
'xlObj.activeworkbook.Save As xlFile - Un-comment if you want to automatically save the file
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
End Sub
Sub exp2XL2()
Dim rs As DAO.Recordset, rsDir As DAO.Recordset
Dim ssql As String, iCol
Dim xlObj As Object
Dim Sheet As Object
Set rsDir = CurrentDb.OpenRecordset("s elect distinct LastName from export_WSAManagementVerifi ed")
If rsDir.EOF Then Exit Sub
rsDir.MoveFirst
Do Until rsDir.EOF
Set xlObj = CreateObject("Excel.Applic ation")
xlObj.Workbooks.Add
ssql = "SELECT export_WSAManagementVerifi ed.EPCFunc tionID, export_WSAManagementVerifi ed.EPCFunc tion,"
ssql = ssql & " export_WSAManagementVerifi ed.Directo rBems, export_WSAManagementVerifi ed.Directo r,"
ssql = ssql & " export_WSAManagementVerifi ed.WGManag erBems, export_WSAManagementVerifi ed.WGManag er,"
ssql = ssql & " export_WSAManagementVerifi ed.WGBudge tNum, export_WSAManagementVerifi ed.NumberO fDirectRep orts,"
ssql = ssql & " export_WSAManagementVerifi ed.NumberO fWorkgroup s, export_WSAManagementVerifi ed.WGVerif ied,"
ssql = ssql & " export_WSAManagementVerifi ed.WGWSARe quired, export_WSAManagementVerifi ed.WSAFunc tionRequir ed,"
ssql = ssql & " export_WSAManagementVerifi ed.WGWSACo mpletions, export_WSAManagementVerifi ed.WSAComp letions_Da ta,"
ssql = ssql & " export_WSAManagementVerifi ed.WGWSARe quirement, export_WSAManagementVerifi ed.WSAComp letions_Ov erride,"
ssql = ssql & " export_WSAManagementVerifi ed.WGNotes "
ssql = ssql & " FROM export_WSAManagementVerifi ed"
ssql = ssql & " Where export_WSAManagementVerifi ed.LastNam e='" & rsDir("LastName") & "'"
Set rs = CurrentDb.OpenRecordset(ss ql, dbOpenDynaset)
Set Sheet = xlObj.activeworkbook.Sheet s("sheet1" )
'rename the sheet, you can use any of the recordset field
Sheet.Name = rsDir("LastName")
'copy the headers
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
Next
Sheet.Range("A2").CopyFrom Recordset rs 'copy the data
xlObj.activeworkbook.SaveA s "C:\Users\Jon\Work\WSA\Mon thly Verification Excel Files\Test\" & rsDir("lastName") & "_" & "WSAVerification.xls", FileFormat:=-4143
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
rsDir.MoveNext
Loop
rsDir.Close
rs.Close
Set rsDir = Nothing
Set rs = Nothing
End Sub
Run-time error 3075 *** Syntax error (missing operator) in query expression '(((tbl_WSAManagementVerif
and then the dubugger highlightes:
*** Set rs = CurrentDb.OpenRecordset(ss
Below is the modified code I used; note the name of the query has changed. Thanks again for all your help, Jon
''''''''''''''''''''''''''
Option Explicit
Dim strFilter As String
Private Sub Director_AfterUpdate()
strFilter = ""
If Me.Director <> "" And Not IsNull(Me.Director) Then
If strFilter = "" Then
strFilter = "[Director]= " & Chr(34) & Me.Director & Chr(34) & ""
Else
strFilter = strFilter & " and [Director]= " & Chr(34) & Me.Director & Chr(34) & ""
End If
End If
End Sub
Private Sub cmdExport_click()
If IsNull(Me.Director) Then
exp2XL2
Else
exp2XL
End If
End Sub
Sub exp2XL()
Dim rs As DAO.Recordset, sql1, ssql, db As DAO.Database, qd As DAO.QueryDef
Set db = DBEngine.Workspaces(0).Dat
Set qd = db.QueryDefs("export_WSAMa
sql1 = Replace(qd.SQL, ";", "")
' Accounts for Null values returned by strFilter
If Len(strFilter) > 0 Then
ssql = sql1 & " where " & strFilter
Else
ssql = sql1
End If
Set rs = CurrentDb.OpenRecordset(ss
Dim xlObj As Object, xlFile As String
Dim Sheet As Object
xlFile = "C:\Users\Jon\Work\WSA\Mon
Set xlObj = CreateObject("Excel.Applic
xlObj.Workbooks.Add
Set Sheet = xlObj.activeworkbook.workS
Sheet.Name = Me.Director
'This copies the headers
Dim iRow, iCol
iRow = 1
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(iRow, iCol + 1).Value = rs.Fields(iCol).Name
Next
'This copies just the data
Sheet.Range("A2").CopyFrom
xlObj.Visible = True
'xlObj.activeworkbook.Save
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
End Sub
Sub exp2XL2()
Dim rs As DAO.Recordset, rsDir As DAO.Recordset
Dim ssql As String, iCol
Dim xlObj As Object
Dim Sheet As Object
Set rsDir = CurrentDb.OpenRecordset("s
If rsDir.EOF Then Exit Sub
rsDir.MoveFirst
Do Until rsDir.EOF
Set xlObj = CreateObject("Excel.Applic
xlObj.Workbooks.Add
ssql = "SELECT export_WSAManagementVerifi
ssql = ssql & " export_WSAManagementVerifi
ssql = ssql & " export_WSAManagementVerifi
ssql = ssql & " export_WSAManagementVerifi
ssql = ssql & " export_WSAManagementVerifi
ssql = ssql & " export_WSAManagementVerifi
ssql = ssql & " export_WSAManagementVerifi
ssql = ssql & " export_WSAManagementVerifi
ssql = ssql & " export_WSAManagementVerifi
ssql = ssql & " FROM export_WSAManagementVerifi
ssql = ssql & " Where export_WSAManagementVerifi
Set rs = CurrentDb.OpenRecordset(ss
Set Sheet = xlObj.activeworkbook.Sheet
'rename the sheet, you can use any of the recordset field
Sheet.Name = rsDir("LastName")
'copy the headers
For iCol = 0 To rs.Fields.Count - 1
Sheet.cells(1, iCol + 1).Value = rs.Fields(iCol).Name
Next
Sheet.Range("A2").CopyFrom
xlObj.activeworkbook.SaveA
Set Sheet = Nothing
xlObj.Quit
Set xlObj = Nothing
rsDir.MoveNext
Loop
rsDir.Close
rs.Close
Set rsDir = Nothing
Set rs = Nothing
End Sub
ASKER
I'm guessing that you will want the SQl from the query too, so here it is:
Query Name: export_WSAManagementVerifi ed
'''''''''''''''''''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '''''''''' '
SELECT IIf(IsNull([Director]),"Un known",Lef t([Directo r],InStr([ Director], ",")-1)) AS LastName, tbl_WSAManagementVerified. WGManagerB ems, tbl_WSAManagementVerified. WGManager, tbl_WSAManagementVerified. WGBudgetNu m, tbl_WSAManagementVerified. WGVerified , tbl_WSAManagementVerified. WGWSARequi red, tbl_WSAManagementVerified. WGWSACompl etions, tbl_WSAManagementVerified. WSAComplet ions_Overr ide, tbl_WSAManagementVerified. WSAComplet ions_Data, tbl_WSAManagementVerified. WGWSARequi rement, tbl_WSAManagementVerified. WGNotes, tbl_WSAManagementVerified. NumberOfDi rectReport s, tbl_WSAManagementVerified. NumberOfWo rkgroups, tbl_WSAManagementVerified. EPCFunctio nID, tbl_WSAManagementVerified. EPCFunctio n, tbl_WSAManagementVerified. WSAFunctio nRequired, tbl_WSAManagementVerified. DirectorBe ms, tbl_WSAManagementVerified. Director
FROM tbl_WSAManagementVerified
WHERE (((tbl_WSAManagementVerifi ed.WSAFunc tionRequir ed)="Yes") );
Query Name: export_WSAManagementVerifi
''''''''''''''''''''''''''
SELECT IIf(IsNull([Director]),"Un
FROM tbl_WSAManagementVerified
WHERE (((tbl_WSAManagementVerifi
ASKER
I would also like to note that I still get the same error above even if I remove [WSAFunctionRequired] field from both the [export_WSAManagementVerif ied] query and from the VB code. So I have no idea of what it is. Thanks again, Jon
Jon,
first, i don't know where you got this line
(((tbl_WSAManagementVerifi ed.WSAFunc tionRequir ed)="Yes") ) where [Director]= "Akers, Brien T" ***
post the codes *you are using*
first, i don't know where you got this line
(((tbl_WSAManagementVerifi
post the codes *you are using*
ASKER
I don't know what the problem was, but I made a couple little adjustments today, and everything works nicely :)
Thanks again for your help, Jon
Thanks again for your help, Jon
try
Set xlObj = CreateObject("Excel.Applic
xlObj.workbooks.Add
Set Sheet = xlObj.activeworkbook.workS
If Not IsNull(Me.Director) Then Sheet.Name = Me.Director '<< add this line