vba - MS Access复制表到另一个表VBA代码

标签 vba ms-access ms-access-2010 ms-access-2016

我有一个问题,我需要为 ms access 2016 创建 vba 代码,它将表 1 的内容复制到表 2

如果表 2 中有重复项,它会显示有关重复项的消息以及是否继续或中止的问题。

我有这个 vba 代码,但我不知道如何将它转换为我想要的工作方式 我在图片上遇到了这个错误

My error

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/

相关文章:

excel - 通过 VBA 访问 DLL

sql-server - Excel 2016 复杂嵌套 IF

SQL 为嵌套选择创建别名列?

ms-access - MS Access 2010 运行时 - 缺少连续形式的鼠标右键单击上下文菜单

ms-access - 确保 Access 2000 mdb 上的 SQL-92 到 Access 2010 下的 SQL Server

sql - sql语句末尾缺少分号访问

vba - excel vba 插入带格式的行

excel - 当相邻列中的相应行具有特定值时,计算列中非空单元格的总和 - VBA 或公式?

sql - MS Access 选择表中的第 N 个最高值

ms-access - 打开数据库时 Access 2016 AppCrash