Link to home
Start Free TrialLog in
Avatar of KP_SoCal
KP_SoCalFlag for United States of America

asked on

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?
 
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

Open in new window

Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

wha tis the data type of fldYear?text or number?
Avatar of KP_SoCal

ASKER

Text.
ASKER CERTIFIED SOLUTION
Avatar of Rey Obrero (Capricorn1)
Rey Obrero (Capricorn1)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Give yourself a firm handshake from me.  Thank you!  That absolutely accomplishes all I needed to with this block of code.  Thanks again!!!