Excel工作簿作为数据库,插入更新问题

时间:2016-04-21 16:06:10

标签: vba excel-2010

有一个excel工作簿,用作保存在共享文件夹中的数据库。用户具有其他工作簿,其具有允许插入和更新到数据库中的用户表单。

我使用connection.Execute方法进行插入和更新,并使用recordsaffected进行确认。

虽然recordsaffected给出正确的,但有时插入或更新永远不会发生。

我该怎么做才能解决这个问题?

Set oCn = CreateObject("ADODB.Connection")
oCn.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & shtSetting.Range("filePath").Value & ";" & _
"Extended Properties=Excel 12.0;"


strSQL = "Insert Into [Sheet1$] (" & _
"sr_no, process,employee_id,employee_name) Values ( " & _
"'" & sr_no & "','" & process & "','" & emp_id & "','" & emp_name & "')"

oCn.Execute strSQL, r_status
oCn.Close

Set oCn = Nothing

1 个答案:

答案 0 :(得分:-1)

Unless there's a strong reason to store the data in an Excel spreadsheet, though, I'd consider offloading the database management portion of it to an Access database (even if you don't have Access, you can still create an mdb/accdb file). If nothing else, that's going to give you access to indexing, which can improve performance tremendously. You can still mirror the Access data in Excel by using data connections (or other techniques).

If you are stuck with doing everything in Excel, one (potentially slow) solution is to query the sheet once you've performed your update, to confirm that it has made the change that you expect. If you're doing an insert, you may be able to get away with just counting the records, but it's definitely safer to look for the exact value you just inserted.

Also: if the users are making multiple updates, you might want to consider keeping the connection open until the form closes. (Though I don't recall if this causes locking issues with an Excel backend or not. It would generally improve performance in Access.)