-
06-05-2009, 12:23 AM #1
Junior Member
- Ngày tham gia
- Aug 2015
- Bài viết
- 0
giúp em sử dụng đoạn code này với!!!
Vô tình e đọc trên topic có đoạn code giúp tìm kiếm file trên ổ đĩa, mà e ko biết sử dụng đoạn code này ra sao, có bác nào biết chỉ e sử dụng với.
đoạn code đó nè:
Public Function DirFiles(Optional strPath As String, Optional strFileName As String = "*.*") As String
If strPath = "" Then strPath = CurrentProject.Path
Dim i As Long, fs As FileSearch
Set fs = Application.FileSearch
With fs
.LookIn = strPath
.FileName = strFileName
If .Execute(SortBy:=msoSortbyFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
Debug.Print "There were " & .FoundFiles.Count & _
" file(s) found in folder '" & strPath & "':"
For i = 1 To .FoundFiles.Count
strFileName = .FoundFiles(i)
strFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName, "\"))
DirFiles = DirFiles & strFileName & ","
Next i
DirFiles = Left(DirFiles, Len(DirFiles) - 1)
Else
Debug.Print "There were no files found."
End If
End With
End Function
Public Function File_Select() As String
On Error Resume Next
Dim dlgOpen As FileDialog, strFileName As String
Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True
.Show
End With
Dim i As Long
For i = 1 To dlgOpen.SelectedItems.Count
strFileName = dlgOpen.SelectedItems(i)
strFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName, "\"))
File_Select = File_Select & strFileName & ","
Next
File_Select = Left(File_Select, Len(File_Select) - 1)
Debug.Print File_Select
End Function
Bác nào biết giúp e nha, hoặc có đoạn code nào hay về tìm kiếm file trên ổ đĩa (chính xác là file word) up lên và chỉ e sử dụng với nha!! thank mấy bác trước.View more random threads:
- Mình muốn tạo Form tìm kiếm dạng lọc theo ngày tháng mong mọi người giúp
- Học Access bằng cách xây dựng một ứng dụng cụ thể - Quản lý Mua và Bán hàng - Phần 1 - Mô tả
- File minh họa việc kết nối đến dữ liệu SQL Server từ MS. Access
- Hỏi xuất dữ liệu Access sang Excel để in!
- Tắt mục
- V/v tiếp tục khởi động ứng dụng quản lý nhập xuất
- Link table từ MDB có password
- Vấn đề với sự kiện BeforeDelConfirm ?
- Tổng quát về phần mềm quản lý nhập - xuất - tồn
- Tạo chương trình trắc nghiệm bằng ACcess
-
06-05-2009, 06:20 AM #2
Junior Member
- Ngày tham gia
- Aug 2015
- Bài viết
- 0
Ðề: giúp em sử dụng đoạn code này với!!!
Hàm DirFiles trả về một chuỗi các tên tập tin tìm thấy trong một đường dẫn.
Ở đây xài CurrentProject: file này chạy kết nối với SQL Server.
Public Function DirFiles(Optional strPath As String, Optional strFileName As String = "*.*") As String
If strPath = "" Then strPath = CurrentProject.Path ' nếu không nhập đường dẫn thì xem đường dẫn là đường dẫn chứa file project - tức file Access đang chạy
' Định nghĩa một biến để tìm kiếm file
Dim i As Long, fs As FileSearch
Set fs = Application.FileSearch
With fs
.LookIn = strPath ' tìm trong đường dẫn đã chỉ định
.FileName = strFileName ' tìm theo mẫu tên file
' Nếu tìm thấy file thì hiển thị thông báo cho biết có bao nhiêu file
If .Execute(SortBy:=msoSortbyFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
Debug.Print "There were " & .FoundFiles.Count & _
" file(s) found in folder '" & strPath & "':"
' Duyệt tuần tự từng file để lấy tên
For i = 1 To .FoundFiles.Count
strFileName = .FoundFiles(i)
strFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName, "\"))
DirFiles = DirFiles & strFileName & ","
Next i
DirFiles = Left(DirFiles, Len(DirFiles) - 1) ' bỏ bớt dấu phẩy cuối cùng
Else
' Báo không có file nào
Debug.Print "There were no files found."
End If
End With
End Function
Hàm File_Select dùng để chọn một số file, gán danh sách file vào một chuỗi
Public Function File_Select() As String
On Error Resume Next
' Định nghĩa một hộp thoại mở file
Dim dlgOpen As FileDialog, strFileName As String
Set dlgOpen = Application.FileDialog(msoFileDialogOpen)
With dlgOpen
.AllowMultiSelect = True ' cho phép chọn nhiều file cùng lúc
.Show ' Hiện hộp thoại
End With
Dim i As Long
' Gán danh sách vào chuỗi
For i = 1 To dlgOpen.SelectedItems.Count
strFileName = dlgOpen.SelectedItems(i)
strFileName = Right(strFileName, Len(strFileName) - InStrRev(strFileName, "\"))
File_Select = File_Select & strFileName & ","
Next
File_Select = Left(File_Select, Len(File_Select) - 1)
Debug.Print File_Select
End Function
-
06-05-2009, 07:47 AM #3
Silver member
- Ngày tham gia
- Mar 2016
- Bài viết
- 6
Ðề: giúp em sử dụng đoạn code này với!!!
uả mà e muốn sử dụng đoạn code này để tìm 1 file word, thì e phải sử dụng sao hả a phatnq2002
-
06-05-2009, 04:04 PM #4
Junior Member
- Ngày tham gia
- Aug 2015
- Bài viết
- 0
Ðề: giúp em sử dụng đoạn code này với!!!
Gửi bởi nttien1234
Public Function DirFiles(strFileName As String, Optional strPath As String) As Boolean
If strPath = "" Then strPath = CurrentProject.Path
Dim i As Long, fs As FileSearch
Set fs = Application.FileSearch
With fs
.LookIn = strPath
.FileName = strFileName
If .Execute(SortBy:=msoSortbyFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
DirFiles = True
Else
DirFiles = Flase
End If
End With
End Function
Ở nơi cần tìm file (sub/function khác), gõ vào đoạn lệnh sau:
If Not DirFiles("myfile.doc", "D:-/My Documents") Then
MsgBox "Không tìm thấy."
Else
' Làm gì khi tìm thấy
End If
-
06-05-2009, 06:11 PM #5
Junior Member
- Ngày tham gia
- Nov 2015
- Bài viết
- 3
Ðề: giúp em sử dụng đoạn code này với!!!
Thank a phatnq2002 nha! mà sẵn đó, a hướng dẫn e cái đoạn code thứ 2 luôn đi! Có phải đoạn code thứ 2 nó giống với nút browse phải ko a? nghĩa là để mình imports 1 file nào đó vào 1 table mà mình đã tạo sẵn trong access phải ko?
vd: như mình muốn imports 1 file excel, thì mình sẽ gán đoạn code đó vào 1 nút button, nút đó mình đặt tên là btgan, khi mình bấm nút đó và chọn đường dẫn đến 1 file excel bất kỳ, thì nó tự động gán vào cái table mà mình đã tạo sẵn trong access phải ko a?
Nếu đúng là đoạn code đó sử dụng như vậy thì a hướng dẫn e cách sử dụng nó luôn đi và làm sao để mình imports cái file excel đó vào trong table mình tạo sẵn trong access? thank a phatnq2002 trước :giavo:
-
06-05-2009, 06:42 PM #6
Junior Member
- Ngày tham gia
- Aug 2015
- Bài viết
- 0
Ðề: giúp em sử dụng đoạn code này với!!!
Gửi bởi nttien1234
Private Sub cmdBROWSE_Click()
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogFilePicker)
With dlg
.AllowMultiSelect = False
.ButtonName = "Chon file"
.Filters.Add "File MS Excel", "*.xls", 1
If .Show = -1 Then
txtFilename = dlg.SelectedItems(1)
End If
End With
End Sub
Private Sub cmdOK_Click()
If Trim(txtFilename) = "" Then Exit Sub
On Error GoTo err_cmdOK_Click
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "myTable", txtFilename, True
MsgBox "Da import du lieu thanh cong."
exit_cmdOK_Click:
Exit Sub
err_cmdOK_Click:
MsgBox "Loi: " & Err.Number & vbCrLf & "Noi dung: " & Err.Description & vbCrLf & "Khong the import duoc."
Resume exit_cmdOK_Click
End Sub
Trong đó: myTable là tên của table nhận. Nếu table này chưa có, Access sẽ tự tạo một table mới.
-
06-06-2009, 12:08 AM #7
Junior Member
- Ngày tham gia
- Aug 2015
- Bài viết
- 0
Ðề: giúp em sử dụng đoạn code này với!!!
thank a phatnq2002 nhìu lắm!!! :nuhon::noinhiu:
Theo phong tục của người M’nông, khi đứa trẻ vừa tròn một mùa rẫy (nghĩa là vừa tròn một năm tuổi), cha mẹ đứa trẻ phải tiến hành làm lễ cắt tóc (krah soh) và lễ xỏ tai (chuh tor) cho con mình, nhằm...
Lễ cắt tóc (krah soh) và lễ xỏ tai (chuh tor) cho trẻ sơ sinh của người M’nông