Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2003

    Unanswered: TransferText using Macros

    I am trying to import a text file using a TransferText Macro, is there anyway to dynamically look for the text instead of static?


    Tranfer Type: Import Fixed Width
    Specification Name: Actual Import
    Table Name: Actual
    File Name: C:\Bills\Actual.txt

    For the File Name I need to select the text because they are emailed with different file names and I cannot replace the existing file name every time I get a new file. Thank you..

  2. #2
    Join Date
    Oct 2003
    I found this code on a website ( and modified a liitle.. Does anyone know how I can add a browse button instead of typing the path & file name?

    Private Sub Import_Click()

    Dim stInputDocName As String, strMsg As String, strInput As String, stDocName As String, stExpDocName As String

    strMsg = "Please Enter the File's name with path and extension"

    strInput = InputBox(Prompt:=strMsg, Title:="Input Data Info", XPos:=2000, YPos:=2000)
    stInputDocName = strInput
    stDocName = "Actual Import"

    DoCmd.TransferText acImportFixed, "Actual", stDocName, stInputDocName

    End Sub

  3. #3
    Join Date
    Oct 2003
    Provided Answers: 1


    You mean you need a button to be clicked to browse and choose a file name, or?
    if so hten

    just copy and past the following code as a module,

    Option Compare Database
    Option Explicit

    Public Type OPENFILENAME
    lStructSize As Long
    hwndOwner As Long
    hInstance As Long
    strFilter As String
    strCustomFilter As String
    nMaxCustFilter As Long
    nFilterIndex As Long
    strFile As String
    nMaxFile As Long
    strFileTitle As String
    nMaxFileTitle As Long
    strInitialDir As String
    strTitle As String
    flags As Long
    nFileOffset As Integer
    nFileExtension As Integer
    strDefExt As String
    lCustData As Long
    lpfnHook As Long
    lpTemplateName As String
    End Type

    Public Const OFN_ALLOWMULTISELECT = &H200
    Public Const OFN_CREATEPROMPT = &H2000

    Public Const OFN_EXPLORER = &H80000 ' new look commdlg
    Public Const OFN_FILEMUSTEXIST = &H1000
    Public Const OFN_HIDEREADONLY = &H4
    Public Const OFN_LONGNAMES = &H200000 ' force long names for 3.x modules
    Public Const OFN_NOCHANGEDIR = &H8
    Public Const OFN_NODEREFERENCELINKS = &H100000
    Public Const OFN_NOREADONLYRETURN = &H8000
    Public Const OFN_NOTESTFILECREATE = &H10000
    Public Const OFN_NOVALIDATE = &H100
    Public Const OFN_OVERWRITEPROMPT = &H2
    Public Const OFN_PATHMUSTEXIST = &H800
    Public Const OFN_READONLY = &H1
    Public Const OFN_SHAREAWARE = &H4000
    Public Const OFN_SHOWHELP = &H10

    Public Declare Function GetSaveFileName Lib "comdlg32.dll" Alias "GetSaveFileNameA" (pOpenfilename As OPENFILENAME) As Long
    Public Declare Function GetOpenFileName Lib "comdlg32.dll" Alias "GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long
    Public Declare Function GetOpenClipboardWindow Lib "user32" () As Long

    Function CommonFileOpenSave( _
    Optional ByRef flags As Variant, _
    Optional ByVal InitialDir As Variant, _
    Optional ByVal Filter As Variant, _
    Optional ByVal FilterIndex As Variant, _
    Optional ByVal DefaultExt As Variant, _
    Optional ByVal FileName As Variant, _
    Optional ByVal DialogTitle As Variant, _
    Optional ByVal hWnd As Variant, _
    Optional ByVal OpenFile As Variant) As Variant
    ' This is the entry point you'll use to call the common
    ' file open/save dialog. The parameters are listed
    ' below, and all are optional.
    ' In:
    ' Flags: one or more of the OFN_* constants, OR'd together.
    ' InitialDir: the directory in which to first look
    ' Filter: a set of file filters, set up by calling
    ' AddFilterItem. See examples.
    ' FilterIndex: 1-based integer indicating which filter
    ' set to use, by default (1 if unspecified)
    ' DefaultExt: Extension to use if the user doesn't enter one.
    ' Only useful on file saves.
    ' FileName: Default value for the file name text box.
    ' DialogTitle: Title for the dialog.
    ' hWnd: parent window handle
    ' OpenFile: Boolean(True=Open File/False=Save As)
    ' Out:
    ' Return Value: Either Null or the selected filename
    Dim strFileName As String
    Dim strFileTitle As String
    Dim fResult As Boolean
    ' Give the dialog a caption title.
    If IsMissing(InitialDir) Then InitialDir = CurDir
    If IsMissing(Filter) Then Filter = ""
    If IsMissing(FilterIndex) Then FilterIndex = 1
    If IsMissing(flags) Then flags = 0&
    If IsMissing(DefaultExt) Then DefaultExt = ""
    If IsMissing(FileName) Then FileName = ""
    If IsMissing(DialogTitle) Then DialogTitle = ""
    If IsMissing(hWnd) Then hWnd = Application.hWndAccessApp
    If IsMissing(OpenFile) Then OpenFile = True
    ' Allocate string space for the returned strings.
    strFileName = Left(FileName & String(256, 0), 256)
    strFileTitle = String(256, 0)
    ' Set up the data structure before you call the function
    With OFN
    .lStructSize = Len(OFN)
    .hwndOwner = hWnd
    .strFilter = Filter
    .nFilterIndex = FilterIndex
    .strFile = strFileName
    .nMaxFile = Len(strFileName)
    .strFileTitle = strFileTitle
    .nMaxFileTitle = Len(strFileTitle)
    .strTitle = DialogTitle
    .flags = flags
    .strDefExt = DefaultExt
    .strInitialDir = InitialDir
    ' Didn't think most people would want to deal with
    ' these options.
    .hInstance = 0
    .strCustomFilter = ""
    .nMaxCustFilter = 0
    .lpfnHook = 0
    'New for NT 4.0
    .strCustomFilter = String(255, 0)
    .nMaxCustFilter = 255
    End With
    ' This will pass the desired data structure to the
    ' Windows API, which will in turn it uses to display
    ' the Open/Save As Dialog.
    If OpenFile Then
    fResult = GetOpenFileName(OFN)
    fResult = GetSaveFileName(OFN)
    End If

    ' The function call filled in the strFileTitle member
    ' of the structure. You'll have to write special code
    ' to retrieve that if you're interested.
    If fResult Then
    ' You might care to check the Flags member of the
    ' structure to get information about the chosen file.
    ' In this example, if you bothered to pass in a
    ' value for Flags, we'll fill it in with the outgoing
    ' Flags value.
    If Not IsMissing(flags) Then flags = OFN.flags
    CommonFileOpenSave = TrimNull(OFN.strFile)
    CommonFileOpenSave = vbNullString
    End If
    End Function

    Private Function TrimNull(ByVal strItem As String) As String
    Dim intPos As Integer
    intPos = InStr(strItem, vbNullChar)
    If intPos > 0 Then
    TrimNull = Left(strItem, intPos - 1)
    TrimNull = strItem
    End If
    End Function

    Function AddFilterItem(strFilter As String, strDescription As String, Optional varItem As Variant) As String
    ' Tack a new chunk onto the file filter.
    ' That is, take the old value, stick onto it the description,
    ' (like "Databases"), a null character, the skeleton
    ' (like "*.mdb;*.mda") and a final null character.

    If IsMissing(varItem) Then varItem = "*.*"
    AddFilterItem = strFilter & strDescription & vbNullChar & varItem & vbNullChar
    End Function

    Function DBSuchen(DBName As String, Optional tabName As Variant)
    On Error Resume Next
    Dim strFilter As String
    Dim strInputFileName As String

    'strFilter = AddFilterItem(strFilter, "Access (*.mdb)", "*.MDB;*.MDE;*.MDA")
    strFilter = AddFilterItem(strFilter, "Text (*.txt)", "*.csv;*.dat")
    'strFilter = AddFilterItem(strFilter, "Access (*.xls)", "*.xls;*.xlc")
    strFilter = AddFilterItem(strFilter, "Alle Dateien (*.*)", "*.*")
    If IsMissing(tabName) Then
    strInputFileName = CommonFileOpenSave(InitialDir:=DBName, Filter:=strFilter, OpenFile:=True, DialogTitle:="Datenbank öffnen ...", _
    strInputFileName = CommonFileOpenSave(InitialDir:=DBName, Filter:=strFilter, OpenFile:=True, FileName:=tabName, DialogTitle:="Datenbank öffnen ...", _
    End If

    DBSuchen = strInputFileName
    End Function
    'Note that in order to call the Save As dialog box, you can use the same wrapper function by just setting the OpenFile option as False. For example,

    Function DBSpeichern(DBName As String)
    On Error Resume Next
    Dim strFilter As String
    Dim strSaveFileName As String

    strFilter = AddFilterItem(strFilter, "Access (*.mdb)", "*.MDB")
    strFilter = AddFilterItem(strFilter, "Alle Dateien (*.*)", "*.*")
    strSaveFileName = CommonFileOpenSave(InitialDir:=DBName, OpenFile:=False, Filter:=strFilter, DialogTitle:="Datenbank speichern ...", _
    If IsNull(strSaveFileName) Then
    DBSpeichern = ""
    DBSpeichern = strSaveFileName
    End If
    End Function


    then as a click event for your button type

    Private Sub cmd_yourButtonName_Click()
    dim DokumentPath as string
    DokumentPath =DBSuchen(CurDir & "\*.txt")
    End Sub

    Good luck and have nice time

  4. #4
    Join Date
    Oct 2003
    Thanks for the reply.. How can I incorporate the TransferText with this code?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts