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

    Question Unanswered: Importing CSV into Access Using VB

    I would like to be able to have a button that when pressed imports a CSV file into my system. I know what the file will be called but dont know where the user will have placed it on their system.

    My question is how can I get the code to open a dialog box asking where the file is located as it does if you use the File Import from the menu.

    I have tried using the menu but that causes the Import wizard to start which I dont want the users touching.

    If anyone has any smart ideas I would love to hear from you.

    Thanks in advance.


  2. #2
    Join Date
    Oct 2003
    Provided Answers: 1
    Hello there,
    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
    If it doesn't function, please let me know

  3. #3
    Join Date
    Oct 2003

    Talking Works Perfectly Thank you

    Loaded it as a new Module and called it from the form.

    Worked almost first time -- Thanks a million

  4. #4
    Join Date
    Oct 2003

    Importing CSV into Access Using VB

    OK... Dang that's a lot of code. Why did Micro$oft just not include a DoCmd.OpenFileDialog Life would be so much simpler.


Posting Permissions

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