Class 使用类模块将可编辑ADO记录集返回MS Access窗体

Class 使用类模块将可编辑ADO记录集返回MS Access窗体,class,ms-access,vba,ms-access-2007,ado,Class,Ms Access,Vba,Ms Access 2007,Ado,前言:我使用SQL Server 2008 R2后端和MS Access 2007作为前端 我有一个类模块,可以从SQL Server返回我想要的任何ADO记录集。然后,我可以将其分配给任何表单RecordSource属性 问题是,当我试图编辑字段时,状态栏中会显示“此表单为只读”。我希望表单是可编辑的 我有两张表格 形式实体 表单编辑 FormEntityEdit表单不使用类模块。相反,所有的代码都是在表单本身中 类模块的目的是避免冗余,并且能够使用类模块轻松地从SQL Server获取记录集

前言:我使用SQL Server 2008 R2后端和MS Access 2007作为前端

我有一个类模块,可以从SQL Server返回我想要的任何ADO记录集。然后,我可以将其分配给任何表单
RecordSource
属性

问题是,当我试图编辑字段时,状态栏中会显示“此表单为只读”。我希望表单是可编辑的

我有两张表格

  • 形式实体
  • 表单编辑
  • FormEntityEdit表单不使用类模块。相反,所有的代码都是在表单本身中

    类模块的目的是避免冗余,并且能够使用类模块轻松地从SQL Server获取记录集

    首先是我的全局模块

    'Default error message. 'eh' stands for error handler Public eh As String 'Global variables for universal use Public conn As ADODB.Connection Public rs As ADODB.Recordset Public com As ADODB.Command “默认错误消息。”eh’代表错误处理程序 公共eh作为字符串 '通用的全局变量 公共连接作为ADODB.连接 作为ADODB.Recordset的公共rs 作为ADODB.Command的公共com 第二个是类模块(名称为cADO)。 此类模块使用上面的连接对象

    Option Explicit Private Const CONST_LockType = 3 Private Const CONST_CursorType = 1 Private Const CONST_CursorLocationServer = 3 Private Const CONST_CursorLocationClient = 2 Private m_Recordset As ADODB.Recordset 'For Public Recordset function Private cSQL$ '********************************************************************** Public Function cGetRecordset(ByRef sql) As ADODB.Recordset Set m_Recordset = New ADODB.Recordset cSQL = sql cOpenRecordset Set cGetRecordset = m_Recordset End Function '********************************************************************** Public Property Set Recordset(Value As ADODB.Recordset) 'Assigns private variable a property If Not Value Is Nothing Then Set m_Recordset = Value End Property '********************************************************************** Public Property Get Recordset() As ADODB.Recordset 'Reads the recordset from the private variable and assigns to new object variable Set Recordset = m_Recordset End Property '********************************** PRIVATE SECTION ********************************** Private Sub cOpenRecordset() On Error GoTo eh 'Ensures that if a recordset is opened from previously that it closes before opening a new one If m_Recordset.State adStateClosed Then m_Recordset.Close Set m_Recordset.ActiveConnection = conn With m_Recordset .LockType = CONST_LockType .CursorType = CONST_CursorType .CursorLocation = CONST_CursorLocationClient .Source = cSQL .Open .Source End With If Not m_Recordset.EOF Then m_Recordset.MoveFirst Exit Sub eh: eh = "Error # " & Str(Err.Number) & " was generated by " & _ Err.Source & Chr(13) & Err.Description MsgBox eh, vbCritical, "Open Recordset System" End Sub '********************************************************************** Private Sub cCloseRecordset() m_Recordset.Close Set m_Recordset = Nothing End Sub '********************************************************************** Private Sub Class_Terminate() If Not (m_Recordset Is Nothing) Then Set m_Recordset = Nothing End Sub 选项显式 私有常量常量锁类型=3 Private Const_CursorType=1 私有常量常量游标位置服务器=3 私有Const Const_CursorLocationClient=2 私有m_记录集为ADODB.Recordset '用于公共记录集函数 私有cSQL$ '********************************************************************** 作为ADODB.Recordset的公共函数cGetRecordset(ByRef sql) Set m_Recordset=New ADODB.Recordset cSQL=sql cOpenRecordset Set cGetRecordset=m_记录集 端函数 '********************************************************************** 公共属性集记录集(值为ADODB.Recordset) '为私有变量指定属性 如果Not Value为Nothing,则设置m_Recordset=Value 端属性 '********************************************************************** 公共属性获取记录集()作为ADODB.Recordset '从专用变量读取记录集,并将其分配给新的对象变量 Set Recordset=m_记录集 端属性 “******************************************私人部门********************************** 专用子副本记录集() 关于错误转到eh '确保如果从以前打开记录集,则在打开新记录集之前,该记录集将关闭 如果m_Recordset.State adstate已关闭,则m_Recordset.Close Set m_Recordset.ActiveConnection=conn 使用m_记录集 .LockType=CONST\u LockType .CursorType=CONST_CursorType .CursorLocation=CONST_CursorLocation客户端 .Source=cSQL .开源 以 如果不是m_Recordset.EOF,则m_Recordset.MoveFirst 出口接头 呃, eh=“Error#”&Str(Err.Number)&”由“&”生成_ 错误来源、错误(13)和错误描述 MsgBox eh,vbCritical,“打开记录集系统” 端接头 '********************************************************************** 私有子记录集() m_记录集。关闭 Set m_记录集=无 端接头 '********************************************************************** 私有子类_Terminate() 如果不是(m_记录集为Nothing),则设置m_记录集=Nothing 端接头 第三个是MYFormEntities表单背后的代码(使用cADO类模块)

    选项显式 Dim db As cADO '********************************************************************** 私有子表单_当前() 负荷表 端接头 '********************************************************************** 专用子表单_加载() 设置db=新的cADO 获取记录源 端接头 '********************************************************************** 私有子FetchRecordSource() db.cGetRecordset(“从dbo.Entities中选择*) Set Forms(“fEntities”).Recordset=db.Recordset 端接头 第四个也是最后一个是表单实体编辑表单背后的代码(此表单不使用类模块,我可以编辑它)

    选项比较数据库 选项显式 作为新ADODB.Recordset的Dim R实体 '********************************************************************** 专用子表单_加载() 获取记录源 端接头 '********************************************************************** 私有子FetchRecordSource() 设置rsEntity.ActiveConnection=conn '设置主窗体的记录源 坚定地 .LockType=adlockType .CursorType=adOpenKeyset .CursorLocation=adUseClient .Source=“从dbo.Entities中选择* .开源 以 设置表单(“fentiesedit”)。记录集=实体 端接头 '********************************************************************** 私人分公司() 真实,接近 端接头 如果Access Jet SQL可以执行SQL,我会将此表单绑定到链接表。然而,我使用的是Jet SQL无法实现的分层(递归)查询,因此我不得不绕过将表单绑定到链接表的想法

    我已将表单上的.Allow Edits和.allowaditions设置为true

    我还尝试将ADO记录集上的.LockType更改为

  • adOpenKeyset和
  • 无动力
  • 我的锁型是Adlock

    如您所见,属性设置正确,可以编辑我返回的记录集

    我知道这是真的,因为当我使用具有相同属性的FormEntitiesEdit表单时,它允许我编辑字段。但是当我使用Modu类时 Option Explicit Dim db As cADO '********************************************************************** Private Sub Form_Current() LoadTab End Sub '********************************************************************** Private Sub Form_Load() Set db = New cADO FetchRecordSource End Sub '********************************************************************** Private Sub FetchRecordSource() db.cGetRecordset ("SELECT * FROM dbo.Entities") Set Forms("fEntities").Recordset = db.Recordset End Sub Option Compare Database Option Explicit Dim rsEntity As New ADODB.Recordset '********************************************************************** Private Sub Form_Load() FetchRecordSource End Sub '********************************************************************** Private Sub FetchRecordSource() Set rsEntity.ActiveConnection = conn 'Sets the record source for the main form With rsEntity .LockType = adLockOptimistic .CursorType = adOpenKeyset .CursorLocation = adUseClient .Source = "SELECT * FROM dbo.Entities" .Open .Source End With Set Forms("fEntitiesEdit").Recordset = rsEntity End Sub '********************************************************************** Private Sub CloseConn() rsEntity.Close End Sub