【发布时间】:2020-01-24 04:03:34
【问题描述】:
我正在尝试在记录集的最后一行插入一些值,在这种情况下,记录集是用作我的数据库的 Excel 文件。我有下面的代码,用于将文本框的值插入到 excel 记录集的最后一行。但是,它没有创建插入值的新表行。
Sub CreaterRow()
Dim strFile As String
Dim strConnect As String
Dim strSQL As String
Dim lngCount As Long
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
strFile = "C:\Excel\Test.xlsx"
strConnect = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strFile & _
";Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
cnn.Open ConnectionString:=strConnect
strSQL = "SELECT [ID] FROM [Sheet1$]"
rst.Open Source:=strSQL, ActiveConnection:=cnn, CursorType:=adOpenForwardOnly, Options:=adCmdText
With rst
.AddNew
.Fields("ID").Value = tbx_ID.Value 'Inserting this in the recordset did not create a new row
.Update
End with
rst.Close
cnn.Close
End Sub
表如何自动创建一个新行,其中包含插入到最后一行的值?谢谢你。
【问题讨论】:
-
你没有在你的记录集上调用
Update -
感谢您指出这一点。我无法粘贴 '.Update' 我的问题还是一样。
-
你没有收到任何错误?
-
不,我没有收到任何错误。