Private Sub Form_DblClick(Cancel As Integer)
Dim cn As New ADODB.Connection
Dim rs As New ADODB.Recordset
Dim sql As String
Set cn = CurrentProject.Connection
sql = "select * from tblGSdengji"
rs.Open sql, cn, adOpenKeyset, adLockPessimistic, 1
' rs.AddNew
With Forms!frmGSdengji1.Form
!cboxiaozu = rs("小组")
!txtpinghao = rs("品号")
!txtgj = rs("工件")
!cbogx = rs("工序")
!cbogr = rs("操作工")
!txtNo.SetFocus
rs.Update
rs.Close
cn.Close
Set cn = Nothing
这个事情很简单嘛,你在主窗体上建一个按钮,然后写如下代码:
Private Sub 复制_Click()
Dim ctls As Controls
Dim ctl As Control
Set ctls = Me.sub0.Form.Controls
For Each ctl In ctls
If ctl.ControlType <> acLabel Then
If ctl.Name <> "编号" Then
Me.Controls(ctl.Name).Value = ctl.Value
End If
End If
Next ctl
End Sub
如果想用子窗体某字段的双击事件,则可以:
Dim ctls As Controls
Dim ctl As Control
Set ctls = Me.Controls
For Each ctl In ctls
If ctl.ControlType <> acLabel Then
If ctl.Name <> "编号" Then
Me.Parent.Form.Controls(ctl.Name).Value = ctl.Value
End If
End If
Next ctl
如果想对子窗体所有控件的双击事件均加载复制功能,则可以:
Function AllDblClick()
Dim ctls As Controls
Dim ctl As Control
Set ctls = Me.Controls
For Each ctl In ctls
If ctl.ControlType <> acLabel Then
If ctl.Name <> "编号" Then
Me.Parent.Form.Controls(ctl.Name).Value = ctl.Value
End If
End If
Next ctl
Me.Parent.Form.Requery
Me.Requery
Me.Parent.SetFocus
DoCmd.RunCommand acCmdRecordsGoToNew
End Function
Private Sub Form_Load()
Dim ctls As Controls
Dim ctl As Control
Set ctls = Me.Controls
For Each ctl In ctls
If ctl.ControlType <> acLabel Then
ctl.OnDblClick = "=AllDblClick()"
End If
Next ctl
End Sub
加一句排除某些字段即可:
Function AllDblClick()
Dim ctls As Controls
Dim ctl As Control
Set ctls = Me.Controls
For Each ctl In ctls
If ctl.ControlType <> acLabel Then
If ctl.Name <> "编号" Then
If ctl.Name <> "登记日期" Then
Me.Parent.Form.Controls(ctl.Name).Value = ctl.Value
End If
End If
End If
Next ctl
Me.Parent.Form.Requery
Me.Requery
Me.Parent.SetFocus
DoCmd.RunCommand acCmdRecordsGoToNew
End Function
Private Sub 表_AfterUpdate()
Dim i As Long
For i = 0 To Me.表.ColumnCount - 1
Me.Controls(Me.表.Column(i, 0)).Value = Me.表.Column(i)
Next
End Sub
Private Sub 确定_Click()
Call 编辑(Me.选项.Value)
End Sub
Sub 编辑(num As Long)
Dim rs As New ADODB.Recordset
Dim ssql As String
Dim i As Long
ssql = "select * from tblGSdengji where 编号=" & Nz(Me.编号.Value, 0)
rs.Open ssql, CurrentProject.Connection, adOpenKeyset, adLockOptimistic
If num = 1 Then
rs.AddNew
End If
For i = 1 To rs.Fields.Count - 1
rs(i).Value = Me.Controls(rs(i).Name).Value
Next
rs.Update
rs.Close
Set rs = Nothing
Me.表.Requery
End Sub