2014-02-11 61 views
6

我已經創建了一個Excel用戶窗體來收集數據。我已將它連接到Access以轉儲數據。但是,我想每次用戶按下提交按鈕時更新Access。如何使用ID檢查記錄,然後如果記錄存在更新,如果不添加新記錄

基本上我需要Select語句來確定一個id的存在,那麼如果它不存在,我需要使用INSERT來添加新行。我對任何SQL都很新,所以任何幫助都會很棒。

這裏是我現在的代碼,我需要將它調整到ADO。

Sub Update() 
Dim cnn As ADODB.Connection 
Dim MyConn 
Dim rst As ADODB.Recordset 
Dim StrSql As String 

Set cnn = New ADODB.Connection 
MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB 

With cnn 
    .Provider = "Microsoft.ACE.OLEDB.12.0" 
    .Open MyConn 

Set rst = New ADODB.Recordset 
rst.CursorLocation = adUseServer 
rst.Open Source:="Foam", ActiveConnection:=cnn, _ 
     CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _ 
     Options:=adCmdTable 





StrSql = "SELECT * FROM Foam WHERE FoamID = " & txtMyID 
Set rst = CurrentDb.OpenRecordset(StrSql, dbOpenDynaset) 

If (rst.RecordCount = 0) Then 
    DoCmd.RunSQL "INSERT INTO Foam (ID, Part, Job, Emp, Weight, Oven) VALUES " & _ 
      "(" & txtID & ", '" & txtField1 & "', '" & txtField2 & "', '" & txtField3 & "', '" & txtField4 & "', '" & txtField5 & "');" 

End If 

' Close the connection 
    rst.Close 
    cnn.Close 
    Set rst = Nothing 
    Set cnn = Nothing 


End With 

End Sub 

回答

3

我修改您的代碼示例就足以得到它的工作我的系統上,並在Excel 2007

測試這個版本當我使用的值對於與現有記錄的id匹配的lngId,該記錄在記錄集中打開,我可以更新其字段的值。

lngId與現有記錄的id不匹配時,記錄集打開爲空[(.BOF And .EOF) = True]。在那種情況下,我添加一條新記錄並添加字段值。

Sub Update() 
    Const TARGET_DB As String = "database1.mdb" 
    Dim cnn As ADODB.Connection 
    Dim MyConn As String 
    Dim rst As ADODB.Recordset 
    Dim StrSql As String 
    Dim lngId As Long 

    Set cnn = New ADODB.Connection 
    MyConn = ThisWorkbook.Path & Application.PathSeparator & TARGET_DB 
    With cnn 
     .Provider = "Microsoft.ACE.OLEDB.12.0" 
     .Open MyConn 
    End With 

    lngId = 4 
    StrSql = "SELECT * FROM tblFoo WHERE id = " & lngId 
    Set rst = New ADODB.Recordset 
    With rst 
     .CursorLocation = adUseServer 
     .Open Source:=StrSql, ActiveConnection:=cnn, _ 
       CursorType:=adOpenDynamic, LockType:=adLockOptimistic, _ 
       Options:=adCmdText 
     If (.BOF And .EOF) Then 
      ' no match found; add new record 
      .AddNew 
      !ID = lngId 
      !some_text = "Hello World" 
     Else 
      ' matching record found; update it 
      !some_text = "Hello World" 
     End If 
     .Update 
     .Close 
    End With 

    Set rst = Nothing 
    cnn.Close 
    Set cnn = Nothing 
End Sub 
+0

對於Excel 2013,是否需要進行重大更改?我得到「在請求的名稱或序號對應的集合中找不到錯誤3265項目。」 !some + text =「Hello World」出錯這個錯誤 – Crabara

+0

'some_text'是我的'tblFoo'表中的一個字段的名字。替換您的字段名稱。如果該名稱包含空格,標點符號或字母,數字或'_'以外的任何字符,請將字段名稱括在方括號中,如下所示......'[您的字段]' – HansUp

+0

謝謝!我實際上更新了我的訪問數據庫從一個Excel用戶表單!現在我只需要從文本字段填充對數據庫的更新,而不是靜態值。但是你讓我非常接近!謝謝! – Crabara

1

使用VBA,下面是如何查詢某個ID以檢查它是否存在的示例。如果它不然後通過INSERT聲明添加:

Dim rs As DAO.Recordset 
Dim StrSql As String 

StrSql = "SELECT * FROM MyTable WHERE ID = " & txtMyID 
Set rs = CurrentDb.OpenRecordset(StrSql, dbOpenDynaset) 

If (rs.RecordCount = 0) Then 
    DoCmd.RunSQL "INSERT INTO MyTable (ID, Field1, Field2) VALUES " & _ 
      "(" & txtID & ", '" & txtField1 & "', '" & txtField2 & "');" 
End If 
+0

我該如何改變101,無論,男人)到新增加的數據? – Crabara

+0

我可以添加像(TextBox1.Value,TextBox2.Value .....)嗎? – Crabara

+0

** @ user3297842 **,是的,應該工作 – Linger

相關問題