How to insert new records into tableb no duplicate with tablea

btamsgn

Member
Local time
Today, 21:50
Joined
Nov 8, 2010
Messages
51
Dear everyone,
I add a function dayso() in module1 and 1 button "dayso" .after click data insert into tableb. However data duplicate
I want to insert records no duplicate both tablea and tableb. That records inserted tableb based on C1 to C6 from tablea.
example: insert into tableb(H1,H2,H3,H4,H5,H6)
01 18 23 26 31 40
etc,...
Look forward to receiving your support, thanks.
 

Attachments

Set unique indices?
Only select records that do not already exist in table b ?
 
Before you think about it any further: Normalize your tables. Enumeration fields (C1, C2, C3, ..., C0815) with the same contents are not a suitable basis for meaningful database actions.
 
here try your dayso() sub.
 

Attachments

Dear arnelgp,
after click button "dayso" with your public sub dayso(), all records as the same from tablea.
ex. after click button "dayso" =>ouput records 1 10 21 25 32 39 or 1 3 22 27 38 40 exist from tablea already etc.
*I want to insert new records no duplicate and different from tablea, but records mixed order based on C1 to C6 from tablea.
* the sub below insert still missing records
Sub dayso(tablea As String, fromngay As Date, tongay As Date, loai As String, tableb As String)
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim rsCheckA As DAO.Recordset
Dim rsCheckB As DAO.Recordset
Dim strSQL As String
Dim arr(1 To 6) As Variant
Dim i As Integer
Dim STT As Integer

Set db = CurrentDb()

' Initialize STT
STT = 1

' Select records from tablea based on the criteria
strSQL = "SELECT * FROM " & tablea & " WHERE Ngay >= #" & fromngay & "# AND Ngay <= #" & tongay & "# AND Type = '" & loai & "'"
Set rs = db.OpenRecordset(strSQL)

' Loop through the recordset
Do Until rs.EOF
' Store the values of the current record
For i = 1 To 6
arr(i) = rs.Fields("C" & i).Value
Next i

' Move to the next record
rs.MoveNext

' If there is a next record, mix the values and insert a new record into tableb
If Not rs.EOF Then
' Check if the record already exists in tablea
strSQL = "SELECT * FROM " & tablea & " WHERE C1 = " & arr(1) & " AND C2 = " & arr(2) & " AND C3 = " & rs!C3 & " AND C4 = " & rs!C4 & " AND C5 = " & rs!C5 & " AND C6 = " & rs!C6
Set rsCheckA = db.OpenRecordset(strSQL)

' Check if the record already exists in tableb
strSQL = "SELECT * FROM " & tableb & " WHERE h1 = " & arr(1) & " AND h2 = " & arr(2) & " AND h3 = " & rs!C3 & " AND h4 = " & rs!C4 & " AND h5 = " & rs!C5 & " AND h6 = " & rs!C6
Set rsCheckB = db.OpenRecordset(strSQL)

If rsCheckA.EOF And rsCheckB.EOF Then
' If the record does not exist in both tables, insert it into tableb
' But first, check if the values are unique
Dim uniqueValues As New Collection
On Error Resume Next
uniqueValues.Add arr(1), CStr(arr(1))
uniqueValues.Add arr(2), CStr(arr(2))
uniqueValues.Add rs!C3, CStr(rs!C3)
uniqueValues.Add rs!C4, CStr(rs!C4)
uniqueValues.Add rs!C5, CStr(rs!C5)
uniqueValues.Add rs!C6, CStr(rs!C6)
On Error GoTo 0
If uniqueValues.Count = 6 Then
strSQL = "INSERT INTO " & tableb & " (h1, h2, h3, h4, h5, h6, type, STT) VALUES (" & arr(1) & ", " & arr(2) & ", " & rs!C3 & ", " & rs!C4 & ", " & rs!C5 & ", " & rs!C6 & ", '" & loai & "', " & STT & ")"
db.Execute strSQL
STT = STT + 1
End If
End If
rsCheckA.Close
rsCheckB.Close
End If
Loop

' Clean up
rs.Close
Set rs = Nothing
Set db = Nothing
End Sub
 
Last edited:
ebs17's correct comment is getting lost in the people trying to help you down this incorrect path. So let me restate it:

Normalize your data properly


That is the process of correctly setting up your fields and tables in a relational database to accommodate your data. Access is not Excel With Forms™. It is a relational database and requires you think about your data differently than what Excel does. Give the above link a read, google a few tutorials and then apply what you learn to your data.
 
Hi Everyone,
Pls fix error 3021 at result = result & num & "," from the function below:
Function tkeso(tablea As String, fromngay As Date, tongay As Date, loai As String) As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim SQL As String
Dim result As String
Dim numbers As New Collection
Dim num As Variant

Set db = CurrentDb()

' T?o câu truy v?n SQL v?i các di?u ki?n dã cho
SQL = "SELECT C1, C2, C3, C4, C5, C6 FROM " & tablea & " WHERE Ngay BETWEEN #" & fromngay & "# AND #" & tongay & "# AND Type = '" & loai & "'"

Set rs = db.OpenRecordset(SQL)

' Ki?m tra xem t?p h?p b?n ghi có b?n ghi nào không
If Not rs.EOF Then
' Duy?t qua t?ng b?n ghi và thêm các s? vào b? suu t?p
Do Until rs.EOF
On Error Resume Next
For Each num In Array(rs!c1, rs!c2, rs!c3, rs!c4, rs!c5, rs!c6)
numbers.Add num, CStr(num) ' S? d?ng s? làm khóa d? tránh l?p l?i
Next num
On Error GoTo 0
rs.MoveNext
Loop
End If

' T?o chu?i k?t qu?
For Each num In numbers
result = result & num & ","
Next num

' Lo?i b? d?u ph?y cu?i cùng
If Len(result) > 0 Then result = Left(result, Len(result) - 1)

tkeso = result

rs.Close
Set rs = Nothing
Set db = Nothing
End Function
 
No code tags, no effort on my part.
No idea what error 3021 is either. Why should I have to look it up? :(
 
No code tags, no effort on my part.
No idea what error 3021 is either. Why should I have to look it up? :(
Hi Gasman,
The function belows : used to get values from c1 to c6 and no repetition. however after click then error 3021 at result = result & num & "," from the function below:
Function tkeso(tablea As String, fromngay As Date, tongay As Date, loai As String) As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim SQL As String
Dim result As String
Dim numbers As New Collection
Dim num As Variant

Set db = CurrentDb()

' T?o câu truy v?n SQL v?i các di?u ki?n dã cho
SQL = "SELECT C1, C2, C3, C4, C5, C6 FROM " & tablea & " WHERE Ngay BETWEEN #" & fromngay & "# AND #" & tongay & "# AND Type = '" & loai & "'"

Set rs = db.OpenRecordset(SQL)

' Ki?m tra xem t?p h?p b?n ghi có b?n ghi nào không
If Not rs.EOF Then
' Duy?t qua t?ng b?n ghi và thêm các s? vào b? suu t?p
Do Until rs.EOF
On Error Resume Next
For Each num In Array(rs!c1, rs!c2, rs!c3, rs!c4, rs!c5, rs!c6)
numbers.Add num, CStr(num) ' S? d?ng s? làm khóa d? tránh l?p l?i
Next num
On Error GoTo 0
rs.MoveNext
Loop
End If

' T?o chu?i k?t qu?
For Each num In numbers
result = result & num & ","
Next num

' Lo?i b? d?u ph?y cu?i cùng
If Len(result) > 0 Then result = Left(result, Len(result) - 1)

tkeso = result

rs.Close
Set rs = Nothing
Set db = Nothing
End Function
 
Hi Gasman,
The function belows : used to get values from c1 to c6 and no repetition. however after click then error 3021 at result = result & num & "," from the function below:
Function tkeso(tablea As String, fromngay As Date, tongay As Date, loai As String) As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim SQL As String
Dim result As String
Dim numbers As New Collection
Dim num As Variant

Set db = CurrentDb()

' T?o câu truy v?n SQL v?i các di?u ki?n dã cho
SQL = "SELECT C1, C2, C3, C4, C5, C6 FROM " & tablea & " WHERE Ngay BETWEEN #" & fromngay & "# AND #" & tongay & "# AND Type = '" & loai & "'"

Set rs = db.OpenRecordset(SQL)

' Ki?m tra xem t?p h?p b?n ghi có b?n ghi nào không
If Not rs.EOF Then
' Duy?t qua t?ng b?n ghi và thêm các s? vào b? suu t?p
Do Until rs.EOF
On Error Resume Next
For Each num In Array(rs!c1, rs!c2, rs!c3, rs!c4, rs!c5, rs!c6)
numbers.Add num, CStr(num) ' S? d?ng s? làm khóa d? tránh l?p l?i
Next num
On Error GoTo 0
rs.MoveNext
Loop
End If

' T?o chu?i k?t qu?
For Each num In numbers
result = result & num & ","
Next num

' Lo?i b? d?u ph?y cu?i cùng
If Len(result) > 0 Then result = Left(result, Len(result) - 1)

tkeso = result

rs.Close
Set rs = Nothing
Set db = Nothing
End Function
Posting the same code again (still without code tags) and repeating the error code without the description, is wasting space. :(

Try walking your code and inspect the variables. That normally sorts out silly errors.
 
here is a combination, but it is very slow.
 

Attachments

file is not working as belows
1709556794583.png
 
Posting the same code again (still without code tags) and repeating the error code without the description, is wasting space. :(

Try walking your code and inspect the variables. That normally sorts out silly errors.
textbox1=tkeso("All455", txthnay, txttongay,"T45") =>error 3021 at result = result & num & ","
you tried do the function or not ?
 

Users who are viewing this thread

Back
Top Bottom