컴퓨터/액세스

ms access vba 외부엑셀파일 가져오기

풍경소리^^ 2023. 10. 30. 13:18

https://m.blog.naver.com/PostView.naver?blogId=seek316&logNo=222108639805&categoryNo=67&proxyReferer=

 

[MS-SQL] DATA TYPE - 데이터 타입 종류

● DATA TYPE - 데이터 타입 종류 SQL Server에서 각 열, 지역 변수, 식 및 매개 변수는 관련...

blog.naver.com

Public Sub ImportExcelSpreadsheet(fileName As String, tableName As String)
On Error GoTo BadFormat
' 기존 데이터 지우고 새 데이터 덮어쓰기 - 일련번호 새로부여
    DoCmd.RunSQL "DROP TABLE 가져오기"
    DoCmd.RunSQL "CREATE TABLE 가져오기 (일련번호 AUTOINCREMENT PRIMARY KEY, 성명 TEXT, 전화번호 TEXT, 성별 TEXT)"

' 위 코드 없이 아래 코드만 실행하면 기존 데이터에 새 데이터 추가하게 됨
	DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, "가져오기", fileName, True
    Exit Sub
    
BadFormat:
    MsgBox "The file you tried to import was not an Excel spreadsheet."
End Sub

기존 테이블에 데이터 추가하기

Private Sub Command1_Click()

    Dim excelFilePath As String
    
    excelFilePath = "G:\회사\인사\급여\급여대장데이터.xlsx"
    Dim excelSheetName As String
    
    excelSheetName = "4대급여" ' 대상 시트 이름
    
    ' Access 데이터베이스 파일 경로 및 테이블 이름 설정
    Dim dbPath As String
    dbPath = "급여대장2023엑셀가져오기.accdb" ' Access 데이터베이스 파일 경로
    Dim tableName As String
    tableName = "4대급여" ' 대상 테이블 이름
    
    ' 엑셀 파일 데이터 가져오기
    DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel12, tableName, excelFilePath, True, excelSheetName & "$"
End Sub
Private Sub btnBrowse_Click()
    Dim diag As Office.FileDialog
    Dim item As Variant
    
    Set diag = Application.FileDialog(msoFileDialogFilePicker)
    diag.AllowMultiSelect = False
    diag.Title = "Please select an Excel Spreadsheet"
    diag.Filters.Clear
    diag.Filters.Add "Excel Spreadsheets", "*.xls, *.xlsx"
    
    If diag.Show Then
        For Each item In diag.SelectedItems
            Me.txtFileName = item
        Next
    End If
    
End Sub

Private Sub btnImportSpreadsheet_Click()
    Dim FSO As New FileSystemObject
    
    If Nz(Me.txtFileName, "") = "" Then
        MsgBox "Please select a file!"
        Exit Sub
    End If
    
    If FSO.FileExists(Nz(Me.txtFileName, "")) Then
        ExcelImport.ImportExcelSpreadsheet Me.txtFileName, FSO.GetFileName(Me.txtFileName)
    Else
        MsgBox "File not found!"
    End If
    
End Sub

format_xlsx.xlsx
0.01MB
ImportExcel.accdb
0.49MB

'컴퓨터 > 액세스' 카테고리의 다른 글

access 조합년월 폼 기본값 202407 형식  (0) 2024.07.09
access 일자 조합  (0) 2024.07.09
access 동적배열 - AccessOn  (0) 2022.04.19
액세스 엑셀 데이터 가져오기 TransferSpreadsheet  (0) 2022.04.18
access 과정  (0) 2022.04.17