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

nhtuan

New member
Local time
Today, 08:21
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
 
just save the QR image path to a Short Text, instead of using Attachment field.
 
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?
 
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"
 
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.
 
Code:
Dim CoutriesRset, FilteredRset As Recordset

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

Users who are viewing this thread

Back
Top Bottom