我用了一位帅哥的函数想判断文件是以doc还是以dwg ,jpg文件,我在一个小程序中用的是Data控件与数据库绑定,DBlist再与数据库中的文件存放地址绑定,实现打开文件,可以用这个函数,一点问题也没有
Private Function ExtractFileName(ByVal PathName As String) As String
     Dim str_Pos As Integer   
       For str_Pos = Len(PathName) To 1 Step -1
        If Mid$(PathName, str_Pos, 1) = "." Then
            Exit For
        End If
    Next str_Pos   
       ExtractFileName = Right$(PathName, Len(PathName) - str_Pos)    
End Function
但我在另外一个程序中,用的是ADODC,用textBox与地址这个字段绑定,但运行却碰到如我标题所说的错误,就出现在Right$上,什么原因啊

解决方案 »

  1.   

    单步跟踪一下!这样看是没有问题关键在于len(PathName)-str_Pos是否大于等于0,小于0便出错。
      

  2.   

    Right$(PathName, Len(PathName) - str_Pos)//出什么错?
    假如Private Sub Command1_Click()
        MsgBox Right$("lxcc", -1)
    End Sub参数是不匹配的!
      

  3.   

    我逐语句执行到打开文件是出错就在Right$上,我在立即窗口查了
    ?Len(PathName)
     0 
    ?Len(PathName)-str_Pos
     0 
    不知道该怎么改啊
      

  4.   

    把right(变量,数值)中的数值保持在1及以上,还有就是数值的值不要大于变量的len(变量)
      

  5.   

    Private Function ExtractFileName(ByVal PathName As String) As String
         Dim str_Pos As Integer   
           For str_Pos = Len(PathName) To 1 Step -1
            If Mid$(PathName, str_Pos, 1) = "." Then
                Exit For
            End If
        Next str_Pos   
           ExtractFileName = Right$(PathName, Len(PathName) - str_Pos)    
    End Function
    这个是获的扩展名吧,你用instrrev多好
      

  6.   

    Private Function ExtractFileName(ByVal PathName As String) As String
      Dim intDotPos As Integer   '右边开边最近的点号位置
      dim intExLen as integer
      dim strReturn as string  strReturn=""
      intDotPos=instrrev(PathName,".")
      if intDotPos>0 then
        intExLen=len(PathName)-intDotPos
        strReturn=right$(PathName,intExLen)
      end if
      ExtractFileName=strReturn
    End Function