|
本帖最后由 luhao 于 2009-10-19 21:37 编辑
以下的自定义函数不知错在那里,在控件中修改后就是不能保存到数据表中,(与控件的OnlyRead设置无关)
Private Sub Command0_Click()
Call AutoctGrid(ctGrid1, "MRP排产备单档案")
End Sub
Public Function AutoctGrid(xctGrid As Control, DBTable As String)
xctGrid.ClearColumns
xctGrid.ClearItems
'定义数据表
Dim rs As New ADODB.Recordset
Dim cn As New ADODB.Connection
Set cn = CurrentProject.Connection
rs.ActiveConnection = cn
rs.CursorType = adOpenDynamic
rs.LockType = adLockOptimistic
' Set rs = CurrentDb.OpenRecordset(DBTable)
rs.Open DBTable
Dim i As Integer
Dim n As Integer
Dim l As Integer
i = 0
xctGrid.AddItem ""
For n = 1 To rs.Fields.Count
'添加字段名
xctGrid.AddColumn (rs.Fields(n - 1).Name), 100
Next n
While Not rs.EOF
'显示数据
xctGrid.AddItem ""
For l = 0 To rs.Fields.Count - 1
xctGrid.CellText(i, l) = IIf(IsNull(rs.Fields(l).Value) = True, "", rs.Fields(l).Value)
Next l
rs.MoveNext
i = i + 1
Wend
Set rs = Nothing
End Function |
|