To add attachment file to specific record, I use following code but don't know if it is the best way? (1 Viewer)

nhtuan

New member
Local time
Today, 06:32
Joined
Dec 23, 2010
Messages
24
I use this code to attach a QR code picture to current record, is there any further improvement?
Code:
Private Sub Command4_Click()
Dim CoutriesRset, FilteredRset As Recordset
Dim SQL As String
 
SQL = "Select * From tblCountries Where Country = '" & Me.Country & "'"
 
' can use SQL for recordset to instantiate parent recordset
Set CoutriesRset = CurrentDb.OpenRecordset(SQL)
 
'active edit mode
CoutriesRset.Edit
 
' instantiate a child recordset
Set FilteredRset = CoutriesRset.Fields("ImageQrcode").Value

If FilteredRset.RecordCount <= 0 Then
'Add Scanned Appication
FilteredRset.AddNew
If Dir(Application.CurrentProject.Path & "\onecqr.jpg") <> "" Then
FilteredRset.Fields("FileData").LoadFromFile Application.CurrentProject.Path & "\onecqr.jpg"
Kill Application.CurrentProject.Path & "\onecqr.jpg"
FilteredRset.Update
FilteredRset.Close
'update parent recordset
CoutriesRset.Update
CoutriesRset.Close
'FilteredRset.Close
End If
End If

'CoutriesRset.Close
'FilteredRset.Close
Set CoutriesRset = Nothing
Set FilteredRset = Nothing
Me.Refresh
End Sub
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 21:32
Joined
May 7, 2009
Messages
19,237
just save the QR image path to a Short Text, instead of using Attachment field.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:32
Joined
Oct 29, 2018
Messages
21,468
Hi. I agree. Can't you just use a Text field to store the path to the image file? Otherwise, if you must use an Attachment field, I don't think your current code really needs any further improvement. How is the QR code generated?
 

nhtuan

New member
Local time
Today, 06:32
Joined
Dec 23, 2010
Messages
24
any further improvement. How is the QR code
Thanks for your generous comments, the QR code is programmatically created using Google API
Code:
"https://chart.googleapis.com/chart?chs=250x250&cht=qr&chl=" & [NGAYBATDAU] & " &choe=UTF-8&chma=1,1,1,1&chof=png"
 

theDBguy

I’m here to help
Staff member
Local time
Today, 06:32
Joined
Oct 29, 2018
Messages
21,468
Thanks for your generous comments, the QR code is programmatically created using Google API
Code:
"https://chart.googleapis.com/chart?chs=250x250&cht=qr&chl=" & [NGAYBATDAU] & " &choe=UTF-8&chma=1,1,1,1&chof=png"
Hi. Thanks for the additional information.
 

moke123

AWF VIP
Local time
Today, 09:32
Joined
Jan 11, 2013
Messages
3,916
Code:
Dim CoutriesRset, FilteredRset As Recordset

You should dim CoutriesRset as a recordset rather than a variant.
 

Users who are viewing this thread

Top Bottom