Page 3 of 3 FirstFirst 123
Results 31 to 37 of 37
  1. #31
    daredan is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Jul 2022
    Posts
    44
    Alright, sorry I'm a bit slow, but I'm trying my best.

    I have now assigned 'Value List' to the row source type for the 'FileList' Listbox.
    Click image for larger version. 

Name:	2022-08-11_13-51-40.png 
Views:	25 
Size:	8.3 KB 
ID:	48505

    When I press the 'Import' button, then on a test of two files only one is imported.


    Click image for larger version. 

Name:	2022-08-11_15-12-04.png 
Views:	22 
Size:	44.9 KB 
ID:	48506

    So somehow it doesn't loop to the next file for import.

  2. #32
    daredan is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Jul 2022
    Posts
    44
    Still trying to resolve the issue. Further guidance would really be appreciated.

    Here is the current code:

    Code:
    Private Sub btnImport_Click()
    Dim fDialog As FileDialog
    Dim strFileName As String
    Dim db As DAO.Database
    Dim sqlStr As String
    Dim varFile As Variant
    
    Me.FileList.RowSource = ""
    
    On Error GoTo errHandler
    Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
    Set db = CurrentDb
    
    With fDialog
        .AllowMultiSelect = True
        .Title = "Please select one or more files"
        .Filters.Clear
        .Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx, *.xlsm, *.xlsb"
        If .Show = True Then
            For Each varFile In .SelectedItems
                Me.FileList.AddItem varFile
                strFileName = .SelectedItems(1)
                sqlStr = "INSERT INTO tblTransaction (YearMo, Entity, Acct, ActDKK, FcDKK, ActLocCur, FcLocCur, Comment) " & _
                "SELECT * FROM (SELECT YearMo, Entity, Acct, ActDKK, FcDKK, ActLocCur, FcLocCur, Replace([Comment1],Chr(10),Chr(13) & Chr(10)) " & _
                "As Comment FROM [DB$] AS xlData IN '" & strFileName & "'[Excel 12.0;HDR=yes;IMEX=2;ACCDB=Yes])"
                db.Execute sqlStr
                MsgBox strFileName & vbCrLf & db.RecordsAffected & " records imported"
            Next
            'MsgBox strFileName & vbCrLf & db.RecordsAffected & " records imported"
        Else
           MsgBox "Import selection was cancelled."
           Exit Sub
        End If
    End With
    
    exitHere:
    Set fDialog = Nothing
    Set db = Nothing
    Exit Sub
    
    errHandler:
    MsgBox "Error " & Err.Number & ": " & Err.Description
    Resume exitHere
    End Sub

    I also know that the strfilename is not updating corresponding to the entry in the listbox, but I couldn't find specific guidance helping me to adjust the current code accordingly.
    As long as I just select one file, the code works and the content is imported, however selecting multiple files ends up like shown below:
    Click image for larger version. 

Name:	2022-08-15_15-07-45.png 
Views:	18 
Size:	63.3 KB 
ID:	48516

  3. #33
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    this
    .SelectedItems(1) ensures that only 1 item will ever be used in the code that follows so the sql should repeat n times with the same file. I didn't try to figure out why the query edit only occurs the first time though - the answer is probably buried in the code or sql. Try strFileName = .SelectedItems
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  4. #34
    daredan is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Jul 2022
    Posts
    44
    Thanks Micron. I also tried playing around with this, as the 1 seemed too static for my needs.
    Code:
    strFileName = .SelectedItems
    results in
    Click image for larger version. 

Name:	2022-08-15_20-41-31.png 
Views:	15 
Size:	23.3 KB 
ID:	48517

    I tried some variations using the same three files as in post #32:

    Code:
    strFileName = .SelectedItems(1)
    results as what I described in my post #32, 3 x PHO_2206.xlsb is being triggered in the related MsgBox

    I have tried now
    Code:
    strFileName = .SelectedItems(2)
    will trigger 3 x REG_CLU_NAM_2206.xlsb

    and
    Code:
    strFileName = .SelectedItems(3)
    will trigger 3 x REG_CLU_PACIF_2206.xlsb

    Any number < 1 or > 3 will result in an Error -2147024809: The expression you entered has an invalid reference to the Parent property.
    Last edited by daredan; 08-15-2022 at 01:06 PM. Reason: added some test cases

  5. #35
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    Ooops. I think that should be strFileName = varFile?
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  6. #36
    daredan is offline Advanced Beginner
    Windows 10 Office 365
    Join Date
    Jul 2022
    Posts
    44
    Next drink is on me. And seeing the solution now, it seems so obvious.
    Click image for larger version. 

Name:	2022-08-15_21-10-53.png 
Views:	15 
Size:	59.0 KB 
ID:	48518

    Thanks a lot for your help!

  7. #37
    Micron is offline Virtually Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    12,800
    You're welcome. Glad you got a solution.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

Page 3 of 3 FirstFirst 123
Please reply to this thread with any new information or opinions.

Similar Threads

  1. VBA to run msgbox failed import excel worksheet
    By mlrucci in forum Import/Export Data
    Replies: 5
    Last Post: 03-14-2021, 08:57 AM
  2. Import an Excel worksheet into Access?
    By Dave Lambert in forum Import/Export Data
    Replies: 1
    Last Post: 09-18-2018, 01:52 PM
  3. Replies: 7
    Last Post: 09-24-2013, 04:53 AM
  4. Replies: 7
    Last Post: 04-22-2013, 08:01 AM
  5. VBA to Import Excel Worksheet
    By bdaniel in forum Programming
    Replies: 2
    Last Post: 11-23-2010, 10:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums