Mam problem, że muszę utworzyć kod VBA dla MS Access 2016, który skopiuje zawartość tabeli 1 do tabeli 2 z dwoma warunkami
1.) Jeśli nie znajdzie duplikatów w tabeli 2, kopiuje dane
2.) Jeśli w tabeli 2 są duplikaty, wyświetla się komunikat o duplikatach i pytanie, czy kontynuować, czy przerwać.
Mam ten kod VBA, ale nie wiem, jak go przerobić, aby działał tak, jak chcę
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 = "tblVWI"
strSQL = "SELECT Count (*) FROM" & strTable & "WHERE" & "[Department] = '" &
cboDepartment & "' AND [VWINum] = '" & txtVWI & "'"
rst.Open strSQL, Conn
If rst (0)> 0 Then 'duplicated record found.
If Me.NewRecord Then
intResponse = MsgBox ("This record exist" & vbCrLf & "Do you want to
duplicate Item Number?", 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
1.) Jeśli nie znajdzie duplikatów w tabeli 2, kopiuje dane
2.) Jeśli w tabeli 2 są duplikaty, wyświetla się komunikat o duplikatach i pytanie, czy kontynuować, czy przerwać.
Mam ten kod VBA, ale nie wiem, jak go przerobić, aby działał tak, jak chcę
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 = "tblVWI"
strSQL = "SELECT Count (*) FROM" & strTable & "WHERE" & "[Department] = '" &
cboDepartment & "' AND [VWINum] = '" & txtVWI & "'"
rst.Open strSQL, Conn
If rst (0)> 0 Then 'duplicated record found.
If Me.NewRecord Then
intResponse = MsgBox ("This record exist" & vbCrLf & "Do you want to
duplicate Item Number?", 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