编译错误:属性的使用无效 如何解决???!!!
大家好,
我建立了两个工程,分别属于两个目录。
一个目录为DAL,作为存放Access数据库的操作代码,生成DLL。
一个目录为EXE,作为存放调用DLL的代码,生成exe。
DLL的代码如下:
Public strerror As String
Private Function Connect() As Variant
'建立数据库链接
On Error GoTo ErrorHandler
Dim CN As New ADODB.Connection
CN.Provider = "Microsoft.Jet.OLEDB.4.0"
CN.ConnectionTimeout = 5
CN.CursorLocation = adUseClient
CN.ConnectionString = "data source=" & App.Path & "\..\DataBase\DEFDatabase.mdb" '在此修改数据库路径
CN.Open
Set Connect = CN
Exit Function
ErrorHandler:
Set Connect = Nothing
Set CN = Nothing
strerror = Err.Description
End Function
Public Function GetRecordset(strSQL As String) As ADODB.Recordset
'形成记录集
On Error GoTo ErrorHandler
Dim CN As New ADODB.Connection
Dim rs As New ADODB.Recordset
Set CN = Connect()
If (CN Is Nothing) Then
Set GetRecordset = Nothing
Exit Function
End If
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient
rs.ActiveConnection = CN
rs.Open strSQL
Set CN = Nothing '
Set GetRecordset = rs
Exit Function
ErrorHandler:
strerror = Err.Description
Set CN = Nothing
Set rs = Nothing
Set GetRecordset = Nothing
End Function
EXE工程的代码如下:
Private Sub Command1_Click()
Dim MyDal As DllForDAL.DAL
Dim rs As New ADODB.Recordset
rs = MyDal.GetRecordset("select * from ProjTable where ProjName='" & ProjName & "'")
End Sub
结果报错,光标在EXE工程的第三行代码的rs上,提示错误:编译错误:属性的使用无效
望大家帮忙指点指点啊。谢谢大家。
[解决办法]
要在exe工程引用中加入对你的dll的部件的引用
[解决办法]
set rs = MyDal.GetRecordset("select * from ProjTable where ProjName='" & ProjName & "'")
这样呢
[解决办法]
set rs = MyDal.GetRecordset("select * from ProjTable where ProjName='" & ProjName & "'")
你可以debug到dll里面去看看是否执行顺利
[解决办法]
Public Function GetRecordset(strSQL As String) As ADODB.Recordset
'形成记录集
On Error GoTo ErrorHandler
Dim CN As New ADODB.Connection
Dim rs As New ADODB.Recordset
Set CN = Connect()
If (CN Is Nothing) Then
Set GetRecordset = Nothing
Exit Function
End If
rs.CursorType = adOpenDynamic
rs.CursorLocation = adUseClient
rs.ActiveConnection = CN
'strSQL = "select * from ProjTable where ProjName='" & ProjName & "'"
rs.Open strSQL
Set CN = Nothing '
Set GetRecordset = rs
Exit Function
ErrorHandler:
strerror = Err.Description
Set CN = Nothing
Set rs = Nothing
Set GetRecordset = Nothing
End Function
我没调试, 但好象标记为红色的那句有问题,你提前关闭了ADODB.Connection对象。
[解决办法]