Copy a row from a table to another table in Access - vba

This is my first time working with Access so I am kind of confused now. Here is my code and I don't know which part is wrong. There isn't error but there isn't anything happening after I click the button. Thanks! Here is the code:
Private Sub Command12_Click()
Dim db As Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Amity")
Set rs2 = db.OpenRecordset("Opportunity")
With rs2
.AddNew
.Fields("Donor_Code") = rs!Donor_Code
.Update
.Close
End With
rs.Close
End Sub

You can try this with a Do loop:
Private Sub Command12_Click()
Dim db As Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Amity")
Set rs2 = db.OpenRecordset("Opportunity")
Do While (Not rs.EOF)
rs2.AddNew
rs2.Fields("Donor_Code").Value = rs!Donor_Code.Value
rs2.Update
rs.MoveNext
Loop
'
rs2.Close
Set rs2 = Nothing
rs.Close
Set rs = Nothing
Set db = Nothing
'
End Sub

Related

DAO database only references the first record in a table, meant to put it into specific record - Microsoft Access Code

At the moment, it takes an image I add to my signature folder and puts it straight into the attachments field of my contract. HOWEVER! It does not put it into the specific record for which the button is pressed- it puts the image into the signature box of the first record every time. I am new to SQL, how would I go about making it
So below is my code:
Private Sub Command21_Click()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset2
Dim strFile As String
Set db = CurrentDb()
strFile = "C:\Users\Reception\Desktop\Files\Signature\sign.png"
Set rs1 = db.OpenRecordset("Table1")
rs1.Edit
Set rs2 = rs1!GuestRegistration.Value
With rs2
.AddNew
!FileData.LoadFromFile strFile
.Update
End With
rs1.Update
Form.Refresh
Set rs2 = Nothing
Set rs1 = Nothing
Set db = Nothing
End Sub
Any help is greatly appreciated!!! Sorry I am so novice.
I tried a few things but I don't understand enough about coding to know what I was doing. I am still a beginner doing this for my business. I just need it to pt the image in the correct record rather than instantly putting it in the first record of the recordset.
Nevermind!! Thank you so much #June7 for the response! I set his response code as a string and referenced it instead of Table1 so it isolates only the record I wanted. Find attached my code below if you need this for help with any projects :)
Private Sub Command21_Click()
Dim db As DAO.Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset2
Dim strFile As String
Dim strSQL As String
strSQL = "SELECT * FROM Table1 WHERE ID=" & Me!ID
Set db = CurrentDb()
strFile = "C:\Users\Reception\Desktop\Files\Signature\sign.png"
Set rs1 = db.OpenRecordset(strSQL)
rs1.Edit
Set rs2 = rs1!GuestRegistration.Value
With rs2
.AddNew
!FileData.LoadFromFile strFile
.Update
End With
rs1.Update
Form.Refresh
Set rs2 = Nothing
Set rs1 = Nothing
Set db = Nothing
End Sub

How do you copy the entire record from one table to another including attachment filed?

I have two tables, tb1 and tb2. I would like to copy the entire record from tab1 to tbl2. The tables contain attachment fields so INSERT statement is not suitable. My current approach uses DAO but its only copying the first record. Please see code:
Private Sub InsertRecord_Click()
Dim db As Database
Dim rs1 As DAO.Recordset
Dim rs2 As DAO.Recordset
Dim rsAttachment1 As DAO.Recordset2
Dim rsAttachment2 As DAO.Recordset2
Set rs1 = CurrentDb.OpenRecordset("tbl1")
Set rs2 = CurrentDb.OpenRecordset("tbl2")
With rs1
rs2.AddNew
rs2.Fields("ItemNo").Value = rs1.Fields("ItemNo").Value
rs2.Fields("Location").Value = rs1.Fields("Location").Value
rs2.Fields("Owner").Value = rs1.Fields("Owner").Value
rs2.Fields("DateSent").Value = DateTime.Now
Set rsAttachment1 = rs1.Fields("ItemImage").Value
Set rsAttachment2 = rs2.Fields("ItemImage").Value
With rsAttachment1
Do While Not .EOF
rsAttachment2.AddNew
rsAttachment2.Fields("FileData") = .Fields("FileData")
rsAttachment2.Fields("FileName") = .Fields("FileName")
rsAttachment2.Update
rsAttachment1.MoveNext
Loop
End With
rs2.Update
.MoveNext
End With
rs2.Close
Set rs2 = Nothing
'rsAttachment1.Close
Set rsAttachment1 = Nothing
Set rsAttachment2 = Nothing
End Sub
Any other better approach is also welcome.
Use a loop:
While Not rs1.EOF
With rs1
rs2.AddNew
rs2.Fields("ItemNo").Value = rs1.Fields("ItemNo").Value
rs2.Fields("Location").Value = rs1.Fields("Location").Value
rs2.Fields("Owner").Value = rs1.Fields("Owner").Value
rs2.Fields("DateSent").Value = DateTime.Now
Set rsAttachment1 = rs1.Fields("ItemImage").Value
Set rsAttachment2 = rs2.Fields("ItemImage").Value
With rsAttachment1
Do While Not .EOF
rsAttachment2.AddNew
rsAttachment2.Fields("FileData") = .Fields("FileData")
rsAttachment2.Fields("FileName") = .Fields("FileName")
rsAttachment2.Update
rsAttachment1.MoveNext
Loop
End With
rs2.Update
.MoveNext
End With
rs1.MoveNext
Wend

VBA Access IF condition

I have a problem that my IF somehow doesn't validate good value of the field on index 0.
Here is the UDPATED code:
Private Sub Parametri()
Dim db As dao.Database
Dim rs As dao.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("ribe")
rs.MoveLast
rs.MoveFirst
For i = 0 To rs.RecordCount
If rs.Fields(i).Value > 2 Then
Debug.Print rs.Fields("Lokacija_GS")
rs.MoveNext
End If
Next
End Sub
And here is the result:
1
43.626145
43.626145
43.630122
43.632358
43.625833
This value of "1" on index 0 should be skipped... but it isnt?
here is the table:
So for example if some row is 0 or 1 or NULL I want to skip it...
Here is the correct code,
Private Sub Parametri()
Dim db As dao.Database
Dim rs As dao.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("ribe")
rs.MoveLast
rs.MoveFirst
Do While Not rs.EOF
If rs.Fields("Lokacija_GS").Value > 2 Then _
Debug.Print rs.Fields("Lokacija_GS")
rs.MoveNext
Loop
Set rs = Nothing
Set db = Nothing
End Sub

The checking is always true after first loop in VBA in Access

The "Check" somehow is always 0 after first loop, I keep debugging but still cannot find out why. Any idea? The data suppose to make "check" be 0 sometimes but not all the time.
Private Sub Command12_Click()
Dim db As Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Amity")
Set rs2 = db.OpenRecordset("Opportunity")
Set rs3 = db.OpenRecordset("SalesForceDonor")
Set rs4 = db.OpenRecordset("Donor")
While Not rs.EOF
check = 0
While Not rs3.EOF
If rs("Donor_Code") = rs3("Donor_Code") Then
check = 1
End If
rs3.MoveNext
Wend
If check = 0 Then
rs4.AddNew
rs4![Donor_Code] = rs![Donor_Code]
rs4.Update
End If
rs2.AddNew
rs2![Donor_Code] = rs![Donor_Code]
rs2![Donation_name] = rs![Donation_name]
rs2.Update
rs.MoveNext
Wend
rs3.Close
rs4.Close
rs2.Close
rs.Close
End Sub
I've found somethig that must be corrected, adding rs3.MoveFirst for each record of rs:
Private Sub Command12_Click()
Dim check
Dim db As Database
Dim rs As DAO.Recordset
Dim rs2 As DAO.Recordset
Set db = CurrentDb()
Set rs = db.OpenRecordset("Amity")
Set rs2 = db.OpenRecordset("Opportunity")
Set rs3 = db.OpenRecordset("SalesForceDonor")
Set rs4 = db.OpenRecordset("Donor")
While Not rs.EOF
check = 0
rs3.MoveFirst ' <= here we move to the first record of rs3!!!
Do While Not rs3.EOF
If rs("Donor_Code") = rs3("Donor_Code") Then
check = 1
Exit Do
End If
rs3.MoveNext
Loop
If check = 0 Then
rs4.AddNew
rs4![Donor_Code] = rs![Donor_Code]
rs4.Update
End If
rs2.AddNew
rs2![Donor_Code] = rs![Donor_Code]
rs2![Donation_name] = rs![Donation_name]
rs2.Update
rs.MoveNext
Wend
rs3.Close
rs4.Close
rs2.Close
rs.Close
End Sub

How to move from one record to another

I have created an onclick function which should run an SQL statement, select URLs from the statement and opens the Excel files sequentially from the URL.
It works but if the query returns 3 results then it opens the same excel file 3 times.
Instead of opening one file after the other it opens the same file.
This is what I have:
Dim strSQL As String
Dim rs As DAO.Recordset
Dim db As Database
Dim appexcel As Object
Set db = CurrentDb()
strSQL = "SELECT * from [PROCESSED_CONTRACTS_PRINTALL]"
Set rs = db.OpenRecordset(strSQL)
urlval = rs!url
rs.MoveFirst
Do Until rs.EOF
Set appexcel = CreateObject("Excel.Application")
appexcel.workbooks.Open urlval
appexcel.Visible = True
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub
Any help please? thanks.
How about this, note that urlval is now inside the loop and Excel is outside the loop.
Dim strSQL As String
Dim rs As DAO.Recordset
Dim db As Database
Dim appexcel As Object
Set db = CurrentDb()
strSQL = "SELECT * from [PROCESSED_CONTRACTS_PRINTALL]"
Set appexcel = CreateObject("Excel.Application")
appexcel.Visible = True
Set rs = db.OpenRecordset(strSQL)
rs.MoveFirst
Do Until rs.EOF
urlval = rs!url
appexcel.workbooks.Open urlval
rs.MoveNext
Loop
rs.Close
Set rs = Nothing
End Sub