Page 2 of 3 FirstFirst 123 LastLast
Results 16 to 30 of 37
  1. #16
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    this is also a different approach i have tried:



    Code:
    Dim db As dao.Database, rs As dao.Recordset
    
    Dim xlApp As Excel.Application
    Dim xlFile As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    Dim xlRange As Excel.Range
    Dim r#, c#
    Dim strPath As String, strFile As String, strTable As String
    
    
    
    
    Set xlApp = New Excel.Application
    
    
    strPath = "T:\XL Files\"
    strFile = "ASCII-Codes2.xlsx"
    
    
    Set xlFile = xlApp.Workbooks.Open(strPath & strFile)
    Set xlSheet = xlFile.Sheets("Sheet1")
    Set xlRange = xlSheet.Range("A1:C256")
    
    
        For r = 1 To xlRange.Rows.Count
            For c = 1 To xlRange.Columns.Count
            
            Set db = CurrentDb()
            Set rs = db.OpenRecordset("Select * From tblAscllCodes")
            With rs
                .AddNew
                !Code = c
                .Update
                .Close
            End With
        
    
    
            Next c
        Next r
    
    
    xlApp.Quit
    Set xlApp = Nothing

  2. #17
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    That is nothing like your first bunch of code.
    That code will not do what you were attempting with your first post.

    Just link to it as @June7 suggested.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #18
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    doesn't really answer the question since you didn't provide an example of the required result - yes implies you have one row populated with 0123456..... in your code column for example. If all you are wanting to do is end up with a table with 256 rows, use the sql option

  4. #19
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    maybe upload your ascii-excel workbook and your ms access database (both in .zip).
    then we can see the structure (including the column headers) in the excel sheet against
    the table fields of your table.

  5. #20
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Hi All, just tried this from June7 solution

    Code:
    strPath = "T:\XL Files\"strFile = "ASCII-Codes2.xlsx"
    
    
    CurrentDb.Execute "INSERT INTO tblAscllCodes SELECT * FROM [Sheet1$] IN" & strPath & strFile & "[Excel 12.0;HDR=yes;IMEX=0;ACCDB=Yes]"
    runtime error 3131 Syntax error in FROM clause

    Whilst i have made the file a variable, when hover over, does show correct path and file or i could go back adding full path and file in the execute statement ?

    @jojowhite, yes i can easily add the excel file, the db is more complicated, shared and lots to move, could export table and command button to execute....

    TY

  6. #21
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    Perhaps this is the code you are looking for

    Code:
    Dim db As dao.Database, rs As dao.Recordset
    
    
    Dim xlApp As Excel.Application
    Dim xlFile As Excel.Workbook
    Dim xlSheet As Excel.Worksheet
    
    Dim strPath As String, strFile As String, strTable As String
    dim r as Long
    
    strPath = "T:\XL Files\"
    strFile = "ASCII-Codes2.xlsx"
    strTable=" tblAscllCodes"
    
    Set xlApp = New Excel.Application
    Set xlFile = xlApp.Workbooks.Open(strPath & strFile)
    Set xlSheet = xlFile.Sheets("Sheet1")
    
    
    
    Set db = CurrentDb()    
    Set rs = db.OpenRecordset("Select * From " & strTable)
    
    
    With rs    
    
    
        For r = 2 To  xlSheet.Rows.Count
      
                .AddNew
            !Code =  xlSheet.Range("A" & r & ":A" & r).Value 
            !Character= xlSheet.Range("B" & r & ":B" & r).Value
            !Description= xlSheet.Range("C" & r & ":C" & r).Value
                    .Update
            
        Next r
    
    
       .Close
    
    
    End With 'rs
    
    
    xlApp.Quit
    Set xlApp = Nothing

  7. #22
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    strPath = "T:\XL Files"strFile = "ASCII-Codes2.xlsx
    What on earth!


    try

    Code:
    strPath = "C:\Users\davem\Desktop\Excel\"
    strFile = "ASCII-Codes.xlsx"
    CurrentDb.Execute "INSERT INTO tblAscllCodes SELECT * FROM [Sheet1$] IN " & strPath & strFile & "[Excel 12.0;HDR=yes;IMEX=0;ACCDB=Yes]"


    there was a missing space after IN

    and assumes the column names (and order) in excel matched the field names in your table.

    If they don't then modify the sql string to something like

    "INSERT INTO tblAscllCodes (Code, Character, Description) SELECT XLCode, XLCharacter, XLDescription FROM [Sheet1$] IN " & strPath & strFile & "[Excel 12.0;HDR=yes;IMEX=0;ACCDB=Yes]"

    where XLCode etc is the relevant name of the column in excel

  8. #23
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    @CJ London, that has done the trick, TY indeed, i just nee to add a PK field auto number to increment.

    will look at transferspreadsheet method at some stage just for learning purposes, not really pulled data from excel into table, done lots in reverse

    Also for me to understand, why is:

    For r = 2 To xlRange.Rows.Count ?

    I would assume this would skip a row, it certainly doesn't but i would have written for r = 1 ( 1 row at a time), an not for r = 2, please educate me for learning purposes ?

  9. #24
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    @ CJ, this still comes up with syntax error in FROM clause, note path is at work so if you see path differences, its because laptop @ home and desktop at work

    Code:
    Dim strPath As String, strFile As String
    
    strPath = "T:\XL Files\"
    strFile = "ASCII-Codes2.xlsx"
    
    
    CurrentDb.Execute "INSERT INTO tblAscllCodes SELECT * FROM [Sheet1$] IN " & strPath & strFile & "[Excel 12.0;HDR=yes;IMEX=0;ACCDB=Yes]"
    
    
    Your recordset method totally worked though :)

  10. #25
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    Also for me to understand, why is:

    For r = 2 To xlRange.Rows.Count ?
    r is the row number - r1 would be the header row which you don't want

    You don't really need xlRange since you don't reference it other than for setting the loop - and it has the same number of rows as the sheet.

    actually - just realised that would leave out the last row. You could just as easily have used

    For r = 2 To 257

    I'll edit the example

  11. #26
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    i just nee to add a PK field auto number to increment.
    perhaps, but since we can only guess at the data I would have thought the code would be that - just set allow duplicates to false

    still comes up with syntax error in FROM clause,
    I should have looked at my own code - you need some single quotes - in red here

    Code:
    CurrentDb.Execute "INSERT INTO tblAscllCodes SELECT * FROM [Sheet1$] IN '" & strPath & strFile & "'[Excel 12.0;HDR=yes;IMEX=0;ACCDB=Yes]"

  12. #27
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Ahh for string delimiters ?

    Will give that a go also

  13. #28
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Solved Post 21

  14. #29
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    !Code = xlSheet.Range("A" & r & ":A" & r).Value
    !Character= xlSheet.Range("B" & r & ":B" & r).Value
    !Description= xlSheet.Range("C" & r & ":C" & r).Value

    I would just use
    Code:
            !Code =  xlSheet.Range("A" & r).Value 
            !Character= xlSheet.Range("B" & r).Value
            !Description= xlSheet.Range("C" & r).Value
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  15. #30
    DMT Dave is offline VIP
    Windows 10 Access 2016
    Join Date
    May 2018
    Posts
    1,365
    Ah ok WGM, also additional to CJ's, i have set the range via finding first row and last row

    intFR (first row)
    intLR (last row)

    Code:
    intFR = xlSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlDown).Row
    intLR = xlSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    
    
    Set xlRange = xlSheet.Range("A" & intFR & ":A" & intLR)
    
    
    'Set xlRange = xlSheet.Range("A1:C256")' CJ

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

Similar Threads

  1. Import Excel data cell by cell into an Access table.
    By russmann2000 in forum Import/Export Data
    Replies: 3
    Last Post: 03-21-2018, 07:18 PM
  2. Adding a Cell Range to a String Variable
    By RunTime91 in forum Access
    Replies: 3
    Last Post: 01-27-2018, 02:38 PM
  3. Clear Cell Range Based on Text in Another Cell
    By Oxygen Potassium in forum Access
    Replies: 3
    Last Post: 08-20-2017, 08:12 PM
  4. Replies: 1
    Last Post: 07-12-2013, 01:48 PM
  5. Can we post Access cell data to Excel cell properties?
    By Zethro in forum Import/Export Data
    Replies: 1
    Last Post: 12-13-2005, 08:42 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