Import Validation using Transfertext

tsmithjr

Registered User.
Local time
Yesterday, 23:36
Joined
Apr 25, 2017
Messages
12
I'm stumbling my way through creating a basic import procedure in VB using the Transfertext process. I have the code working to browse to the text file and import successfully but I have no validation in making sure it goes in successfully or error if it fails. Ideally I would parse through the file but for now I would like to create at least a way to pop up a message if it can't import and one if it successfully imports.

My Browse Button
Code:
Option Compare Database

Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long

Private Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    lpstrFilter As String
    lpstrCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    lpstrFile As String
    nMaxFile As Long
    lpstrFileTitle As String
    nMaxFileTitle As Long
    lpstrInitialDir As String
    lpstrTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    lpstrDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
End Type

Function LaunchCD(strform As Form) As String
    Dim OpenFile As OPENFILENAME
    Dim lReturn As Long
    Dim sFilter As String
    OpenFile.lStructSize = Len(OpenFile)
    OpenFile.hwndOwner = strform.Hwnd
    sFilter = "All Files (*.*)" & Chr(0) & "*.*"
    OpenFile.lpstrFilter = sFilter
    OpenFile.nFilterIndex = 1
    OpenFile.lpstrFile = String(257, 0)
    OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
    OpenFile.lpstrFileTitle = OpenFile.lpstrFile
    OpenFile.nMaxFileTitle = OpenFile.nMaxFile
    OpenFile.lpstrInitialDir = "V:\_Records\Notary\DataFiles"
    OpenFile.lpstrTitle = "Please browse to a file"
    OpenFile.flags = 0
    lReturn = GetOpenFileName(OpenFile)
        If lReturn = 0 Then
            MsgBox "A file was not selected!", vbInformation, _
              "Please browse to a file"
         Else
            LaunchCD = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
         End If
End Function

My Import Button
Code:
Private Sub btn_importO_Click()
If InStr(1, Me.filename, "erieo", vbTextCompare) Then
        DoCmd.TransferText TransferType:=acImportFixed, SpecificationName:="NotaryImportO", TableName:="dbo_Notary_test", filename:=Me.filename, HasFieldNames:=False
     Else
        MsgBox "Please make sure you chose the erieo.txt file to import Originals"
    End If
     Me.filename = ""
End If
End Sub

Thank you
 
what about an error trap:

Code:
Private Sub btn_importO_Click()

[B]on error goto errImport[/B]

If InStr(1, Me.filename, "erieo", vbTextCompare) Then
        DoCmd.TransferText TransferType:=acImportFixed, SpecificationName:="NotaryImportO", TableName:="dbo_Notary_test", filename:=Me.filename, HasFieldNames:=False
     Else
        MsgBox "Please make sure you chose the erieo.txt file to import Originals"
    End If
     Me.filename = ""
End If

[B]exit sub
errImport:
msgbox err.description,,err[/B]
End Sub
 
Ranman256,
That actually works perfect for now.

Where could I add an message if no error and import is successful?

Thank You
 

Users who are viewing this thread

Back
Top Bottom