Kết nối đến file dữ liệu Excel cùng cấu trúc, khác tên

  • Thread starter Thread starter knnam
  • Ngày gửi Ngày gửi
Liên hệ QC

knnam

Thành viên mới
Tham gia
12/8/10
Bài viết
1
Được thích
0
Nhờ các cao nhân chỉ giáo

mình cần kết nối đến file data excel theo đường dẫn như hình và load thông tin ra datasheet
các file excel có cùng cấu trúc nhưng sẽ có tên khác nhau

znuSd6i.png
 
Ví dụ ô chưa đường dẫn tên txtPatch; nút [...] tên cmdLink; nút [Lấy dữ liệu] tên cmdImport; table chứa file excel import tên "tblImportExcel".

------------------------------------
Private sub cmdLink_Click()

Dim FilePath As String, FileName As String


FilePath = LaunchCD(Me) '---> hàm LaunchCD() sẽ tạo trong Module. Hàm dùng để lấy đường dẫn đến file cần import
Me.txtPath = FilePath

End sub
----------------------------------

Private sub cmdImport_Click ()

DoCmd.TransferSpreadsheet acImport, , "tblImportExcel", Me.txtPath, True

End sub
--------------------------------


Tạo hàm LaunchCD () trong module (Hàm này tôi lấy trên net)

------------------------------------------
Private Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As Long


Private Type OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type


Function LaunchCD(strform As Form) As String
Dim OpenFile As OPENFILENAME
Dim lReturn As Long
Dim sFilter As String
OpenFile.lStructSize = Len(OpenFile)
OpenFile.hwndOwner = strform.hwnd
sFilter = "All Files (*.*)" & Chr(0) & "*.*" & Chr(0) & _
"JPEG Files (*.JPG)" & Chr(0) & "*.JPG" & Chr(0)
OpenFile.lpstrFilter = sFilter
OpenFile.nFilterIndex = 1
OpenFile.lpstrFile = String(257, 0)
OpenFile.nMaxFile = Len(OpenFile.lpstrFile) - 1
OpenFile.lpstrFileTitle = OpenFile.lpstrFile
OpenFile.nMaxFileTitle = OpenFile.nMaxFile
OpenFile.lpstrInitialDir = "C:\"
OpenFile.lpstrTitle = "Select a file using the Common Dialog DLL"
OpenFile.flags = 0
lReturn = GetOpenFileName(OpenFile)
If lReturn = 0 Then
MsgBox "Ban khong chon file nao!", vbInformation, _
"Thong bao"
Else
LaunchCD = Trim(Left(OpenFile.lpstrFile, InStr(1, OpenFile.lpstrFile, vbNullChar) - 1))
End If
End Function
-------------------------------------

- Cái bảng chứa dữ liệu import thì bạn dùng Subform với RecordSource là cái table vừa import file excel.
- Còn các râu ria như hiện msgbox, textbox thông báo ngày giờ import, số dòng import...thì bạn tự tìm tòi nhé.
 
Lần chỉnh sửa cuối:
Web KT

Bài viết mới nhất

Back
Top Bottom