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
- Private Sub Command1_Click() '(button Update Project)
- Dim rs As DAO.Recordset
- Dim db As DAO.Database
- Dim PNum As String
- Set db = CurrentDb
- Set rs = db.OpenRecordset("Attachments", dbOpenDynaset)
- PNum = PNumber.Value
- With rs
- Do Until .EOF
- If .Fields(1) = PNum Then
- .Edit
- .Fields(6) = Attachments.CurrentAttachment
- .Update
- Else: If .Fields(1) <> PNum Then .AddNew
- .Fields(1) = PNum
- .Fields(2) = CWO.Value
- .Fields(3) = MWO.Value
- .Fields(4) = PIR.Value
- .Fields(5) = Description.Value
- .Fields(6) = Attachments.CurrentAttachment
- .MoveNext
- End If
- Loop
- End With
- rs.Close
- db.Close
- MsgBox ("You have successfully added the attachment")
- End Sub
- Private Sub Command3_Click()
- DoCmd.OpenForm ("Main Menu")
- DoCmd.Close acForm, ("Attachments")
- End Sub
- Private Sub Form_Load()
- 'All Code below this point works flawlessly, it is posted here as a reference of the logic attached to the form
- PNumber.Value = [Forms]![Assemble Attachments]![List19].Column(0)
- CWO.Value = [Forms]![Assemble Attachments]![List19].Column(1)
- MWO.Value = [Forms]![Assemble Attachments]![List19].Column(2)
- PIR.Value = [Forms]![Assemble Attachments]![List19].Column(3)
- Description.Value = [Forms]![Assemble Attachments]![List19].Column(4)
- Dim rs As DAO.Recordset
- Dim db As DAO.Database
- Dim PNum As String
- Set db = CurrentDb
- Set rs = db.OpenRecordset("Attachments", dbOpenDynaset)
- PNum = PNumber.Value
- With rs
- Do Until .EOF
- If .Fields(1) = PNum Then TorF.Value = True Else: If .Fields(1) <> PNum Then TorF.Value = False
- .MoveNext
- Loop
- End With
- End Sub