我对VBA很新,所以试图了解与C#的差异。 我目前有一个Sub进行批量导入,只对从数据表子表单中选择的单行/记录起作用。
Private Sub cmdImport_Click()
Dim strBatchID As String
On Error GoTo NoBatch
Me.subClaimsToProcessLookup.Form.Controls.Item("txtBatchID").SetFocus
strBatchID = Me.subClaimsToProcessLookup.Form.Controls.Item("txtBatchID").Text
GoTo ContinueImport
NoBatch:
MsgBox "Please select a row", vbOKOnly + vbQuestion, Me.Caption
Exit Sub
ContinueImport:
If Not IsNumeric(strBatchID) Then
MsgBox "Please select a row", vbOKOnly + vbQuestion, Me.Caption
Exit Sub
End If
Dim lngUserID As Long
'...process continues here...
这只会处理用户选择的任何行...现在,用户只需点击一下按钮即可处理数据表中的所有行。
我想知道For Each是否适用于此,而不是必须使用记录导航 (移动下一个,等等),我不是很熟悉...但是对于我来说,每个人都是一个简单的概念来自C#。 如: Private Sub cmdImport_Click()
Dim strBatchID As String
On Error GoTo NoBatch
' Add loop here to go through each item in the datasheet view and process it.
For Each Me.subClaimsToProcessLookup.Form.Controls.Item("txtBatchID").Text in Me.subClaimsToProcessLookup.Form.Controls
Me.subClaimsToProcessLookup.Form.Controls.Item("txtBatchID").SetFocus
strBatchID = Me.subClaimsToProcessLookup.Form.Controls.Item("txtBatchID").Text
GoTo ContinueImport
Next
NoBatch: MsgBox“请选择一行”,vbOKOnly + vbQuestion,Me.Caption '退出子
继续ContinueImport: 如果不是IsNumeric(strBatchID)那么 MsgBox“请选择一行”,vbOKOnly + vbQuestion,Me.Caption '退出子 继续 结束如果
Dim lngUserID As Long
....process continues here....
另外,如果For Each会正确地执行此操作,我是否正确设置了'Continue For'流程?
总之,我正在寻找1)确认Form.Controls可以用作For Each中的集合; 2)我为For Each
进行了正确的流量控制(继续)答案 0 :(得分:0)
也许:
Private Sub cmdImport_Click()
''The recordset from the subform
Set rs = Me.subClaimsToProcessLookup.Form.Recordset
Do While Not rs.EOF
''Each record in the subform recordset
sId = rs!ID
MsgBox sId ''Just to check
''Do stuff with id
rs.MoveNext
Loop
End sub