首页 > 解决方案 > MS Access将表复制到另一个表VBA代码

问题描述

我有一个问题,我需要为 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

标签: vbams-accessms-access-2010ms-access-2016

解决方案


如果您要使用连接和行继续符,那么实际上应该在下一行继续。

表名具有特殊字符。用来划界[]。可以简化 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"

建议不要在命名约定中使用标点符号/特殊字符。


推荐阅读