文件名就不用说了吧... 下面的函数是取路径名的. ' '取路径名 '函数:FilePath '参数: Fname 文件绝对路径. '返回值:路径名. '如:"C:\PROMAS\AA.EXE",则返回 "C:\PROMAS\" Public Function FilePath(Fname As String) As String Dim A As Integer Dim B As Integer Dim JlStr As String FilePath = "" B = 0 For A = Len(Fname) To 1 Step -1 If Mid$(Fname, A, 1) = "\" Then B = A: GoTo 100 End If Next A
100: JlStr = Left$(Fname, B) FilePath = JlStr End Function
Public Enum SmDbType
Access = 1 'Access
Excel = 2 'Excel
Text = 3 '文本
FoxPro = 4 'FoxPro
dBase = 5 'dBase
End Enum
'
'创建一个连接(连接到其它数据库类型)
'函数名:CreateOtherConn
'参数: DbConnection ADODB连接,FilePath 数据库路径,UserName 登录用户名,PassWord 登录密码,DbType SmDbType枚举数据库类型
'返回值:TRUE 连接成功.FALSE 连接失败.
'例:
'CreateOtherConn Cnn, "E:\CjhLx\dbf", , , FoxPro
'StrSql = "select * from [employee.dbf]"
'Set Rs = RsOpen(Cnn, StrSql)
'Set DataGrid1.DataSource = RsPublic Function CreateOtherConn(ByRef DbConnection As ADODB.Connection, _
FilePath As String, _
Optional UserName As String = "admin", _
Optional PassWord As String = "", _
Optional DbType As SmDbType = Access) As Boolean
Dim ConnStr As String
Dim DriveName(5) As String
Dim tDbType(5) As String
Dim UserPwd(5) As String
'驱动程序
DriveName(1) = "{Microsoft Access Driver (*.mdb)}"
DriveName(2) = "{Microsoft Excel Driver (*.xls)}"
DriveName(3) = "{Microsoft Text Driver (*.txt; *.csv)}"
DriveName(4) = "{Microsoft Visual FoxPro Driver};SourceType=DBF"
DriveName(5) = "{Microsoft dBase Driver (*.dbf)}"
'类型
tDbType(1) = "MDB"
tDbType(2) = "XLS"
tDbType(3) = "TXT"
tDbType(4) = "FDB"
tDbType(5) = "DDB"
'用户名和密码.
UserPwd(1) = "Uid=" & UserName & ";Pwd=" & PassWord & ";"
UserPwd(2) = "Uid=" & UserName & ";Pwd=" & PassWord & ";"
UserPwd(3) = ""
UserPwd(4) = "Uid=" & UserName & ";Pwd=" & PassWord & ";"
UserPwd(5) = "Uid=" & UserName & ";Pwd=" & PassWord & ";"
On Error Resume Next
If DbConnection.State = adStateOpen And Not IsEmpty(adStateOpen) Then
DbConnection.Close
End If
ConnStr = "Provider=MSDASQL.1;Persist Security Info=False;DRIVER=" & DriveName(DbType) & ";" & UserPwd(DbType) & "DBQ=" & FilePath
DbConnection.ConnectionString = ConnStr
DbConnection.Open
DoEvents
If Err.Number = 0 Then
DbStyle = tDbType(DbType)
CreateOtherConn = True
Else
Err.Clear
DbStyle = ""
CreateOtherConn = False
End If
End Function
'
用一个FILELISTBOX,设置PATTERN属性是:*.DBF即可.
Access = 1 'Access
Excel = 2 'Excel
Text = 3 '文本
FoxPro = 4 'FoxPro
dBase = 5 'dBase1、2、3、5都不要的呀。我不太会,不要写那么深奥好吗?
至于莫依老兄我就不说什么了……(*^_^*)
那怎么用FILELISTBOX得到路径呀?
ConnStr = "Provider=MSDASQL.1;" & _
"Driver=Microsoft Visual FoxPro Driver;" & _
"SourceDB=E:\dbf;" & _
"SourceType=DBF"
conn.Open ConnStr
rs.Open "Select * From Stock", conn, adOpenDynamic, adLockPessimistic
下面的函数是取路径名的.
'
'取路径名
'函数:FilePath
'参数: Fname 文件绝对路径.
'返回值:路径名.
'如:"C:\PROMAS\AA.EXE",则返回 "C:\PROMAS\"
Public Function FilePath(Fname As String) As String
Dim A As Integer
Dim B As Integer
Dim JlStr As String
FilePath = ""
B = 0
For A = Len(Fname) To 1 Step -1
If Mid$(Fname, A, 1) = "\" Then
B = A: GoTo 100
End If
Next A
100:
JlStr = Left$(Fname, B)
FilePath = JlStr
End Function
還是我表達能力爛////