我有一个问题,我需要为 ms access 2016 创建 vba 代码,它将表 1 的内容复制到表 2
如果表 2 中有重复项,它会显示有关重复项的消息以及是否继续或中止的问题。
我有这个 vba 代码,但我不知道如何将它转换为我想要的工作方式 我在图片上遇到了这个错误
Private Sub txtVWI_BeforeUpdate(Cancel As Integer)
On Error GoTo Err_txtVWI_BeforeUpdate
Dim intResponse As Integer
Dim strTable As String
Dim strFind As String
Dim strSQL As String
Dim rst As ADODB.Recordset
Dim Conn As ADODB.Connection
Set Conn = CurrentProject.Connection
Set rst = New ADODB.Recordset
strTable = "Główna"
strSQL = "SELECT Count(Główna.Data/Godzina) AS Duplikaty" & _ " FROM Główna" & _ " GROUP BY Główna.Data/Godzina" & _ " HAVING Count(Główna.Data/Godzina)>1"
Set rs = db.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then strSQL = "INSERT INTO Główna" & _ " SELECT Tymczasowa.*" & _ " FROM Tymczasowa" DoCmd.RunSQL (strSQL)
rst.Open strSQL, Conn
If rst(0) > 0 Then ' duplikaty znalezione.
If Me.NewRecord Then
intResponse = MsgBox("Ten zestaw danych już istnieje" & vbCrLf & "chcesz zduplikować zestaw danych?", vbYesNo)
If intResponse = vbNo Then
Me.Undo
End If
End If
End If
Exit_txtVWI_BeforeUpdate:
Exit Sub
rst.Close
Conn.Close
Set rst = Nothing
Set Conn = Nothing
Err_txtVWI_BeforeUpdate:
MsgBox Err.Description
Resume Exit_txtVWI_BeforeUpdate
End Sub
最佳答案
如果您打算使用连接和行继续字符,那么实际上应该在下一行继续。
表名有特殊性。使用[]
来分隔。可以简化SQL语句。
没有看到声明和设置的 db
变量。一次性使用真的不需要它。应声明 rs
变量。
Dim rs As DAO.Recordset
strSQL = "SELECT Count(*) AS Duplikaty" & _
" FROM Główna" & _
" GROUP BY [Data/Godzina]" & _
" HAVING Count(*)>1"
Set rs = CurrentDb.OpenRecordset(strSQL)
If rs.RecordCount = 0 Then strSQL = "INSERT INTO Główna" & _
" SELECT Tymczasowa.*" & _
" FROM Tymczasowa"
建议不要在命名约定中使用标点符号/特殊字符。
关于vba - MS Access复制表到另一个表VBA代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58162867/