Question : Importing records from external table gives duplicate records.

Greetings,
I am importing records from an external database and somehow I am getting duplicate records.  For example, If there are two records in the external data tables I end up with 4 records in the two target tables (SubMasterTemplate1, SubmasterTemplate2).  

I can't see where the records are getting queried twice.  Is there something about the file selection dialogue that is cycling through the file twice?

Thanks,
Pat
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
'select the external database that you want to append to the SubMaster
Dim dbAddFile As DAO.Database
'create a string variable to use as a test for the presence of the file to be appended.
Dim AddFile As Variant
Dim fDialog As Office.FileDialog
'construct the SQL strings for the two master templates
Dim SQL1 As String
SQL1 = "INSERT INTO SubMasterTemplate1 Select WetForm2x.*, WetForm1x.PlotID, WetForm1x.ProjectSite, WetForm1x.CityCounty, WetForm1x.SDate, WetForm1x.ApplicantOwner, WetForm1x.State, WetForm1x.Investigator, WetForm1x.Section, WetForm1x.Township, WetForm1x.Range, WetForm1x.Landform, WetForm1x.Relief, WetForm1x.SlopePct, "
SQL1 = SQL1 & "WetForm1x.SlopeDeg , WetForm1x.Subregion, WetForm1x.Lat, WetForm1x.Long, WetForm1x.Datum, WetForm1x.SoilUnit, WetForm1x.NWI, WetForm1x.TypicalClimHyd, WetForm1x.VegDist, WetForm1x.SoilDist, WetForm1x.HydDist, WetForm1x.VegProb, WetForm1x.SoilProb, WetForm1x.HydProb, WetForm1x.Normal, WetForm1x.HydVegPresent, "
SQL1 = SQL1 & "WetForm1x.HydricSoilsPresent, WetForm1x.WetHydroPresent, WetForm1x.IsWetland, WetForm1x.Photo_1, WetForm1x.Photo_1_Easting, WetForm1x.Photo_1_Northing, WetForm1x.Photo_1_Orient, WetForm1x.Photo_1_Comment, WetForm1x.Photo_2, WetForm1x.Photo_2_Easting, WetForm1x.Photo_2_Northing, WetForm1x.Photo_2_Orient, "
SQL1 = SQL1 & "WetForm1x.Photo_2_Comment, WetForm1x.Photo_3, WetForm1x.Photo_3_Easting, WetForm1x.Photo_3_Northing, WetForm1x.Photo_3_Orient, WetForm1x.Photo_3_Comment , WetForm1x.Photo_4, WetForm1x.Photo_4_Easting, WetForm1x.Photo_4_Northing, WetForm1x.Photo_4_Orient, WetForm1x.Photo_4_Comment, WetForm1x.Photo_5, "
SQL1 = SQL1 & "WetForm1x.Photo_5_Easting, WetForm1x.Photo_5_Northing, WetForm1x.Photo_5_Orient, WetForm1x.Photo_5_Comment, WetForm1x.Photo_6, WetForm1x.Photo_6_Easting, WetForm1x.Photo_6_Northing, WetForm1x.Photo_6_Orient, WetForm1x.Photo_6_Comment, WetForm1x.Map_1, WetForm1x.PageHolder, WetForm1x.AllPhotoPath "
SQL1 = SQL1 & "FROM WetForm1x INNER JOIN WetForm2x ON WetForm1x.ID1 = WetForm2x.ID2;"
Dim SQL2 As String
SQL2 = "INSERT INTO SubMasterTemplate2 select * from wetform3x inner join wetform4x on wetform3x.id3 = wetform4x.id4;"
'Now append selected files to the submaster templates
'Set up a multiselect dialog box
    ' Requires reference to Microsoft Office 11.0 Object Library.
   ' Set up the File Dialog.
   Dim dbSubMaster As DAO.Database
   Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
'This is the loop that will append each selected fie
   With fDialog
        'Allow the selection of multiple file.
        .AllowMultiSelect = True
         ' Set the title of the dialog box.
       .TITLE = "Please select one or more files"
 
      ' Clear out the current filters, and add .mdb.
      .Filters.Clear
      .Filters.Add "Access Databases", "*.MDB"
      .Filters.Add "All Files", "*.*"
 
        'Use the Show method to display the File Picker dialog box and return the user's action.
          '  If the .Show method returns True, the
      ' user picked at least one file. If the .Show method returns
      ' False, the user clicked Cancel.
        If .Show = -1 Then
            'Step through each string in the FileDialogSelectedItems collection
            For Each AddFile In .SelectedItems
                'Bring the other database over
                DoCmd.TransferDatabase acImport, "Microsoft Access", AddFile, acTable, "WetForm", "WetForm1x", False
                DoCmd.TransferDatabase acImport, "Microsoft Access", AddFile, acTable, "WetVeg", "WetForm2x", False
                DoCmd.TransferDatabase acImport, "Microsoft Access", AddFile, acTable, "WetHyd", "WetForm3x", False
                DoCmd.TransferDatabase acImport, "Microsoft Access", AddFile, acTable, "WetSoil", "WetForm4x", False
                'run the sqls
                DoCmd.RunSQL (SQL1)
                DoCmd.RunSQL (SQL2)
                'delete the temporary files
                DoCmd.DeleteObject acTable, "WetForm1x"
                DoCmd.DeleteObject acTable, "WetForm2x"
                DoCmd.DeleteObject acTable, "WetForm3x"
                DoCmd.DeleteObject acTable, "WetForm4x"
            Next
        Else
            MsgBox "You clicked Cancel in the file dialog box."
        End If
   End With
'end of adding the remote records to the SubMaster
Open in New Window Select All

Answer : Importing records from external table gives duplicate records.

that is what i have in mind too.

when you run the codes by clicking cmdAppendMaster, the code will halt at the first Stop.

inspect your table at this point.

you can continue running the codes by clicking on the Continue button from the toolbar on the VBA code window, the codes will halt again on the second Stop

Random Solutions  
 
programming4us programming4us