求助:在使用VB访问 INI文件时老是提示 数据类型不匹配

skywen 2003-08-20 04:48:13
这一般是由什么原因引起的?





谢谢。
...全文
70 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hxy2003 2003-08-21
  • 打赏
  • 举报
回复
可能是你在long值的地方声明成了string 吧..
skywen 2003-08-21
  • 打赏
  • 举报
回复
我用了别人的一个类模块

'--------cIniFile.cls 代码----------------
'这里定义了一个cIniFile类
Option Explicit

'// Private member that holds a reference to
'// the path of our ini file
Private strInI As String

'// Win API Declares
Private Declare Function WritePrivateProfileString _
Lib "kernel32" Alias "WritePrivateProfileStringA" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As Any, _
ByVal lpString As Any, _
ByVal lpFileName As String) As Long

Private 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 Function MakePath(ByVal strDrv As String, _
ByVal strDir As String) As String

'// Makes an INI file: Guarantees a sub dir
Do While Right$(strDrv, 1) = "\"
strDrv = Left$(strDrv, Len(strDrv) - 1)
Loop

Do While Left$(strDir, 1) = "\"
strDir = Mid$(strDir, 2)
Loop

'// Return the path
MakePath = strDrv & "\" & strDir
End Function

Public Sub CreateIni(strDrv As String, strDir As String)


'// Make a new ini file
strInI = MakePath(strDrv, strDir)
End Sub

Public Sub WriteFile(strSection As String, _
strKey As String, _
strValue As String)

'// Write to strINI
WritePrivateProfileString strSection, _
strKey, strValue, strInI
End Sub

Public Function GetFile(strSection As String, _
strKey As String) As String

Dim strTmp As String
Dim lngRet As String

strTmp = String$(100, Chr(32))
lngRet = GetPrivateProfileString(strSection, _
strKey, "", strTmp, _
Len(strTmp), strInI)

GetFile = strTmp

End Function

Public Property Let INIFile(ByVal New_IniPath As String)

'// Sets the new ini path
strInI = New_IniPath
End Property

Public Property Get INIFile() As String

'// Returns the current ini path
INIFile = strInI
End Property



使用的时候是这样的



'****************读取短信账号**************************
Dim myIniFile As New cIniFile
'---指定访问的ini文件
If Len(App.Path) > 3 Then
'under disk root dir , eg: "C:\"
myIniFile.INIFile = App.Path & "\setting.ini"
Else
myIniFile.INIFile = App.Path & "setting.ini"
End If
'---读出ini文件的数据,注意,如果是字符串,则去掉末尾一个字符

smsNumber = myIniFile.GetFile("setting", "smsNum")
smsNumber = Left(smsNumber, Len(smsNumber) - 1)

'****************读取完成*****************************

谢谢!
lihonggen0 2003-08-21
  • 打赏
  • 举报
回复

'require variable declaration
Option Explicit

'declares for ini controlling
Private Declare Function GetPrivateProfileSection Lib "kernel32" Alias "GetPrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private 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 Declare Function WritePrivateProfileSection Lib "kernel32" Alias "WritePrivateProfileSectionA" (ByVal lpAppName As String, ByVal lpString As String, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

'when form is loaded
Private Sub Form_Load()

'if error occures resume still
On Error Resume Next

'local variables
Dim File As String, OFLen As Double, _
Str As String

'set our varibles
File = "C:\temp.txt"
OFLen = FileLen(File)

'write few example sections:
WriteIniSection File, "Test1", ""
WriteIniSection File, "Test2", "Here shoud be found some text"

'write few ini strings
WriteIni File, "Test3", "Ini1", "This is ini 1"
WriteIni File, "Test1", "Ini2", "This is ini 2"

'inform we're written the data
MsgBox Format((FileLen(File) - OFLen) / 1024, "0.00") & " KB data written to " & Chr(34) & File & Chr(34)

'read the ini file
Str = Str & "Test2 section: " & vbTab & ReadIniSection(File, "Test2") & vbCrLf
Str = Str & "Test1 section: " & vbTab & ReadIniSection(File, "Test1") & vbCrLf
Str = Str & "Ini1 string: " & vbTab & ReadIni(File, "Test3", "Ini1") & vbCrLf
Str = Str & "Ini2 string: " & vbTab & ReadIni(File, "Test1", "Ini2") & vbCrLf

'show data
MsgBox Str

'end application
End

End Sub

'// INI CONTROLLING PROCEDURES

'reads ini string
Public Function ReadIni(Filename As String, Section As String, Key As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileString(Section, Key, "", RetVal, 255, Filename)
ReadIni = Left(RetVal, v - 1)
End Function

'reads ini section
Public Function ReadIniSection(Filename As String, Section As String) As String
Dim RetVal As String * 255, v As Long
v = GetPrivateProfileSection(Section, RetVal, 255, Filename)
ReadIniSection = Left(RetVal, v - 1)
End Function

'writes ini
Public Sub WriteIni(Filename As String, Section As String, Key As String, Value As String)
WritePrivateProfileString Section, Key, Value, Filename
End Sub

'writes ini section
Public Sub WriteIniSection(Filename As String, Section As String, Value As String)
WritePrivateProfileSection Section, Value, Filename
End Sub
zyl910 2003-08-21
  • 打赏
  • 举报
回复
这一般是由什么原因引起的?
=================================
你的代码有问题
evbsky 2003-08-21
  • 打赏
  • 举报
回复
'********************************************************************************
'** 函数功能:从ini配置文件中读取指定段名、关键字名的值
'** 调用语法: GetInIKeyValue(SectionName as string,KeyName As String,FileName As String)
'** 参数说明:
'** SectionName :段名
'** KeyName :关键字名
'** FileName :ini文件名包括路径
'** 返 回 值:
'** String :返回关键字值
'** 处理说明:
'** 调用API函数GetPrivateProfileString
'******************************************************************************
Public Function GetInIKeyValue(ByVal SectionName As String, _
ByVal KeyName As String, _
ByVal FileName As String) As String
Dim KeyValue$
Dim strTmp As String

KeyValue$ = String$(512, " ")
GetPrivateProfileString SectionName, KeyName, "", KeyValue$, 512, FileName
strTmp = Trim(KeyValue$)
GetInIKeyValue = Left(strTmp, Len(strTmp) - 1)
End Function

'********************************************************************************
'** 函数功能:从ini配置文件中写入指定段名、关键字名及值
'** 调用语法: SetInIKeyValue(SectionName as string,KeyName As String,KeyValue as string ,FileName As String)
'** 参数说明:
'** SectionName :段名
'** KeyName :关键字名
'** KeyValue :关键字值
'** FileName :ini文件名包括路径
'** 返 回 值:
'** 处理说明:
'** 调用API函数WritePrivateProfileString
'******************************************************************************
Public Sub SetInIKeyValue(ByVal SectionName As String, _
ByVal KeyName As String, _
ByVal KeyValue As String, _
ByVal FileName As String)
Dim lng As Long

lng = WritePrivateProfileString(SectionName, KeyName, KeyValue, FileName)
End Sub
planetike 2003-08-20
  • 打赏
  • 举报
回复
'两个函数 , 先在一个模快中定义API函数
Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationname As String, ByVal LpKeyName As Any, ByVal lsString As Any, ByVal lplFilename As String) As Long
'如果是读INT值可以用字符串转化,所以没有另外定义函数
'Declare Function GetPrivateProfileInt Lib "kernel32" Alias "GetPriviteProfileIntA" (ByVal lpApplicationname As String, ByVal LpKeyName As String, ByVal nDefault As Long, ByVal lpFileName As String) As Long
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationname As String, ByVal LpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

'定义读与写INI文件的函数
'****读INI文件****
'文件名 lpFileName 如果不存在会自己创建,如果只有文件名,默认在Windows\system目录下
'[lpAppName]
'lpKeyName=取回的设置值
'lpDefault 当键值不存在时的默认值
Public Function ReadINI(lpFileName As String, lpAppName As String, LpKeyName As String) As String
Dim Temp As String * 20
Dim lpDefault As String
lpDefault = ""
If GetPrivateProfileString(lpAppName, LpKeyName, lpDefault, Temp, Len(Temp), lpFileName) <= 0 Then
ReadINI = ""
Else
ReadINI = MyTrim(Temp) 'MyTrim函数见下
End If
End Function
'****写INI文件****
'[lpAppName]
'lpKeyName=lpString
Public Function WriteINI(lpFileName As String, lpAppName As String, LpKeyName As String, lpString As String) As Boolean
If WritePrivateProfileString(lpAppName, LpKeyName, lpString, lpFileName) = 0 Then
WriteINI = False
Else
WriteINI = True
End If
End Function

'包含三个函数,分别取Rtrim,Ltrim,Trim
'可以去字符串中如ASC码为0,10,13,32的字符
Public Function MyRtrim(Tmpstr As String)
Dim i, s As Integer
i = Len(Tmpstr)
If i = 0 Then
MyRtrim = ""
Exit Function
End If
s = Asc(Right(Tmpstr, 1))
While (s = 0 Or s = 13 Or s = 10 Or s = 32) And i > 0
i = i - 1
Tmpstr = Left(Tmpstr, i)
If Len(Tmpstr) = 0 Then
MyRtrim = ""
Exit Function
End If
s = Asc(Right(Tmpstr, 1))
Wend
MyRtrim = Tmpstr
End Function

Public Function MyLtrim(Tmpstr As String)
Dim i, s As Integer
i = Len(Tmpstr)
If i = 0 Then
MyLtrim = ""
Exit Function
End If
s = Asc(Left(Tmpstr, 1))
While (s = 0 Or s = 13 Or s = 10 Or s = 32) And i > 0
i = i - 1
Tmpstr = Right(Tmpstr, i)
If Len(Tmpstr) = 0 Then
MyLtrim = Tmpstr
Exit Function
End If
s = Asc(Left(Tmpstr, 1))
Wend
MyLtrim = Tmpstr
End Function

Public Function MyTrim(Tmpstr As String)
Tmpstr = MyLtrim(Tmpstr)
Tmpstr = MyRtrim(Tmpstr)
MyTrim = Tmpstr
End Function
GetWindowPos 2003-08-20
  • 打赏
  • 举报
回复
把API声明后面的参数

***as Any的全部改为 as String
射天狼 2003-08-20
  • 打赏
  • 举报
回复
代码贴出来看看,你就这样谁知道怎么告诉你!?

7,762

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧