如何适应新代码行的插入

时间:2017-08-17 12:33:27

标签: access-vba

我在下面有以下代码,它按顺序构建/创建基于Value字段(其中包含数据)的描述。例如,我有一个表格,其中包含以下字段...... 项目 分类 属性名称 值 UOM 状态

我尝试做的是插入一行代码,该代码行只会在Value字段(包含数据)中构建描述,其中Status字段= Active。状态字段信息是"活动"或者"外国"。目前,如果状态字段显示"外国"代码还将构建这些记录的描述,我刚刚学到的这些记录不应包括在内。

任何帮助将不胜感激!同样,下面是代码......

Sub SD()
DoCmd.SetWarnings False

Dim db As DAO.Database
Dim rsMara, rs_cou As DAO.Recordset
Dim rs, rs2, x As DAO.Recordset
Dim SD, strAttribute, strValue, sdvalue As String
Dim seq, d, count1 As Integer
Set db = CurrentDb

db.Execute ("UPDATE Item_Template SET Description = '', Long_Description = '';")
Set rsMara = db.OpenRecordset("select * from [Item_Template] order by [Item]")

While Not rsMara.EOF
If rsMara.Fields("Classification") & "" <> "" Then
SD = UCase(rsMara.Fields("Classification")) & ": "
seq = 1
Set rs = db.OpenRecordset("select * from All_Item_Attributes where [Item] = '" & rsMara.Fields("Item") & "' and [Value] & '' <> '' order by [Item],[Sequence (Cls Attribute Mapping)]")

While Not rs.EOF
If (rs.Fields("UOM") Like "IN*" Or rs.Fields("UOM") Like "O*") Then
     SD = SD & rs.Fields("Value") & " " & UCase(rs.Fields("UOM")) & ", "
Else
     SD = SD & rs.Fields("Value") & " " & UCase(rs.Fields("UOM")) & ", "
End If
rs.MoveNext
Wend
        SD = Trim(SD)
        rsMara.Edit
        SD = Trim(Mid(SD, 1, Len(SD) - 1))
        rsMara.Fields("Description") = Trim(SD)
        rsMara.Fields("Description") = Replace(rsMara.Fields("Description"), " ,", ", ")
        rsMara.Fields("Description") = Replace(rsMara.Fields("Description"), "  ", " ")
        rsMara.Update
End If
DoEvents
rsMara.MoveNext
Wend
End Sub

1 个答案:

答案 0 :(得分:0)

您不能说明字段Status所在的表格。
您有两行代码打开记录集,因此它将是以下代码行之一:

Set rsMara = db.OpenRecordset("select * from [Item_Template] WHERE Status='Active' order by [Item]")  

Set rs = db.OpenRecordset("select * from All_Item_Attributes where [Item] = '" & rsMara.Fields("Item") & "' and [Value] & '' <> '' AND Status='Active' order by [Item],[Sequence (Cls Attribute Mapping)]")

基本上将Status='Active'添加到WHERE子句中。如果您的Status字段是查找字段,则此功能无法运行,在这种情况下,您需要使用将其链接到状态类型表的外键值。例如Status=1