vba - 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
解决方案
如果您要使用连接和行继续符,那么实际上应该在下一行继续。
表名具有特殊字符。用来划界[]
。可以简化 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"
建议不要在命名约定中使用标点符号/特殊字符。
推荐阅读
- highcharts - Coloring a region programmatically based by user selection with Highmaps
- jquery - 剑道多选键入的文本与所选项目重叠
- python - 如何将张量流损失函数从单个数字更改为数组
- electron - 如果用户打开对话框,防止 BrowserWindow 隐藏模糊
- r - R: Plot arrows perpendicular to contour lines
- html - Dropdown on alert bootstrap v3
- vue.js - cypress.js e2e test all elements are undefined
- ios - 如何在 iOS 12 中使用深度数据激活人像效果
- python - 错误组参数现在在 multiprocessing.pool 中必须为 None
- node.js - 在nodejs中访问函数外部的restAPI响应