Question : VBA to update field values on a table during a loop

The block of code below imports specific worksheets from 3 different Excel files into one table.  Immediately after ‘Year1.xlsx’ is imported into ‘tblMaster’, I need to update the value for each record under field name ‘fldYear’ in the target table (‘tblMaster’).  

The value it would update to is from the form control of “[forms]![frmName]![txtYear1]”.

Then when ‘Year2.xlsx’ is imported, ‘fldYear’ would be updated with value listed in “[forms]![frmName]![txtYear2]”.  Then the same suit would be followed when importing ‘Year3.xlsx”.

Any insight into this?
 
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:
Public Sub ImportExcelSheetsfIANL()
Dim objXL As Object
Dim sTable, xlPath As String, i As Integer

Dim myPath(), j, iCnt As Integer
myPath = Array("\\server\Year1.xlsx", _
               "\\server\Year2.xlsx", _
               "\\server\Year3.xlsx")
For j = LBound(myPath) To UBound(myPath)
    
If Dir(myPath(j)) <> "" Then

Set objXL = CreateObject("Excel.Application")
    objXL.Workbooks.Open myPath(j), , True
    With objXL
        For i = 1 To .Worksheets.Count
            If InStr(.Worksheets(i).Name, "widget") Then
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
             "tblMaster", myPath(j), True, .Worksheets(i).Name & "!A:M"
            End If
        Next
    End With
    objXL.Quit
    Set objXL = Nothing

    Else
    iCnt = iCnt + 1
End If
Next
If iCnt = 3 Then
MsgBox "Worksheets do not exist. Exiting sub now!", vbInformation, "Error"
Exit Sub
End If

Answer : VBA to update field values on a table during a loop

test this

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:
Public Sub ImportExcelSheetsfIANL()
Dim objXL As Object
Dim sTable, xlPath As String, i As Integer

Dim myPath(), j, iCnt As Integer
myPath = Array("\\server\Year1.xlsx", _
               "\\server\Year2.xlsx", _
               "\\server\Year3.xlsx")
For j = LBound(myPath) To UBound(myPath)
    
If Dir(myPath(j)) <> "" Then

Set objXL = CreateObject("Excel.Application")
    objXL.Workbooks.Open myPath(j), , True
    With objXL
        For i = 1 To .Worksheets.Count
            If InStr(.Worksheets(i).Name, "widget") Then
            DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, _
             "tblMaster", myPath(j), True, .Worksheets(i).Name & "!A:M"
            End If
        Next
        
    End With
    objXL.Quit
    Set objXL = Nothing
    Select Case j
        Case 0
        CurrentDb.Execute "update tblMaster set fldYear='" & [Forms]![frmName]![txtYear1] & "' where fldYear is null"
        Case 1
        CurrentDb.Execute "update tblMaster set fldYear='" & [Forms]![frmName]![txtYear2] & "' where fldYear is null"
        Case 2
        CurrentDb.Execute "update tblMaster set fldYear='" & [Forms]![frmName]![txtYear3] & "' where fldYear is null"
        
        
    End Select
    Else
    iCnt = iCnt + 1
End If
Next
If iCnt = 3 Then
MsgBox "Worksheets do not exist. Exiting sub now!", vbInformation, "Error"
Exit Sub
End If

End Sub
Random Solutions  
 
programming4us programming4us