473,397 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Add a file to an Attachment via VBA code

reginaldmerritt
201 100+
You can add an Attachment though code via RecordSets.
Something like this
Expand|Select|Wrap|Line Numbers
  1. Dim MyRECORDSET As DOA.Recordset
  2. Dim NumberOfRecords As Integer
  3. Dim MyCounter as Integer
  4.  
  5. Set MyRECORDSET to Currentdb.OpenRecordset("MyTable")
  6. NumberOfRecords = DCount("EmployeNumberPK","MyTable")
  7. MyCounter = 0
  8.  
  9. MyRECORDSET.MoveFirst
  10.  
  11. For MyCounter = 1 to NumberOfRecords
  12.    MyRECORDSET.Edit
  13.    If MyRECORDSET("EmployeNumberPK") = Me.EmployeeNumberPK Then
  14.        MyRECORDSET("EmployeePicture").LoadFile "C:\EmployeePictures\" & Me.EmployeeNumberPK & ".jpg"
  15.    End If 
  16.    MyRECORDSET.Update
  17. Next
  18.  
Is there another way to attach a file to an attachment data type field without using RecordSets?

Maybe something like
Expand|Select|Wrap|Line Numbers
  1. Me.EmployeePicture.Value = "C:\EmployeePictures\" & Me.EmployeeNumberPK & ".jpg"
  2.  
With lots of records I find looping though RecordSets slows down the program.

Thanks.
Aug 25 '10 #1
2 19274
reginaldmerritt
201 100+
Apparently you can use an SQL Statement as a Recordset. If that's true I should be able to go straight to the record required and attach the file.

However, I'm having trouble the the syntax I think. I get an error message
To Few parameters. Expected 1

The code is
Expand|Select|Wrap|Line Numbers
  1. Dim CRBRecordSet As Recordset
  2. Dim SQL As String
  3.  
  4. SQL = "Select * From CRB Where FormnNumber = '" & Me.FormNumber & "';"
  5.  
  6. ' can use SQL for recordset
  7. Set CRBRecordSet = CurrentDb.OpenRecordset(SQL)
  8.  
  9. If Not (CRBRecordSet.EOF And CRBRecordSet.BOF) Then
  10.     QYRecordSet.Edit
  11.     QYRecordSet.Fields("Attachments").LoadFromFile "\\Universe\SharedFolders\Scans\CRBApplications\" & Me.FormNumber & ".pdf"
  12.     QYRecordSet.Update
  13. End If
  14.  
I'm guessing there is something wrong with the SQL statement but I can't find out what it is.
Aug 26 '10 #2
reginaldmerritt
201 100+
Sorry, ignore last post. Misspelt Field in SQL statement.

Had to change the code slightly, was working from a bad example online. You can't trust everything you see ;)

Expand|Select|Wrap|Line Numbers
  1. Private Sub AttachScannedDocument()
  2. Dim CRBApplicationRecordSet, ScannedApplicationRecordSet As Recordset
  3. Dim SQL As String
  4.  
  5. SQL = "Select * From CRB Where FormNumber = '" & Me.FormNumber & "'"
  6.  
  7. ' can use SQL for recordset to instantiate parent recordset
  8. Set CRBApplicationRecordSet = CurrentDb.OpenRecordset(SQL)
  9.  
  10. 'active edit mode
  11. CRBApplicationRecordSet.Edit
  12.  
  13. ' instantiate a child recordset
  14. Set ScannedApplicationRecordSet = CRBApplicationRecordSet.Fields("Attachments").Value
  15.  
  16. 'Add Scanned Appication
  17. ScannedApplicationRecordSet.AddNew
  18. ScannedApplicationRecordSet.Fields("FileData").LoadFromFile "Apollo.jpg" '"\\Universe\SharedFolders\Scans\CRBApplications\" & Me.FormNumber & ".pdf"
  19. ScannedApplicationRecordSet.Update
  20.  
  21. 'update parent recordset
  22. CRBApplicationRecordSet.Update
  23.  
  24. Set CRBApplicationRecordSet = Nothing
  25. Set ScannedApplicationRecordSet = Nothing
  26.  
  27. End Sub
  28.  
I have two problems however.

The first time the procedure is run, it runs though with no errors. The second time I get an error message.
You cannot enter value because it duplicates and existing value in the attachment field.
How do I check to see if the value of the attachment field matches the one I'm trying to add?

More importantly the procedure does not attach the file. It seems to think that it has but it doesn't work. Any ideas where I'm going wrong here?
Aug 26 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Gordon H. | last post by:
I'm trying to email a HTML link to a HTML file attached in the same message. I DO NOT want to have the attached HTML file displayed in the message, I just want a link in the email to the HTML file...
1
by: Mike | last post by:
How can I provide a dialog box for a user to select a local file attachment (with just the path to the file stored as a text string in the field in my db). Is there an ActiveX control I should...
6
by: zippy | last post by:
In my code I create and save a file. I would like to be able to zip that file. I've looked online and it seems that there are no zip classes built in to .Net for C#. How can i zip a file then?...
6
by: Gnic | last post by:
Hi, Instead of running "sn-k" to generate a key pair, I need to generate the key file in code, is there anyone know how to generate key files in .NET code? Any help is appreciated. Gnic
5
by: Lee | last post by:
Is there anyway to backup a SQL server database to a .sql script file through code? My provider charges me 5.00 to perform backups. I'd like to do it on my own through code. I don't have access...
1
by: amitverma | last post by:
how can i run executable file through code in vb ?
9
by: deepaks85 | last post by:
Dear Sir, I have created a simple request form which will be mailed to me. Now I want to attach files and send it through that request form. For this I am using the following script: ...
7
ddtpmyra
by: ddtpmyra | last post by:
Need to know how to do the vba coding on attaching the file code Private Sub Command_Click() Dim rs1 As Recordset Set rs1 = CurrentDb.OpenRecordset("cmr_filestorage") rs1.AddNew Me.author =...
2
by: Erik Witkop | last post by:
So I have been trying to get this to work all day. I can't get a local file on my web server to attach to an email. Right now I have it printing out in the body of the email. Please help me with...
6
by: vbbeginner | last post by:
how to make 2 exe files in one exe file by code?? i have two exe files 1.exe 2.exe i need to make them in one exe file by third exe file?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.