Access交流中心

北京 | 上海 | 天津 | 重庆 | 广州 | 深圳 | 珠海 | 汕头 | 佛山 | 中山 | 东莞 | 南京 | 苏州 | 无锡 | 常州 | 南通 | 扬州 | 徐州 | 杭州 | 温州 | 宁波 | 台州 | 福州 | 厦门 | 泉州 | 龙岩 | 合肥 | 芜湖 | 成都 | 遂宁 | 长沙 | 株洲 | 湘潭 | 武汉 | 南昌 | 济南 | 青岛 | 烟台 | 潍坊 | 淄博 | 济宁 | 太原 | 郑州 | 石家庄 | 保定 | 唐山 | 西安 | 大连 | 沈阳 | 长春 | 昆明 | 兰州 | 哈尔滨 | 佳木斯 | 南宁 | 桂林 | 海口 | 贵阳 | 西宁 | 乌鲁木齐 | 包头 |

[5分]上传图片或PDF到指定文件夹

陈琳  发表于:2013-04-08 16:29:47  
复制

     老师,您好。

     我想在新增窗体里加入上传文件的功能,将文件上传到指定的文件夹。

代码是:

     Public Function UploadFile(strDestinationPath As String, _
                            FileType As acFileType, _
                            Optional bIsUpload As Boolean = True) As String

                      
  On Error GoTo ErrorHandler
    Dim strSourceFile As String
    Dim strDestinationFile As String
    Dim strFullFileName As String
    Dim FSO As Object
    
    If Right$(strDestinationPath, 1) <> "\" Then
        strDestinationPath = strDestinationPath & "\"
    End If
    
    Set FSO = CreateObject("Scripting.FileSystemObject")
    If Not FSO.FolderExists(strDestinationPath) Then
        MsgBox "This path[" & strDestinationPath & "]is not found,please check the path!", vbCritical, "????"
        Set FSO = Nothing
        Exit Function
    End If
    
    With Application.FileDialog(3)
        .Title = "Choose File"
        .InitialFileName = ""
        .Filters.Clear
        Select Case FileType
        Case 1
            .Filters.Add "Graphics Files", "*.jpg;*.bmp;*.gif;*.png"
        Case 2
            .Filters.Add "Files", "*.pdf;*.doc;*.docx;*.xls;*.rar"
        Case Else
            .Filters.Add "All Files", "*.*"
        End Select
        .AllowMultiSelect = False
        If .Show Then
            strSourceFile = .SelectedItems.Item(1)
            strDestinationFile = strDestinationPath & Mid$(strSourceFile, InStrRev(strSourceFile, "\") + 1)
            strFullFileName = Mid$(strSourceFile, InStrRev(strSourceFile, "\") + 1)
            
            If bIsUpload = True Then
                If FSO.FileExists(strDestinationFile) = True Then
                    If MsgBox("File Exists,overwrite it?", vbOKCancel + vbInformation, "??") = vbOK Then
                        FSO.copyfile strSourceFile, strDestinationFile, True
                    Else
                        Exit Function
                    End If
                Else
                    FSO.copyfile strSourceFile, strDestinationFile
                End If
                Set FSO = Nothing
                UploadFile = strDestinationFile
            Else
                UploadFile = strSourceFile
            End If
            
        End If
    End With
    
ExitHere:
    Set FSO = Nothing
    Exit Function
    
ErrorHandler:
    MsgBox Err.Description, vbInformation, "??"
    Resume ExitHere
End Function


Private Sub cmd_addImg_Click()
    Dim strDestinationPath As String
    Dim strReturnFile As String
    strDestinationPath = CurrentProject.Path & "\NDA\"
    strReturnFile = UploadFile(strDestinationPath, acPicture, True)


    If Len(strReturnFile) > 0 Then
        Me.imgPath = strReturnFile
        Me.Image0.Picture = strReturnFile
    End If
End Sub
Private Sub Image0_DblClick(Cancel As Integer)
    ShellEx Me.Image0.Picture
End Sub

出现这个错误:


不知道可否指导一下,谢谢!

 

Top
杜超-2号 发表于:2013-04-08 16:35:51

有个函数没定义,模块复制了没

 



陈琳 发表于:2013-04-08 17:35:32
金宇的例子里,只有两个模块,一个是UploadFile,一个是系统模块,我只复制了UploadFile这个模块。

陈琳 发表于:2013-04-08 18:06:44
老师,好了。

总记录:3篇  页次:1/1 9 1 :