Copying Data from Excel and Pasting in an Access Table (1 Viewer)

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
Hello Guys,

I have an Excel Spreadsheet. By the End of the day, users copy and paste values from the Excel cells in to an Access Table.

Is there any method to automate this process?

In the Excel spreadsheet when a User select a Range (e.g. A1 to D 14) and click a command button in Excel, I want to paste those values in the Range A1 to D14 (Only the Selected Cell's values) in my Access's Table.

Thanks
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:59
Joined
Feb 19, 2002
Messages
43,263
Have you considered having the users enter the data directly into Access? That would be the best solution. An alternative would be to number the rows so that there is a unique identifier. Then the Access app can link to the spreadsheet and use an append query to copy in all rows > the maxim row number previously imported. This method isn't reliable if your users might mess with the sequence # or delete or update old rows.
 

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
Have you considered having the users enter the data directly into Access? That would be the best solution. An alternative would be to number the rows so that there is a unique identifier. Then the Access app can link to the spreadsheet and use an append query to copy in all rows > the maxim row number previously imported. This method isn't reliable if your users might mess with the sequence # or delete or update old rows.
Thanks a lot for your reply.

We are getting data from an External source in an Excel Format. For Data Entry that Excel in to Access it may take 1-2 hours and it will be done in few minutes if they copy and paste data (Manually) from Excel to Access. I am trying to even minimize that copy paste time. If there is a Macro to copy and paste selected Excel Data in to Access Table in a Button click, users can do it in few seconds. Select the range and click the command button, job is done. But I am not sure how to do this.

Thank You
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 07:59
Joined
Feb 19, 2002
Messages
43,263
If the data comes to you as spreadsheets, have you tried importing the data directly? You might try linking to the spreadsheet. That way you can use an append query to select specific columns and depending on the sheet, perhaps even identify just the rows you want to import.

Creating a macro in excel to copy and paste is also a possibility but the macro would have to be in every workbook.
 
Last edited:

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
If the data comes to you as spreadsheets, have you tried importing the data directly? You might try linking to the spreadsheet. That way you can use an append query to select specific columns and depending on the sheet, perhaps even identify just the rows you want to import.

Creating a macro in excel to copy and paste is also a possibility be the macro would have to be in every workbook.
Thanks a lot for your suggestion. Let me try

Thank You
 

bastanu

AWF VIP
Local time
Today, 04:59
Joined
Apr 13, 2010
Messages
1,402
Have a look at Docmd.ransferSpreadsheet method specifically using the Range argument, might be exactly what you need:

You can build an importing form with a text box where users can enter the range then reference that in your code

DoCmd.TransferSpreadsheet acImport, 3, "Employees","C:\Lotus\Newemps.wk3", True, Me.txtExcelRange '"A1:G12"
 

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
Have a look at Docmd.ransferSpreadsheet method specifically using the Range argument, might be exactly what you need:

You can build an importing form with a text box where users can enter the range then reference that in your code

DoCmd.TransferSpreadsheet acImport, 3, "Employees","C:\Lotus\Newemps.wk3", True, Me.txtExcelRange '"A1:G12"

Many thanks for your reply.

I have put the following code in the Click event of a command button

DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel3, "TestTable", "\\ABC\MyDocs\S\FirstName.LastName\Test.rawdata", True, "A1:O3"

And I am getting an Error "Could not find installable ISAM"

1616507777089.png


Could you please let me know how to solve this?

Thanks
 

bastanu

AWF VIP
Local time
Today, 04:59
Joined
Apr 13, 2010
Messages
1,402
I agree, you should try acSpreadsheetTypeExcel12 and also you need to add the full name of the Excel file (full path and extension).
Cheers,
Vlad
 

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
I doubt Excel3 is the spreadsheet type you want
Try something more current.

These are the Fields in my TestTable:

1616510898936.png


This is the Values in Test.xlsx File:

1616511038988.png


My Test.xlsx file is located in a Network Drive in M: Drive

This is my Code:

Code:
Private Sub Command154_Click()


DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "TestTable", "M:\DATBASES ALL PROGRAM\Test.xlsx", True, "A2:C24"


End Sub

I am getting the following Error Message

1616511183250.png


Thanks
 

bastanu

AWF VIP
Local time
Today, 04:59
Joined
Apr 13, 2010
Messages
1,402
Because you start from A2 you should set the HasFieldNames to False instead of True (just before the range). Also the date format in the sample Excel file might not be recognized as a Date field.
Cheers,
 

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
Because you start from A2 you should set the HasFieldNames to False instead of True (just before the range). Also the date format in the sample Excel file might not be recognized as a Date field.
Cheers,
Thanks a lot for your reply. I tried this before. Also I even tried delete the Date field in the Access Table and Excel. But getting same error message

Thanks
 

bastanu

AWF VIP
Local time
Today, 04:59
Joined
Apr 13, 2010
Messages
1,402
And what size (length) are the two fields in the Access table? Notice you have LName in Access but SName in Excel.
Cheers,
 

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
And what size (length) are the two fields in the Access table? Notice you have LName in Access but SName in Excel.
Cheers,
Sorry that was my mistake. I have changed LName in both Access Table and Excel. But getting still the same Error

Fname - Short Text - Field Size 255
Lname - Short Text - Field Size 255
DoB - Date/Time - Format yyyy-mm-dd

Thanks
 

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
And what size (length) are the two fields in the Access table? Notice you have LName in Access but SName in Excel.
Cheers,

Hi,

When I create a new database and a new table, this code is working perfectly. I am not sure what's wrong with my Existing Database and I need this in my Existing Database.

Thanks
 

bastanu

AWF VIP
Local time
Today, 04:59
Joined
Apr 13, 2010
Messages
1,402
Sorry but can't really say without seeing it, could you create a small sample with just that table (can be empty) and a sample Excel file to import and attach it here so we could have a look.
Cheers,
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 19:59
Joined
May 7, 2009
Messages
19,235
maybe you need a Custom import function.
to use
Code:
Private Sub Command154_Click()


call importExcelData("TestTable", "M:\DATBASES ALL PROGRAM\Test.xlsx", "A2:C24")


End Sub

the custom function:
Code:
Public Function importExcelData(tbl As String, ByVal wrkBook As String, ByVal rng As String, Optional ByVal shtNumber As Integer = 1)
' arnelgp
'
' Note:
'
' the excel sheet must have Column Header for this to work.
' rng must not include the Column Header (only the data to import)
'
' answer to:
' https://www.access-programmers.co.uk/forums/threads/copying-data-from-excel-and-pasting-in-an-access-table.316926/
' DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "TestTable", "M:\DATBASES ALL PROGRAM\Test.xlsx", True, "A2:C24"
'
'
    Dim cFields As New Collection
   
    Dim objExcel As Object
    Dim objWbk As Object
    Dim objSht As Object
    Dim start_column As Long, end_column As Long
    Dim start_row As Long, end_row As Long
    Dim v As Variant, i As Long, j As Long
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim sColumn As String, bOK As Boolean
    Set db = CurrentDb
    Set rs = db.OpenRecordset(tbl, dbOpenDynaset)
    Set objExcel = CreateObject("Excel.Application")
    Set objWbk = objExcel.Workbooks.Open(wrkBook)
    Set objSht = objWbk.Sheets(shtNumber)
   
    'get the header
    v = Split(rng, ":")
    With objSht
        start_column = .Range(LetterOnly(v(0)) & "1").Column
        start_row = NumberOnly(v(0))
        end_column = .Range(LetterOnly(v(1)) & "1").Column
        end_row = NumberOnly(v(1))
       
        For i = start_column To end_column Step 1
            sColumn = .Cells(1, i).Value & ""
            bOK = False
            If Len(sColumn) > 0 Then
                For j = 0 To rs.Fields.Count - 1
                    If rs.Fields(j).Name = sColumn Then
                        bOK = True
                        Exit For
                    End If
                Next
                If bOK Then
                    cFields.Add sColumn, i & ""
                Else
                    cFields.Add "", i
                End If
            End If
        Next i
   
        'save the data to the table
        For i = start_row To end_row Step 1
            rs.AddNew
            For j = start_column To end_column Step 1
                If Len(cFields(j)) > 0 Then
                    v = .Cells(i, j).Value
                    rs(cFields(j & "")).Value = v
                End If
            Next
            rs.Update
        Next
   
    End With
    'housekeeping
    Set objSht = Nothing
    objWbk.Close
    Set objWbk = Nothing
    objExcel.Quit
    Set objExcel = Nothing
   
    rs.Close
    Set rs = Nothing
    Set db = Nothing
End Function

Public Function LetterOnly(ByVal ps As String) As String
LetterOnly = getps(ps, "string")
End Function

Public Function NumberOnly(ByVal ps As String) As Long
NumberOnly = getps(ps, "number")
End Function

Public Function getps(ByVal ps As String, pn As String) As Variant
    With CreateObject("VBScript.RegExp")
   
        If pn = "string" Then
            .Pattern = "[^a-z]"
        Else
            .Pattern = "[^0-9]"
        End If
        .Global = True
        .IgnoreCase = True
       
        getps = Trim(.Replace(ps, ""))
       
    End With
End Function
 

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
Sorry but can't really say without seeing it, could you create a small sample with just that table (can be empty) and a sample Excel file to import and attach it here so we could have a look.
Cheers,

I think I solved this. Actually my Database was saved in a Network Drive and there were 8 Subfolders. When I moved my Access Database in to the Main Network Drive (e.g. M:\Database\Current Database) and put the following code, this work perfectly

Code:
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "TestTable", "M:\Database\Current Database\Excel Files\Test.xlsx", True, Text158
 

JithuAccess

Member
Local time
Today, 05:59
Joined
Mar 3, 2020
Messages
297
maybe you need a Custom import function.
to use
Code:
Private Sub Command154_Click()


call importExcelData("TestTable", "M:\DATBASES ALL PROGRAM\Test.xlsx", "A2:C24")


End Sub

the custom function:
Code:
Public Function importExcelData(tbl As String, ByVal wrkBook As String, ByVal rng As String, Optional ByVal shtNumber As Integer = 1)
' arnelgp
'
' Note:
'
' the excel sheet must have Column Header for this to work.
' rng must not include the Column Header (only the data to import)
'
' answer to:
' https://www.access-programmers.co.uk/forums/threads/copying-data-from-excel-and-pasting-in-an-access-table.316926/
' DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "TestTable", "M:\DATBASES ALL PROGRAM\Test.xlsx", True, "A2:C24"
'
'
    Dim cFields As New Collection
  
    Dim objExcel As Object
    Dim objWbk As Object
    Dim objSht As Object
    Dim start_column As Long, end_column As Long
    Dim start_row As Long, end_row As Long
    Dim v As Variant, i As Long, j As Long
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim sColumn As String, bOK As Boolean
    Set db = CurrentDb
    Set rs = db.OpenRecordset(tbl, dbOpenDynaset)
    Set objExcel = CreateObject("Excel.Application")
    Set objWbk = objExcel.Workbooks.Open(wrkBook)
    Set objSht = objWbk.Sheets(shtNumber)
  
    'get the header
    v = Split(rng, ":")
    With objSht
        start_column = .Range(LetterOnly(v(0)) & "1").Column
        start_row = NumberOnly(v(0))
        end_column = .Range(LetterOnly(v(1)) & "1").Column
        end_row = NumberOnly(v(1))
      
        For i = start_column To end_column Step 1
            sColumn = .Cells(1, i).Value & ""
            bOK = False
            If Len(sColumn) > 0 Then
                For j = 0 To rs.Fields.Count - 1
                    If rs.Fields(j).Name = sColumn Then
                        bOK = True
                        Exit For
                    End If
                Next
                If bOK Then
                    cFields.Add sColumn, i & ""
                Else
                    cFields.Add "", i
                End If
            End If
        Next i
  
        'save the data to the table
        For i = start_row To end_row Step 1
            rs.AddNew
            For j = start_column To end_column Step 1
                If Len(cFields(j)) > 0 Then
                    v = .Cells(i, j).Value
                    rs(cFields(j & "")).Value = v
                End If
            Next
            rs.Update
        Next
  
    End With
    'housekeeping
    Set objSht = Nothing
    objWbk.Close
    Set objWbk = Nothing
    objExcel.Quit
    Set objExcel = Nothing
  
    rs.Close
    Set rs = Nothing
    Set db = Nothing
End Function

Public Function LetterOnly(ByVal ps As String) As String
LetterOnly = getps(ps, "string")
End Function

Public Function NumberOnly(ByVal ps As String) As Long
NumberOnly = getps(ps, "number")
End Function

Public Function getps(ByVal ps As String, pn As String) As Variant
    With CreateObject("VBScript.RegExp")
  
        If pn = "string" Then
            .Pattern = "[^a-z]"
        Else
            .Pattern = "[^0-9]"
        End If
        .Global = True
        .IgnoreCase = True
      
        getps = Trim(.Replace(ps, ""))
      
    End With
End Function
I think I solved this. Actually my Database was saved in a Network Drive and there were 8 Subfolders. When I moved my Access Database in to the Main Network Drive (e.g. M:\Database\Current Database) and put the following code, this work perfectly

Code:
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "TestTable", "M:\Database\Current Database\Excel Files\Test.xlsx", True, Text158
 

Users who are viewing this thread

Top Bottom