Solved

Nested Do While Loops

Posted on 2006-07-08
5
217 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:budorat
  • 2
  • 2
5 Comments
 
LVL 142

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 500 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:budorat
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:budorat
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now