标题:[求助]关于类模块的探讨
只看楼主
tiger111
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2007-8-19
 问题点数:0 回复次数:7 
[求助]关于类模块的探讨
我编写了一个类模块,如下
Public Function ExecuteSQL(ByVal SQL As String) As ADODB.Recordset
SQL = Trim(SQL)
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset

Dim strAppPath As String
strAppPath = App.Path
If Right(strAppPath, 1) <> "\" Then
strAppPath = strAppPath & "\"
End If

strAppPath = strAppPath & "data\wx.mdb"
ConnectString = "DRIVER=Microsoft Access Driver (*.mdb);DSN=" & strAppPath & ";UID=;PWD="
rst.open
Set rst.activeconnection = cnn
rst.LockType = adLockOptimistic
rst.CursorType = adOpenKeyset
rst.open SQL
Set exesql = rst
Set rst = Nothing
Set cnn = Nothing
end function
但是运行时提示“用户定义类型未定义”的错误,本人不知应是如处进行添加,请懂得的给于帮忙,谢谢!
搜索更多相关主题的帖子: 类模块 SQL strAppPath ADODB 
2007-08-19 07:38
multiple1902
Rank: 8Rank: 8
等 级:贵宾
威 望:42
帖 子:4881
专家分:671
注 册:2007-2-9
得分:0 

和是不是类模块无关

……你也不告诉我是哪行出错的

检查你有没有引入ADODB的库

Dim strAppPath As String
strAppPath = App.Path
If Right(strAppPath, 1) <> "\" Then
strAppPath = strAppPath & "\"
End If

如果这个过程会多次执行的话建议你用缓存,就算你不想用缓存的话也最好不要用Right(str,1)<>"\"

建议用Len(App.Path)=3代替,毕竟字符串的比较更费时间

2007-08-19 10:28
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
得分:0 

郁闷


/images/2011/147787/2011051411021524.jpg" border="0" />
2007-08-19 17:45
ggyy66
Rank: 1
等 级:新手上路
帖 子:427
专家分:0
注 册:2007-8-14
得分:0 
应该是没有引用ado类库

2007-08-20 11:16
缘吇弹
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:地球
等 级:版主
威 望:43
帖 子:3038
专家分:27
注 册:2007-7-2
得分:0 
ioriliao有何郁闷

Repeat  Life=Study;Until (death);
2007-08-20 14:22
ioriliao
Rank: 7Rank: 7Rank: 7
来 自:广东
等 级:贵宾
威 望:32
帖 子:2829
专家分:647
注 册:2006-11-30
得分:0 
郁闷原来不是类模块的问题

/images/2011/147787/2011051411021524.jpg" border="0" />
2007-08-20 20:51
simpson
Rank: 3Rank: 3
等 级:论坛游民
威 望:7
帖 子:863
专家分:17
注 册:2006-11-16
得分:0 
Public Function ExecuteSQL(ByVal SQL As String) As ADODB.Recordset
SQL = Trim(SQL)
Dim cnn As ADODB.Connection
Dim rst As ADODB.Recordset
Set cnn = New ADODB.Connection
Set rst = New ADODB.Recordset

Dim strAppPath As String
strAppPath = App.Path
If Right(strAppPath, 1) <> "\" Then
strAppPath = strAppPath & "\"
End If

strAppPath = strAppPath & "data\wx.mdb"
ConnectString = "DRIVER=Microsoft Access Driver (*.mdb);DSN=" & strAppPath & ";UID=;PWD="
rst.open
Set rst.activeconnection = cnn

rst.LockType = adLockOptimistic
rst.CursorType = adOpenKeyset
rst.open SQL
Set exesql = rst
Set rst = Nothing
Set cnn = Nothing
end function

应该错在这里(红色标志), 提醒:1。 用DSN 做连接源,确定你有没有配置“数据源 (ODBC)”(控制面板中配置)?
2。 Acess建议用Microsoft.JET 做连接源方便点。
给你个例子看带加密的。
Public Function TransactSQL(ByVal sql As String) As adodb.Recordset
Dim con As adodb.Connection
Dim rs As adodb.Recordset
Dim strConnection As String
Dim strArray() As String
Set con = New adodb.Connection
Set rs = New adodb.Recordset
On Error GoTo TransactSQL_Error
strConnection = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & App.Path & "\data\EMSDB.mdb;Jet OLEDB:Database password=" & DBPWDstr
strArray = Split(sql)
con.Open strConnection
If StrComp(UCase$(strArray(0)), "select", vbTextCompare) = 0 Then
rs.Open Trim$(sql), con, adOpenKeyset, adLockOptimistic
Set TransactSQL = rs
iflag = 1
Else
con.Execute sql
iflag = 1
End If
TransactSQL_Exit:
Set rs = Nothing
Set con = Nothing
Exit Function
TransactSQL_Error:
MsgBox "连接错误:" & err.Description
iflag = 2
Resume TransactSQL_Exit
End Function


全国最大的 Java专业电子书免费分享[url]http:///in.asp?id=xrmao[/url]
2007-08-20 21:54
tiger111
Rank: 1
等 级:新手上路
帖 子:74
专家分:0
注 册:2007-8-19
得分:0 
谢谢,已弄好
2007-08-30 13:05



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-163799-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.311011 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved