这个是我以前做的。。。。你可以参照一下
Public Declare Function GetPrivateProfileString Lib "kernel32" _
Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Type getini
strProvider As String
strServer As String
strUserid As String
strPassword As String
strDatabase As String
End Type
Public Sub main() '读取INI文件
With X '先给变量赋30个空格
.strProvider = Space(30)
.strServer = Space(30)
.strUserid = Space(30)
.strPassword = Space(30)
.strDatabase = Space(30)
GetPrivateProfileString "connectct-zx2", "provider", "sqloledb", .strProvider, 30, App.Path & "\Connect.ini"
GetPrivateProfileString "connectct-zx2", "server", "server", .strServer, 30, App.Path & "\Connect.ini"
GetPrivateProfileString "connectct-zx2", "User id", "sa", .strUserid, 30, App.Path & "\Connect.ini"
GetPrivateProfileString "connectct-zx2", "Password", "hushimiao", .strPassword, 30, App.Path & "\Connect.ini"
GetPrivateProfileString "connectct-zx2", "Database", "jxc", .strDatabase, 30, App.Path & "\Connect.ini"
'取消空格,截取回车符
.strProvider = Left(Trim(.strProvider), (Len(Trim(.strProvider)) - 1))
.strServer = Left(Trim(.strServer), (Len(Trim(.strServer)) - 1))
.strUserid = Left(Trim(.strUserid), (Len(Trim(.strUserid)) - 1))
.strPassword = Left(Trim(.strPassword), (Len(Trim(.strPassword)) - 1))
.strDatabase = Left(Trim(.strDatabase), (Len(Trim(.strDatabase)) - 1))
PobjCn.Open "provider=" & .strProvider & ";server=" & .strServer & ";user id=" & .strUserid & ";password=" & .strPassword & ";database=" & .strDatabase
End With
frmlogin.Show