从具有多值的字段中操作数据
Manipulating data from a field with multi-valuable
我有一个 table,其中包含一个包含多值的字段,如下所示:
在表单中,我想让用户在文本框中输入 NCR_Num,然后使用 VBA 进行一些输入验证,然后将其添加到 "text_Pool",如下所示:
此 Text_Pool 以 NCR_Num 作为控制源,因此如果添加或删除了 NCR 号码,它将自动更新 NCR_Num 字段。
我不太确定如何处理这种数据类型。
在 VBA 中,我无法从 Text_Pool 中获取值,因为我认为我需要将其视为数组或记录集
下面是我尝试记录集尝试的示例,但显然我对自己在做什么感到很困惑:
Public Function get_NCR_Num(SCAR_Num As Integer) As Integer()
Dim dbsMain As DAO.Database
Dim rstMain As DAO.Recordset
Dim childRS As Recordset
Dim sSearchField, sCriteria As String
Set dbsMain = CurrentDb
Set rstMain = dbsMain.OpenRecordset("tbl_SCAR", dbOpenDynaset, dbReadOnly)
Set childRS = rstMain!NCR_Num.Value
sSearchField = "[SCAR_Num]"
sCriteria = sSearchField & " = " & [SCAR_Num]
With rstMain
.MoveLast
.FindFirst (sCriteria)
With childRS
Do While (Not .EOF)
MsgBox (childRS!NCR_Num.Value)
.MoveNext
Loop
End With
End With
rstMain.Close
dbsMain.Close
Set rstMain = Nothing
Set dbsMain = Nothing
End Function
任何帮助将不胜感激!
我误解了你的问题,并用以下代码更新了答案。这应该做你想做的。将子程序 'Command_LinkNCR_Click' 中的代码替换为以下代码。
这将:(a) 验证 nbr 是否存在; (b) 如果不存在则添加; (c) 如果存在则删除;
警告!!此代码仅解决您试图克服的一个问题。但是,它会更新您在表单上查看的同一记录集,因此如果您的表单是 'Dirty'.
,则可能存在问题
试一试,如果您有任何问题,请告诉我。
私人订阅 Command_LinkNCR_Click()
昏暗的数据库为 DAO.Database
将 rsMain 调暗为 DAO.Recordset
将 rsChild 调暗为 DAO.Recordset
将 strSQL 调暗为字符串
Dim blnMatch As Boolean
If IsNull(Me.Text_NCR) Or Me.Text_NCR = "" Then
MsgBox "No value entered for NCR_Num", vbOKOnly, "Missing Value"
Exit Sub
End If
blnMatch = False
Set dbs = CurrentDb
' Only need to work on the current record
strSQL = "select * from tbl_SCAR where SCAR_Num = " & Me!SCAR_Num & ";"
Set rsMain = dbs.OpenRecordset(strSQL, dbOpenDynaset)
If rsMain.EOF Then
' Should never happen
Else
Set rsChild = rsMain!NCR_Num.Value
If rsChild.EOF Then ' If no values yet, add this new one
MsgBox "Add item"
Else
Do While Not rsChild.EOF
' See if we have a match...
If Int(rsChild.Fields(0)) = Int(Me.Text_NCR) Then
blnMatch = True
rsChild.Delete ' Delete item
Exit Do
End If
rsChild.MoveNext
Loop
If blnMatch = False Then ' Need to Add it
rsMain.Edit
rsChild.AddNew
rsChild.Fields(0) = Me.Text_NCR
rsChild.Update
rsMain.Update
End If
End If
End If
'rsChild.Close
rsMain.Close
dbs.Close
Set rsMain = Nothing
Set rsChild = Nothing
Set dbs = Nothing
Me.Refresh
End Sub
我有一个 table,其中包含一个包含多值的字段,如下所示:
此 Text_Pool 以 NCR_Num 作为控制源,因此如果添加或删除了 NCR 号码,它将自动更新 NCR_Num 字段。
我不太确定如何处理这种数据类型。 在 VBA 中,我无法从 Text_Pool 中获取值,因为我认为我需要将其视为数组或记录集
下面是我尝试记录集尝试的示例,但显然我对自己在做什么感到很困惑:
Public Function get_NCR_Num(SCAR_Num As Integer) As Integer()
Dim dbsMain As DAO.Database
Dim rstMain As DAO.Recordset
Dim childRS As Recordset
Dim sSearchField, sCriteria As String
Set dbsMain = CurrentDb
Set rstMain = dbsMain.OpenRecordset("tbl_SCAR", dbOpenDynaset, dbReadOnly)
Set childRS = rstMain!NCR_Num.Value
sSearchField = "[SCAR_Num]"
sCriteria = sSearchField & " = " & [SCAR_Num]
With rstMain
.MoveLast
.FindFirst (sCriteria)
With childRS
Do While (Not .EOF)
MsgBox (childRS!NCR_Num.Value)
.MoveNext
Loop
End With
End With
rstMain.Close
dbsMain.Close
Set rstMain = Nothing
Set dbsMain = Nothing
End Function
任何帮助将不胜感激!
我误解了你的问题,并用以下代码更新了答案。这应该做你想做的。将子程序 'Command_LinkNCR_Click' 中的代码替换为以下代码。 这将:(a) 验证 nbr 是否存在; (b) 如果不存在则添加; (c) 如果存在则删除;
警告!!此代码仅解决您试图克服的一个问题。但是,它会更新您在表单上查看的同一记录集,因此如果您的表单是 'Dirty'.
,则可能存在问题试一试,如果您有任何问题,请告诉我。
私人订阅 Command_LinkNCR_Click() 昏暗的数据库为 DAO.Database 将 rsMain 调暗为 DAO.Recordset 将 rsChild 调暗为 DAO.Recordset 将 strSQL 调暗为字符串 Dim blnMatch As Boolean
If IsNull(Me.Text_NCR) Or Me.Text_NCR = "" Then
MsgBox "No value entered for NCR_Num", vbOKOnly, "Missing Value"
Exit Sub
End If
blnMatch = False
Set dbs = CurrentDb
' Only need to work on the current record
strSQL = "select * from tbl_SCAR where SCAR_Num = " & Me!SCAR_Num & ";"
Set rsMain = dbs.OpenRecordset(strSQL, dbOpenDynaset)
If rsMain.EOF Then
' Should never happen
Else
Set rsChild = rsMain!NCR_Num.Value
If rsChild.EOF Then ' If no values yet, add this new one
MsgBox "Add item"
Else
Do While Not rsChild.EOF
' See if we have a match...
If Int(rsChild.Fields(0)) = Int(Me.Text_NCR) Then
blnMatch = True
rsChild.Delete ' Delete item
Exit Do
End If
rsChild.MoveNext
Loop
If blnMatch = False Then ' Need to Add it
rsMain.Edit
rsChild.AddNew
rsChild.Fields(0) = Me.Text_NCR
rsChild.Update
rsMain.Update
End If
End If
End If
'rsChild.Close
rsMain.Close
dbs.Close
Set rsMain = Nothing
Set rsChild = Nothing
Set dbs = Nothing
Me.Refresh
End Sub