我有一个Employee类,我在其中调用数据库连接模块以进行查询。
由于我在窗体顶部设置了empObj
,因此我无法理解“我正在获取”编译错误:预期的函数或变量“。
我想从Employee.getEmployee方法返回值。有人可以告诉我如何从表单中调用Employee类吗?我必须先导入课程吗?我不相信VB6支持Imports
关键字。
这是我的表格:
Option Explicit
Private empObj As New Employee
Private Sub Form_Load()
'For testing only
MsgBox (empObj.getEmployee)
End Sub
这是我的课程:
Public Sub getEmployee()
'ConnectSQL is a database connection
return ConnectSQL
End Sub
这是模块:
Public Function ConnectSQL()
Set SQLMyconn = New ADODB.Connection
Set SQLRecset = New ADODB.Recordset
SQLMyconn.Open "Driver={MySQL ODBC Client Interface};ServerName=localhost;dbq=@testdb"
End Function
您想要做的事情的基本外壳是这样的:
Option Explicit
Private empObj As Employee
Private Sub Form_Load()
Set empObj = New Employee
MsgBox empObj.getEmployee
End Sub
Public Function getEmployee() As String
getEmployee = ConnectSQL
End Function
Public Function ConnectSQL() As String
Set SQLMyconn = New ADODB.Connection
Set SQLRecset = New ADODB.Recordset
SQLMyconn.Open "Driver={MySQL ODBC Client Interface};ServerName=localhost;dbq=@testdb"
ConnectSQL = "data from your DB lookup"
End Function
几乎每一行都与您发布的内容不同,因此请仔细查看代码。
编辑:
基于注释,这是修改代码以返回连接对象的方法:
Option Explicit
Private empObj As Employee
Private Sub Form_Load()
Set empObj = New Employee
MsgBox empObj.getEmployee
Dim MyConnection As ADODB.Connection
Set MyConnection = ConnectSQL()
'you can grab and use the connection in your form, too.
End Sub
Public Function getEmployee() As String
Dim MyConnection As ADODB.Connection
Set MyConnection = ConnectSQL()
'use the connection to grab data
getEmployee = "data from your DB lookup"
End Function
Public Function ConnectSQL() As ADODB.Connection
Set ConnectSQL = New ADODB.Connection
ConnectSQL.Open "Driver={MySQL ODBC Client Interface};ServerName=localhost;dbq=@testdb"
End Function
完成连接后,请不要忘记关闭连接。总结更改: