Continuing a Loop when there is a false value

psulions83

Board Regular
Joined
Nov 16, 2015
Messages
126
Hi,

I am new to writing loop statements so I apologize if this is a previous topic but I could not find anything pertinent before posting.

I have a loop statement that reads from a file and loops. It brings in specified files from a location in the loop. Can I have this loop skip over a file if it does not exist in the folder? The below works as is unless I do not have one of the specified files in the folder. ie it calls for files 1-5 but i only have files 1, 2, 3 & 5.

Code:
Public Function loopTable()

    Dim strSQL As String
    Dim rs As DAO.Recordset
    
    ' Define TableFields
    Dim ImprtSpec, TblNm, FileNm As String
    
    
    strSQL = "SELECT * FROM Tbl_Data"
    Set rs = CurrentDb.OpenRecordset(strSQL)
    
    If Not rs.BOF And Not rs.EOF Then
        rs.MoveFirst
        While (Not rs.EOF)
        
            ImprtSpec = rs.Fields("ImportSpecNm") 'define the field you want to return data
            TblNm = rs.Fields("TableName")
            FileNm = rs.Fields("FileName")
            
  DoCmd.TransferText acImportDelim, ImprtSpec, TblNm, Application.CurrentProject.Path & "\" & FileNm, True
          
  Debug.Print rs.Fields("FileName")
 
Last edited:

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,214,874
Messages
6,122,036
Members
449,062
Latest member
mike575

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top