![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I searched a few days to come up with all these answers, so I'd like to share my knowledge. I wanted to export to excel using multiple criteria based on either region number or county name (text) using a list box to select which. My goal was to not embed any plain text sql. For some reason the query def wouldn't work for me, maybe because I was using - In([qryCriteria]). Instead I set up two single field tables (jtCounty, jtRegion). These are emptied then updated from the list box selections. Two make table queries are then used - one for region, one for county since the fields are different types (number, text). The table (jtCounty or jtRegion) is joined to the main table where I'm getting all my info. Procedure - 1. Select list rowsource to populate lstMain. I used two buttons: cmdByRegion, cmdByCounty. 2. Select items from lstMain. 3. Add to criteria list (lstMainSelected) with the cmdAddToSelected or clear both lists with cmdClearSelected. 4. Finally, use cmdExportExcel to create the spreadsheet. The make table queries create a table called tblExcelExport. I use Ken Getz's module for the Save As dialog box: http://www.mvps.org/access/api/api0001.htm ----------------------------------------------------------------------- Option Compare Database Public strFilterBy As String Private Sub cmdAddToSelected_Click() Dim i As Integer Dim strCriteria As String EmptyTable Dim db As DAO.Database Dim rst As DAO.Recordset Set db = CurrentDb If Me!lstMain.RowSource = "qLkpRegion" Then Set rst = db.OpenRecordset("jtRegion") Else Set rst = db.OpenRecordset("jtCounty") End If Set lst1 = Me!lstMain Set lst2 = Me!lstMainSelected lst2.RowSource = "" With lst1 For i = 0 To .ListCount - 1 If (.Selected(i)) <> 0 Then lst2.AddItem .ItemData(i) rst.AddNew rst!field1 = .ItemData(i) rst.Update End If Next End With End Sub Private Sub cmdClearSelected_Click() Dim varItem As Variant Me!lstMainSelected.RowSource = "" For Each varItem In Me!lstMain.ItemsSelected Me!lstMain.Selected(varItem) = False Next End Sub Private Sub cmdExportExcel_Click() On Error Resume Next Dim db As DAO.Database Dim strFilter As String Dim strInputFileName As String Set db = DBEngine(0)(0) db.Execute "DROP TABLE tblExcelExport;" If Me!lstMain.RowSource = "qLkpRegion" Then DoCmd.OpenQuery "qryExcelExport_Region" Else DoCmd.OpenQuery "qryExcelExport_County" End If strFilter = ahtAddFilterItem(strFilter, "Excel File (*.xls)", "*.xls") strInputFileName = ahtCommonFileOpenSave( _ Filter:=strFilter, _ OpenFile:=False, _ DialogTitle:="Save File As...", _ Flags:=ahtOFN_HIDEREADONLY) If Len(strInputFileName) <> 0 Then DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "tblExcelExport", strInputFileName, True End If End Sub Private Sub cmdByRegion_Click() Me!lstMain.RowSource = "qLkpRegion" End Sub Private Sub cmdByCounty_Click() Me!lstMain.RowSource = "qLkpCounty" End Sub Private Sub EmptyTable() Dim db As DAO.Database Dim strSql As String Set db = DBEngine(0)(0) If Me!lstMain.RowSource = "qLkpRegion" Then strSql = "DELETE FROM jtRegion;" Else strSql = "DELETE FROM jtCounty;" End If db.Execute strSql, dbFailOnError End Sub |
#3
| |||
| |||
|
|
Glad you got it working! One enhancement you might consider is to put jtCounty and jtRegion into a temporary database, so that you can avoid the bloating that will occur from repeated deleting and adding the tables. Tony Toews has an example athttp://www.granite.ab.ca/access/temptables.htm |
#4
| |||
| |||
|
|
Glad you got it working! One enhancement you might consider is to put jtCounty and jtRegion into a temporary database, so that you can avoid the bloating that will occur from repeated deleting and adding the tables. Tony Toews has an example athttp://www.granite.ab.ca/access/temptables.htm |
#5
| |||
| |||
|
|
Any time you delete rows from tables and repopulate them, you're going to get bloat. That's because Access doesn't actually return the space that was used unless you do a Compact and Repair of the database. How much bloat you'll get depends on how large the tables are and how frequently you perform the deletions and repopulations. |
#6
| |||
| |||
|
|
Any time you delete rows from tables and repopulate them, you're going to get bloat. That's because Access doesn't actually return the space that was used unless you do a Compact and Repair of the database. How much bloat you'll get depends on how large the tables are and how frequently you perform the deletions and repopulations. |
#7
| |||
| |||
|
|
While you say "one field tables", how many rows are they typically going to have in them? |
|
Whether you delete the table and recreate it, or delete the data from the table and append to it makes no difference from the point of view of bloating. |
#8
| |||
| |||
|
|
While you say "one field tables", how many rows are they typically going to have in them? |
|
Whether you delete the table and recreate it, or delete the data from the table and append to it makes no difference from the point of view of bloating. |
#9
| |||
| |||
|
|
On Jun 30, 2:07 pm, "Douglas J. Steele" NOSPAM_djsteele (AT) NOSPAM_gmail (DOT) com> wrote: While you say "one field tables", how many rows are they typically I originally wanted to filter with qdf.parameters but it gave errors I think because the [parameter] was wrapped with an In() clause. |
#10
| |||
| |||
|
|
adding a [Selected] column to each. Then bind continuous subforms to each table (instead of using listboxes), and allow the user to make the selections that get saved in the tables. In the export query, filter on the [Selected] records. At the end, run update queries to clear the selections (and/or clear the selections at the beginning of the process as well). |

|
Now that I think of it, counties and regions could be related couldn't they? Perhaps a cascading selection scheme is called for: i.e., the user selects a region and is presented with the list of counties within that region from which to select. Does your database design support this? |

![]() |
| Thread Tools | |
| Display Modes | |
| |