2017-10-05 46 views
0

我有一个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 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作为整数和代码列都有一个值,则ID = ID + 1。这样我总是知道我必须修改哪一行。我也修改updateDB(现在我使用3个参数),并在查询中添加了WHERE条件。尽管更新,没有任何改变

+0

首先将“Option Explicit”放在模块上并尝试调试它。它会告诉你代码的哪些部分不正确。 – Vityata

+0

但我知道错误在哪里。当我打电话给updateDB时,我进入了For,如果我的索引是= 3,我更新第三行。我不知道如何更新前一行。我的代码只适用于当前行 – Matteo

回答

1

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

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

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

代码变化量很大,所以对不起,我不会提供固定的代码,但我希望想法很清楚。

顺便说一句:描述未更新的原因在你的文章中有描述。您正在加大ID只有当代码存在,从第一组所以每一个描述字段具有ID = 1。在你的代码最简单的解决方法是创建2个update语句 - 一个与代码行

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

二一行用于无代码:

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

id的增量是正确的。如果我没有像PK那样的身份证,我必须声明,对吧?我会尝试修改我的代码并提示 – Matteo

+0

这不是必须的,而是一种很好的做法。而代码似乎在你的情况下更可行的PK(未来也许有人会重新排列行?)。但是id也行。最主要的是,在1个数据库记录中,您应该存储来自excel的完整数据(包括许多描述行)或考虑更复杂的数据模型。 – smartobelix