Error 424 Object Required Access VBA - sql

I'm trying to find the first record in the table that has my Active Field Name (Boolean) as True (-1).
It goes straight to error on the findfirst line. Please advise.
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("tblinstalments", dbOpenDynaset)
With rs
rs.FindFirst "[Active] = -1"
If rst.NoMatch Then
MsgBox "No entry found"
End If

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

Automated Attachment Import Module -- [Error: "Run-time error '424': Object required"]

This is my first time actually posting in a forum so let me know if I am using any bad practice here.
I am using access to organize iMessage data to format into a human-readable book. The database contains timestamp and attachment filename fields. The actual attachment file names contain said timestamp and filename values with some filler between. My goal is to parse through several thousand texts and import all attachments where applicable. See example below.
Database Example: https://i.stack.imgur.com/6rcgi.png
File Example: https://i.stack.imgur.com/0xvjy.png
I have the module written out as seen below and am getting "Error: "Run-time error '424': Object required" referencing line 27. Very helpful, Microsoft.
Option Compare Database
Function ImportAttach()
Dim db As DAO.Database
Dim rs As DAO.Recordset2
Dim rsT As DAO.Recordset2
Dim rsA As DAO.Recordset2
Dim fldT As DAO.Field2
Dim fldA As DAO.Field2
Dim strTimeStamp As String
Dim strFileName As String
Dim strPath As String
Dim strFile As String
Set db = CurrentDb
Set rs = db.OpenRecordset("iMessageDB")
Set fldT = rs("TimeStamp")
Set fldA = rs("Attachment")
strFilePath = "C:\Users\XPS\Documents\Projects\iMessage Book\attachments\"
rs.MoveFirst
Do Until rs.EOF
Set rsT = fldT.Value '<-- Error Here
Set rsA = fldA.Value
If IsNull(strFileName) Then
rs.MoveNext
Else
strFile = Dir(strPath & strTimeStamp & "*" & strFileName)
rs.Edit
rsA.AddNew
rsA("FileData").LoadFromFile strPathFileName
rsA.Update
End If
strFile = Dir
rsA.Close
rs.Update
rs.MoveNext
Loop
rs.Close
db.Close
Set fldT = Nothing
Set fldA = Nothing
Set rsT = Nothing
Set rsA = Nothing
Set rs = Nothing
Set db = Nothing
End Function
I have not found a good solution elsewhere, so I appreciate any help.

Access recordset not updating table

I am learning how to handle Access VBA recordsets.
In the example below, I am using a SQL query to open my recorset and sort the data using strName col. I can see that the query is working when I am debbuging the code, however I don't know why the table is not getting updated with sorted rows. Any suggestion?
Option Compare Database
Option Explicit
Sub OrderDB()
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim mySortedRS As DAO.Recordset
'Set the DAO database to current Access db
Set db = CurrentDb
'Open the table as the recordset
Set rs = db.OpenRecordset("SELECT * FROM Table1 order by strName", dbOpenDynaset)
'Process the rows
Do
Debug.Print rs!strName
rs.Edit
rs.Update
rs.MoveNext
Loop Until rs.EOF
'Cleanup
rs.Close
Set rs = Nothing
End Sub

MS ACCESS VBA Move/Copy Files

I am using the below code to reference a table that has full path information to move (or copy) files from 1 location to another. However, it's not moving anything, but is completing per my Debug.Print message (Move Complete 2/22/2021 1:22:41 PM). Any thoughts on what I'm missing?
Additionally, I'd like to build the folder/subfolder structure where the file was located in the source...but don't know how to achieve that...and pointers on how to do this?
Sub copy_files_from_table()
Dim FSO As Object
Dim source As String
Dim destination As String
Dim SQL As String
Dim RS As DAO.Recordset
Dim db As DAO.Database
SQL = "select * from file_test"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set db = CurrentDb
Set RS = db.OpenRecordset(SQL)
source = RS!LocalFile
destination = "C:\Temp\Test"
While Not RS.EOF
If Dir(source, vbDirectory) <> "" Then
objFSO.CopyFolder source, destination:=destination '
Debug.Print "Move Folder Command Complete From: " & destination
Else
End If
RS.MoveNext
Wend
Debug.Print "Move Complete " & Now()
End Sub
Appreciate any help provided.
So far, I have gotten the following code to work on file paths <259; however, anything longer is causing the code to error. Since I'm pretty green on coding:) any suggestions how I can get around the long file path names?
Sub CopyFilesFromTable2()
On Error GoTo ErrorHandler
Dim source As String
Dim destination As String
Dim FSO As New FileSystemObject
Dim SQL As String
Dim RS As DAO.Recordset
Dim db As DAO.Database
'Test Table
SQL = "select * from file_test"
'Prod Table
'SQL = "select * from file"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set db = CurrentDb
Set RS = db.OpenRecordset(SQL)
source = RS!LocalFile
File = VBA.FileSystem.Dir(source)
destination = "D:\Temp\Test\"
With RS
If Not .BOF And Not .EOF Then
.MoveLast
.MoveFirst
While (Not .EOF)
FSO.CopyFile RS!LocalFile, destination
.MoveNext
Wend
End If
.Close
End With
ExitSub:
Set RS = Nothing
'set to nothing
MsgBox "Done!"
Exit Sub
ErrorHandler:
MsgBox Err, vbCritical
Resume ExitSub
End Sub

How to read values from one of custom Queries in MS Access VBA?

I have a MS ACCESS query called Query11 that sums up amounts.
Let's say it's got SUM_WEEKLY and SUM_MONTHLY as a field in Query11
In VBA, how could I get each value?
I have tried to get them with the codes below and it did not work.
Dim dbMyDB As Database
Dim rsMyRS As Recordset
Set dbMyDB = CurrentDb
Set rsMyRS = dbMyDB.OpenRecordset("Query11")
MsgBox rsMyRS("SUM_WEEKLY")
MsgBox rsMyRS("SUM_MONTHLY")
Your code should work, and you don't tell the error, but try to be a bit more explicit:
Dim dbMyDB As DAO.Database
Dim rsMyRS As DAO.Recordset
Set dbMyDB = CurrentDb
Set rsMyRS = dbMyDB.OpenRecordset("Query11")
' Check that a record exists.
MsgBox CStr(rsMyRS.RecordCount)
MsgBox rsMyRS("SUM_WEEKLY").Value
MsgBox rsMyRS("SUM_MONTHLY").Value
Dim db As DAO.Database
Dim rs As DAO.Recordset
Set db = CurrentDb
Set rs = db.OpenRecordset("Query11")
' Check a record exists
If rs.RecordCount > 0 Then
MsgBox "SUM_WEEKLY = " & rs!SUM_WEEKLY
MsgBox "SUM_MONTHLY = " & rs!SUM_MONTHLY
Else
MsgBox "Recordset has no records"
End If