Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Nested Do While Loops

Posted on 2006-07-08
5
Medium Priority
?
259 Views
Last Modified: 2008-03-17
Hi,

I am trying to build an automatic function to export the data of multiple Trainees from multiple tables based on the selection on a form.

This is what I have so far:
This function cycles through the tables listed in tblTables and exports ALL data to another DB.
*********************
Function DataExportTrainees()
'Exports data for Trainee tables to Exportable DB
Dim rs As DAO.Recordset
Dim sExportPath As String
DoCmd.SetWarnings False
sExportPath = Nz([Forms]![frmMntExportData]![ImportPathName], "\\lpnrsn01\lcl_apps\data\riflemanwing\database\120241_Exportable_Blank.mdb")
If Dir$(sExportPath) = "" Then
    MsgBox "Cannot find " & sExportPath
Else
        Set rs = CurrentDb.OpenRecordset("SELECT tblTables.* FROM tblTables WHERE (((tblTables.Export)=True));")
        Do While Not rs.EOF = True
            sSql = "INSERT INTO [" & rs!tbl & "]  IN '" & sExportPath & "' SELECT * FROM [" & rs!tbl & "] "
            DoCmd.RunSQL sSql
            rs.MoveNext
        Loop
End If
DoCmd.SetWarnings True
MsgBox "Data Transfer Complete"
End Function
*********************

I want to be able to export the data for only selected trainees. Every table has field EID which is the employeeID number. I want to be able to transfer the data where
tblPersDetailsTrainee.EID = [Forms]![frmMntExportData]![SessionID]

I basically need to amend the above function to only transfer data for the EID's captured in the above recordset

Thanks in advance. Kev
0
Comment
Question by:Kev
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17064404
Like this:

'Exports data for Trainee tables to Exportable DB
Dim rs As DAO.Recordset
Dim sExportPath As String
DoCmd.SetWarnings False
sExportPath = Nz([Forms]![frmMntExportData]![ImportPathName], "\\lpnrsn01\lcl_apps\data\riflemanwing\database\120241_Exportable_Blank.mdb")
If Dir$(sExportPath) = "" Then
    MsgBox "Cannot find " & sExportPath
Else
        Set rs = CurrentDb.OpenRecordset("SELECT tblTables.* FROM tblTables WHERE (((tblTables.Export)=True));")
        Do While Not rs.EOF = True
            sSql = "INSERT INTO [" & rs!tbl & "]  IN '" & sExportPath & "' SELECT * FROM [" & rs!tbl & "] where EID = [Forms]![frmMntExportData]![SessionID] "
            DoCmd.RunSQL sSql
            rs.MoveNext
        Loop
End If
DoCmd.SetWarnings True
MsgBox "Data Transfer Complete"
End Function
0
 
LVL 6

Accepted Solution

by:
AHMKC1 earned 1500 total points
ID: 17064418
write your sSql as
if EID is Number format then
 sSql = "INSERT INTO [" & rs!tbl & "]  IN '" & sExportPath & "' SELECT * FROM [" & rs!tbl & "]  Where EID =" & [Forms]![frmMntExportData]![SessionID] & ""

or if EID is text format then
 sSql = "INSERT INTO [" & rs!tbl & "]  IN '" & sExportPath & "' SELECT * FROM [" & rs!tbl & "]  Where EID ='" & [Forms]![frmMntExportData]![SessionID] & "'"
0
 
LVL 5

Author Comment

by:Kev
ID: 17064505
Hi,

Thanks for the help so far. I may have not explained myself clearly enough above, in fact, after looking at it I made a mistake.

I wrote
tblPersDetailsTrainee.EID = [Forms]![frmMntExportData]![SessionID]

This is incorrect. I have created a unbound combobox [Forms]![frmMntExportData]![SessionID]
I want to use this combobox to filter for all records in
tblPersDetailsTrainee where tblPersDetailsTrainee.SessionID = [Forms]![frmMntExportData]![SessionID]

This will return a list of approx 50 records, I then want to cycle through that recordset using the EID for each record as the criteria to select what records are exported in the function above.
0
 
LVL 5

Author Comment

by:Kev
ID: 17064523
Hi AHMKC1,

I have used what you have given me and adapted the function to successfully export the data from all tables for an individual trainee. Code as follows:

**********
'Exports data for Trainee tables to Exportable DB
Dim rs As DAO.Recordset
Dim sExportPath As String
DoCmd.SetWarnings False
sExportPath = Nz([Forms]![frmMntExportData]![ImportPathName], "\\lpnrsn01\lcl_apps\data\riflemanwing\database\120241_Exportable_Blank.mdb")

If Dir$(sExportPath) = "" Then
    MsgBox "Cannot find " & sExportPath
Else
        Set rs = CurrentDb.OpenRecordset("SELECT tblTables.* FROM tblTables WHERE (((tblTables.Export)=True));")
        Do While Not rs.EOF = True
             sSql = "INSERT INTO [" & rs!tbl & "]  IN '" & sExportPath & "' SELECT * FROM [" & rs!tbl & "]  Where EID ='" & [Forms]![frmPersData1]![EID] & "'"

            DoCmd.RunSQL sSql
            rs.MoveNext
        Loop
End If
DoCmd.SetWarnings True
MsgBox "Data Transfer Complete"
**********

The function was added to a cmdbtn on form frmPersData1 The problem is then it would have to be pressed for each trainee. While I would accept this as a last resort, it would be much quicker to be able to transfer the data in bulk.

Kev
0
 
LVL 6

Expert Comment

by:AHMKC1
ID: 17064953
OK you have unbound combobx 'SessionID'

Place it on form  frmPersData1 select a sessionID then click cmdbtn sSql is as follows

 sSql = "INSERT INTO [" & rs!tbl & "]  IN '" & sExportPath & "' SELECT * FROM [" & rs!tbl & "]  Where EID ='" & [Forms]![ frmPersData1]![SessionID].Value & "'"

Thanks

0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

662 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question