Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import a file

Status
Not open for further replies.

Klo

Technical User
Aug 28, 2002
86
US
Hi All,
I have this code to import data into a table. I need to change it to check to see if the txt file exists and if yes then import but if not then exit. How do I do that?

Public Function Import_Assay48control_Data()
On Error GoTo Import_Assay48control_Data_Err
DoCmd.SetWarnings False
DoCmd.TransferText acImportFixed, "Assay48control Import Specification", "IMX Assay48 Controls Table", "c:\IMX\Data\assay48controla.txt", False, ""

DoCmd.SetWarnings True
Import_Assay48control_Data_Exit:
Exit Function

Import_Assay48control_Data_Err:
'MsgBox Error$
Resume Import_Assay48control_Data_Exit

End Function


Thanks
 
i've been using this little function. just add the function FileExists to a module.

Code:
Function FileExists(strFile As String) As Boolean
  ' Comments  : Determines if the file exists
  '             Works for hidden files and folders
  ' Parameters: strFile - file to check
  ' Returns   : True if the file exists, otherwise false
  Dim intAttr As Integer
  Dim errNum As Long
  On Error Resume Next
  'GET THE FILE ATTRIBUTE INSTEAD OF THE LENGTH OF THE FILE NAME
  intAttr = GetAttr(strFile)
  errNum = Err.Number
  FileExists = (Err.Number = 0)
End Function

to import file is it exists use this code to check.

Code:
Public Function Import_Assay48control_Data()
On Error GoTo Import_Assay48control_Data_Err

    If FileExists("c:\IMX\Data\assay48controla.txt") = True Then
        DoCmd.SetWarnings False
            DoCmd.TransferText acImportFixed, "Assay48control Import Specification", "IMX Assay48 Controls Table", "c:\IMX\Data\assay48controla.txt", False, ""
        DoCmd.SetWarnings True
    End If
    
Import_Assay48control_Data_Exit:
    Exit Function

Import_Assay48control_Data_Err:
    'MsgBox Error$
    Resume Import_Assay48control_Data_Exit

End Function
 
Thanks. That's a handy function to have.
On sort of the same (but different) thread ...
If I needed to do something based on whether a table had data would you use the same logic? For example say I had a table named "Testdata" and the first field was called "ID". If there was data in the ID field then do what follows otherwise exit. How could I do that? Does the table need to be opened first?
Thanks again.
 
this is one way to check if a table has data in it.

Code:
Private Sub Command25_Click()
On Error GoTo Err_Command24_Click

    Dim rs As DAO.Recordset, db As DAO.Database
    
        Set db = CurrentDb()
        Set rs = db.OpenRecordset("Table Name")
        rs.MoveLast
        
        If rs.RecordCount < 0 Then
            MsgBox "No Records In Table"
        Else
            MsgBox "There are " & rs.RecordCount & _
                   " records in the table"
        End If

Exit_Command24_Click:
        rs.Close
        db.Close
        DoCmd.SetWarnings True
    Exit Sub

Err_Command24_Click:
    If Err.Description = "no current record." Then
        Resume Next
    Else
        MsgBox Err.Description
        DoCmd.SetWarnings True
    Resume Exit_Command24_Click
    End If
    
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top