473,396 Members | 2,082 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,396 software developers and data experts.

Help Storing Attachments to Specific Records

The database I have created needs a feature in which certain projects can be attached with certain specified PDF's.

I have tried creating the following loop in order to search through a table column (table is named attachments) the column (which is titled project number and is just right of ID or in column(1)by a generated project number on my form (pnumber.value)

If a match exists between my table [Attachments](Field(1))and the generated number (pnumber.value) in my table column that it will edit the attachments column (called attachments - field 6) and update the attachments based on those attached on my form.

If there is not a match I would like it to create a new record with the project number details and associated attachments generated adn saved on the form.

PS (TorF.value is a textbox I generated to tell me if a record exists in the table on the form load to help with troubleshooting and potentially to use as simplyfying the switch / If else statements)

Thank you all very much for your help and I appreciate the time you have taken out to read the post,

LonelyKeyboard


Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click() '(button Update Project)
  2.  
  3. Dim rs As DAO.Recordset
  4. Dim db As DAO.Database
  5. Dim PNum As String
  6. Set db = CurrentDb
  7. Set rs = db.OpenRecordset("Attachments", dbOpenDynaset)
  8.  
  9. PNum = PNumber.Value
  10.  
  11. With rs
  12. Do Until .EOF
  13. If .Fields(1) = PNum Then
  14. .Edit
  15. .Fields(6) = Attachments.CurrentAttachment
  16. .Update
  17. Else: If .Fields(1) <> PNum Then .AddNew
  18. .Fields(1) = PNum
  19. .Fields(2) = CWO.Value
  20. .Fields(3) = MWO.Value
  21. .Fields(4) = PIR.Value
  22. .Fields(5) = Description.Value
  23. .Fields(6) = Attachments.CurrentAttachment
  24. .MoveNext
  25. End If
  26. Loop
  27. End With
  28.  
  29. rs.Close
  30. db.Close
  31.  
  32.  
  33. MsgBox ("You have successfully added the attachment")
  34. End Sub
  35.  
  36.  
  37. Private Sub Command3_Click()
  38. DoCmd.OpenForm ("Main Menu")
  39. DoCmd.Close acForm, ("Attachments")
  40. End Sub
  41.  
  42.  
  43. Private Sub Form_Load()  
  44.  
  45. 'All Code below this point works flawlessly, it is posted here as a reference of the logic attached to the form 
  46.  
  47. PNumber.Value = [Forms]![Assemble Attachments]![List19].Column(0)
  48. CWO.Value = [Forms]![Assemble Attachments]![List19].Column(1)
  49. MWO.Value = [Forms]![Assemble Attachments]![List19].Column(2)
  50. PIR.Value = [Forms]![Assemble Attachments]![List19].Column(3)
  51. Description.Value = [Forms]![Assemble Attachments]![List19].Column(4)
  52.  
  53. Dim rs As DAO.Recordset
  54. Dim db As DAO.Database
  55. Dim PNum As String
  56. Set db = CurrentDb
  57. Set rs = db.OpenRecordset("Attachments", dbOpenDynaset)
  58.  
  59. PNum = PNumber.Value
  60.  
  61. With rs
  62. Do Until .EOF
  63. If .Fields(1) = PNum Then TorF.Value = True Else: If .Fields(1) <> PNum Then TorF.Value = False
  64. .MoveNext
  65. Loop
  66. End With
  67. End Sub
  68.  
  69.  
  70.  
Jun 20 '13 #1

✓ answered by Rabbit

Your recordset should not return record. It should just return the record you're looking for. Then you can use the record count to let you know if there is a match or not.

Basically something like this:
Expand|Select|Wrap|Line Numbers
  1. Set rs = db.OpenRecordset("select * from someTable where idField = " & id, dbOpenDynaset)
  2.  
  3. If rs.RecordCount > 0 Then
  4.    ' do something
  5. Else
  6.    ' do something else
  7. End If

17 2660
Rabbit
12,516 Expert Mod 8TB
Your recordset should not return record. It should just return the record you're looking for. Then you can use the record count to let you know if there is a match or not.

Basically something like this:
Expand|Select|Wrap|Line Numbers
  1. Set rs = db.OpenRecordset("select * from someTable where idField = " & id, dbOpenDynaset)
  2.  
  3. If rs.RecordCount > 0 Then
  4.    ' do something
  5. Else
  6.    ' do something else
  7. End If
Jun 20 '13 #2
I am getting an error in the following line:

Expand|Select|Wrap|Line Numbers
  1. .Fields(6) = Attachments.CurrentAttachment
Method 'Value' of object 'Field2' failed

I have no clue how to handle this either
Jun 20 '13 #3
Also this is generating an error

Expand|Select|Wrap|Line Numbers
  1. Set rs = db.OpenRecordset("Select * From Attachments Where Project = " & PNumber.Value, dbOpenDynaset)
  2.  
Jun 20 '13 #4
Expand|Select|Wrap|Line Numbers
  1. Dim rs As DAO.Recordset
  2. Dim db As DAO.Database
  3. Dim PNum As String
  4. Set db = CurrentDb
  5. Set rs = db.OpenRecordset("Select * From Attachments Where Project = " & PNumber.Value, dbOpenDynaset)
  6. 'error generated on the above line
  7.  
  8. PNum = PNumber.Value
  9.  
  10.  
  11. With rs
  12. Do Until .EOF
  13. If rs.RecordCount > 0 Then
  14. .Edit
  15. .Fields(6) = Attachments.CurrentAttachment
  16. .Update
  17. Else:
  18. .AddNew
  19. .Fields(1) = PNum
  20. .Fields(2) = CWO.Value
  21. .Fields(3) = MWO.Value
  22. .Fields(4) = PIR.Value
  23. .Fields(5) = Description.Value
  24. .Fields(6) = Attachments.CurrentAttachment
  25. .MoveNext
  26. End If
  27. Loop
  28. End With
  29.  
  30. rs.Close
  31. db.Close
Jun 20 '13 #5
Rabbit
12,516 Expert Mod 8TB
CurrentAttachment merely returns the index of the file that is active in an attachment object. It does not represent the file itself. To actually copy the binary data of a file into an attachment field, you need to use the LoadFromFile method of the field object.

An example of that can be found here: http://blogs.office.com/b/microsoft-...cess-2007.aspx

As far as your second error. You haven't told us what the error message is so there's not much I can use to figure out what's happening. If I had to guess, I would say the query is written incorrectly, ie a table name was mispelled or a field name was mispelled or you used the wrong data type.
Jun 20 '13 #6
The Error was posted in message three,

It says "Method 'Value' of object 'Field2' failed"
Jun 20 '13 #7
Is it trying to compose an array? That is what I think of when I see the Field2 there

Thanks for the help Rabbit
Jun 20 '13 #8
Rabbit
12,516 Expert Mod 8TB
That's your first error message. I already answered that in the first paragraph of my previous post along with a link to sample code.

I was referring to your second error with the query that you never provided the error message for.
Jun 20 '13 #9
Data type mismatch in criteria expression
Jun 20 '13 #10
Sorry about that Rabbit
Jun 20 '13 #11
Rabbit
12,516 Expert Mod 8TB
That's what I thought it would be. You just need to match up your data types in your where clause. I'm guessing it's a text and not a numeric data type. Just surround the value in single quotes.
Jun 20 '13 #12
I have tried both single and double quotes in my where statement without a good result.

Here is my original code

Expand|Select|Wrap|Line Numbers
  1. Set rs = db.OpenRecordset("Select * From Attachments Where Project = " & PNumber.Value, dbOpenDynaset)
I have placed single quotes before Pnumber and after.value

I have also tried declaring a variable and placing the number in the variable

so for instance,

Expand|Select|Wrap|Line Numbers
  1. Dim Pnum as string
  2. Pnum = 'Pnumber.value'
All of these scenarios either generate syntax or the data type mismatch errors.

I'm sorry If I am not following what you are saying
Jun 20 '13 #13
Rabbit
12,516 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. Set rs = db.OpenRecordset("Select * From Attachments Where Project = '" & PNumber.Value & "'", dbOpenDynaset) 
Jun 20 '13 #14
Is there a reason my Else statement would not be working?

Expand|Select|Wrap|Line Numbers
  1. Private Sub Command1_Click()
  2.  
  3. Dim rs As DAO.Recordset
  4. Dim db As DAO.Database
  5. Dim PNum As String
  6. Set db = CurrentDb
  7.  
  8. PNum = PNumber.Value
  9.  
  10. Set rs = db.OpenRecordset("Select * From Attachments Where Project = '" & PNumber.Value & "'", dbOpenDynaset)
  11.  
  12.  
  13.  
  14. With rs
  15. Do Until .EOF
  16. If rs.RecordCount > 0 Then
  17. .Edit
  18. .Fields(6) = Attachments.CurrentAttachment
  19. .Update
  20. Else:
  21. .AddNew
  22. .Fields(1) = PNum
  23. .Fields(2) = CWO.Value
  24. .Fields(3) = MWO.Value
  25. .Fields(4) = PIR.Value
  26. .Fields(5) = Description.Value
  27. .Fields(6) = Attachments.CurrentAttachment
  28. .MoveNext
  29. End If
  30. Loop
  31. End With
I tested it with some stored records I have in the database and the "true" statement works but the else statement will not add the value. The code window initiates the code and runs it without error, I know that I have to change the Fields(6) but the initial data should store IE Fields 1-5 I would think

Thanks again
Jun 20 '13 #15
PS i have rs.close and db.close i did not copy and paste it on accident
Jun 20 '13 #16
zmbd
5,501 Expert Mod 4TB
- - Sounds like we have some housekeeping to take careof in the code. If you will follow the first few steps in the following link (and please read the rest) we can start getting thru your code: > Before Posting (VBA or SQL) Code
You may need to repeat the "compile" step several times as the compiler will stop on the first error found. You then need to fix that error and then re-run the compile until you nolonger receive an error.

- - You note that you have a db.close in your code, I do not see where you opened the database... you've only set the pointer Set db = CurrentDb a rule of thumb is to close ONLY those things that you've opened to to release ONLY those things that you have set. So in your case you need somthing like:
Expand|Select|Wrap|Line Numbers
  1. '(...)
  2. rs.close 'close the record set so that you wont lose stuff.
  3. Set rs = Nothing 'release the pointer to the recordset.
  4. Set db = Nothing 'release the pointer to the database.
  5. '(...)
  6.  

Also, IMHO we've moved offtopic to the OP starting with post #3. I Rabbit answered your original question in post#2. We can split the thread at that point if you will suggest a title.
Jun 20 '13 #17
Get back to you on Monday Rabbit, Thanks the work week is over..

Take care and thanks for the help.

LonelyKeyboard
Jun 20 '13 #18

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

Similar topics

6
by: RC | last post by:
My code below will loop through all the records in the table, and when the if statement is true it goes to the ***Me.ContainerNumberProductsTable = GETContainerNumber.Value*** bit like should but...
4
by: kanones | last post by:
I have some data that is been retrieved from a call to sql server stored procedure that I want to store for a period of time in a web farm architecture. I want to minimize the calls to sql server...
1
by: Peter | last post by:
I found this code and it works great. Is there any way to up date the code to export attachments to (c:\Exports)? =================================================== Dim olFolderCalendar As...
4
by: Stinky Pete | last post by:
Hi, In order to keep the file that's in development up to date with the reference (and still used) file, I need to regularly import the original main table and then physically select and append...
1
by: hamachi | last post by:
I want to open a specific form when a table record is 'loaded'. I have a large table that contains different client records. Thank you for your assistance.
16
by: mim77 | last post by:
Hi, I'm very rusty with Access programming. I hope someone can help me. I'm trying to include a count of the number of specific records in a field. Values for the records in the field are either...
3
by: Okonita via DBMonster.com | last post by:
Hi all, Working on a little shell script to remove specific records from a flat file. I have scanned a one or two script books and googled for answers but have no success. Can someone show me how...
1
WyvsEyeView
by: WyvsEyeView | last post by:
I have a dataset subform on which I want to lock a field for just two specific records. Each record has a type_id and a desc field. I want to test the value of the type_id field for the current...
8
by: KPR1977 | last post by:
Below is a very handy function that transposes fields from "Table1" into "Table2". Note, in Table1 the Field2, Field3, etc represent Option codes. Basically it takes this... Table1 ProdBreakDown...
4
by: neelsfer | last post by:
I have specific medicines in a table consisting of about 3000 records, that cause problems when its captured at stocktake every year. My question - can i change the colour of these spedific...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.