KP_SoCal
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]![txtYea r1]”.
Then when ‘Year2.xlsx’ is imported, ‘fldYear’ would be updated with value listed in “[forms]![frmName]![txtYea r2]”. Then the same suit would be followed when importing ‘Year3.xlsx”.
Any insight into this?
The value it would update to is from the form control of “[forms]![frmName]![txtYea
Then when ‘Year2.xlsx’ is imported, ‘fldYear’ would be updated with value listed in “[forms]![frmName]![txtYea
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
wha tis the data type of fldYear?text or number?
ASKER
Text.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Give yourself a firm handshake from me. Thank you! That absolutely accomplishes all I needed to with this block of code. Thanks again!!!