2017-10-05 51 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