更新上一行

时间:2017-10-05 15:43:34

标签: sql excel vba

我有一个Excel文件,其中包含一些我要导出到Access数据库的数据。在C栏中,我有一个名为' Description'的字段。通常这个字段只占用一个单元格,但它可能会发生更长的时间。

enter image description here

在这种情况下,例如,AP.01有5行描述。如何用下一行更新第一行?

Public Sub updateDB(ByVal PathDB As String, str As String, id As Integer)
    Dim db As New cDB
    Dim v As New cVoce
    Dim rs As ADODB.Recordset = db.RecordSet
    v.Description = str
    db.connetti_DB(PathDB)
    db.get_rs("UPDATE Voice SET Description = '" + v.Description + "' WHERE id= '"+id+"'")

End Sub

Public Function get_rs(ByVal query As String) As ADODB.Recordset
    If db Is Nothing Then rs = Nothing : Return rs

    rs = New ADODB.Recordset
    rs.CursorType = ADODB.CursorTypeEnum.adOpenStatic
    rs.LockType = ADODB.LockTypeEnum.adLockOptimistic

    rs.Open(query, db)
    Return rs
End Function

此代码不起作用,因为我更新了当前行,因此UPDATE指令无用。我该如何修复我的代码?

编辑我在这里发布了For循环

For r = 2 To grid.RowCount - 1

        vett = Split(grid(r, 1).Text)
        total = UBound(Split(grid(r, 1).Text, "."))
        If grid(r, 1).Text <> "" Then
            Select Case total
                Case 0 
                    Dim chapter As New cChapter
                    flag = 1
                    id = id + 1
                    chapter.Cod = grid(r, 1).Text.Substring(0, 1)
                    chapter.Description = grid(r, 3).Text

                    If Left(vett(0), 1) >= Chr(65) And Left(vett(0), 1) <= Chr(90) Then
                        chapter.Cod = Left(vett(0), 1)
                        oldChap = chap.Cod
                        If chapter.Cod <> oldCap Then
                            chapters.Add(chapter)
                        End If
                    End If
                    chapters.Add(chapter)
                    stringChap = chap.Description

                    Dim par As New cParagraph
                    If Left(vett(0), 2) >= Chr(65) And Left(vett(0), 2) <= Chr(90) Then
                        par.Cod = Left(vett(0), 2)
                        par.Cod_Chapter = Left(vett(0), 1)
                        oldPar = par.Cod
                        If par.Cod <> oldPar Then
                            paragraphs.Add(par)
                        End If
                    End If
                    If grid(r, 3).Text.Length > 255 Then
                        par.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
                    Else
                        par.Description = grid(r, 3).Text.ToString
                    End If
                    paragraphs.Add(par)
                    stringPar = par.Description

                Case 1 

                    flag = 2
                    id = id + 1
                    c_Voc = voc.Cod_Chapter
                    p_Voc = voc.Cod_Paragraph
                    voc.Cod_Chapter = grid(r, 1).Text.Substring(0, 1)
                    voc.Cod_Paragraph = grid(r, 1).Text.Split(".")(0)
                    voc.Cod_Voice = Right(vett(0), 2)
                    If grid(r, 3).Text.Length > 255 Then
                        voc.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
                    Else
                        voc.Description = grid(r, 3).Text.ToString
                        If voc.Description.EndsWith("-") Then
                            a = Replace(voc.Description, "-", "") 
                            voc.Description = a
                        End If
                    End If
                    stringVoice = voc.Description
                    voices.Add(voc)
                    voices.Save_DB(dbDest)

                Case 2
                    flag = 3
                    id = id + 1
                    sVoice = New cVoice
                    oldSvoice = voice.Cod_SVoice
                    sVoice.Cod_SVoice = Left(vett(0), 2)

                    If sVoice.Cod_SVoce <> oldSvoice Then
                        voices.Add(sVoice)
                        voices.Save_DB(dbDest)
                    End If

                    If grid(r, 3).Text.Length > 255 Then
                        sVoice.Description = grid(r, 3).Text.ToString.Substring(0, 252) + "..."
                    Else
                        sVoice.Description = grid(r, 3).Text
                    End If
                    stringSvoice = sVoice.Description

                    sVoice.Cod_Voce = Left(vett(0), 5)
                    sVoice.Price1 = grid(r, 12).Text
                    sVoice.Price2 = sVoice.Price1
                    sVoice.UniMi = grid(r, 11).Text
                    sVoce.Sep = "."
                    voices.Add(sVoce)
                    voices.Save_DB(dbDest)

            End Select

        Else
            If flag = 1 Then
                stringChap = grid(r, 3).Text
                chap.Description = stringChap & grid(r, 3).Text
                stringPar = grid(r, 3).Text
                paragraph.Description = stringPar & grid(r, 3).Text
            End If

            If flag = 2 Then
                stringVoice = grid(r, 3).Text
                voc.Description = voc.Description & stringVoice
                voices.updateDB(dbDest, stringVoice, id)
                voices.Add(voc)
            End If

            If flag = 3 Then
                stringSvoice = grid(r, 3).Text
                sVoice.Description = stringSvoice & grid(r, 3).Text
                voices.Add(sVoice)
            End If
            chapter.Save_DB(dbDest)
            paragraph.Save_DB(dbDest)
            voice.Save_DB(dbDest)
        End If
    Next

EDIT2 我声明id为Integer,当Code列有值时,id = id + 1。通过这种方式,我总是知道我必须修改哪一行。我还修改了updateDB(现在我使用了3个参数),并在我的查询中添加了WHERE条件。尽管有更新,但没有任何改变

1 个答案:

答案 0 :(得分:1)

在数据库中,你不能存储没有PrimaryKey的记录(实际上你可以,但这是个坏主意)。因为在你的解决方案中,id实际上是Excel行号(对不起,如果我不正确,但看起来像代码),将来很难维护它(如果有人添加或删除描述行)。最好将id列更改为文本并将代码用作PK。 然后可以通过两种方式解决存储描述:

1)将包含description的所有行连接到1个变量中,在其间添加vbNewLine并将其存储在description字段中。

2)更多关系,但也更复杂 - 创建第二个表用于描述PK,即自动编号,ForeignKey代码引用主表。维护将非常复杂。不值得努力。

代码的变化量非常大,很抱歉,我不会提供固定的代码,但我希望这个想法很明确。

BTW:您的帖子中描述了未更新说明的原因。只有当代码存在时才会增加id,因此第一组中的每个描述字段都有id = 1.代码中最简单的修复是创建2个更新语句 - 一个用于代码行

UPDATE Voice SET Description = '" + v.Description + "' WHERE id=    '"+id+"'

第二个没有代码的行:

UPDATE Voice SET Description = Description + CHAR(13) + CHAR(10) + '" + v.Description + "' WHERE id= '"+id+"'