If File Doesn't Exist, Update Recordset, Else Nothing

foxhound

Board Regular
Joined
Mar 21, 2003
Messages
182
Hello,

With the following code, I am trying to determine if a picture exists in a certain folder. If it does not exist, I want the current recordset field "sref" to be updated to "00000000.jpg" If it does exist, I would like to do nothing and loop to the next record. Any suggestions?

Sub UpdateTable()

Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim FileName As String
Dim Picture As String

Const FilePath = "C:\Data\Photos\"
Set db = CurrentDb()
Set rst = CurrentDb.OpenRecordset("SELECT * FROM ProductPhotos")
rst.MoveFirst
Do While Not rst.EOF
Picture = rst!xref
FileName = Dir(FilePath & Picture)
If FileName = vbNullString Then
rst.Edit
rst!xref = "00000000.jpg"
rst.Update
Else
'do nothing then go to next record
End If
rst.MoveNext
Loop
rst.Close
Set rst = Nothing
End Sub

Thanks for any help you can offer :)

Foxhound
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
OK, what's the problem? I tried your code and it seemed to work although in your post you said that you wanted to update field 'sref' but the code refers to 'xref'. Maybe that's your problem. Does this code work for you?

Code:
Sub UpdateTable()

    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim FileName As String
    Dim Picture As String

    Const FilePath = "C:\Data\Photos\"
    Set db = CurrentDb()
    Set rst = CurrentDb.OpenRecordset("SELECT * FROM ProductPhotos")
    rst.MoveFirst
    Do While Not rst.EOF
        Picture = rst!xref
        FileName = Dir(FilePath & Picture)
        If FileName = vbNullString Then
            rst.Edit
            rst!xref = "00000000.jpg"
            rst.Update
        Else
            'do nothing then go to next record
        End If
        rst.MoveNext
    Loop
    rst.Close
    Set rst = Nothing
End Sub
 
Upvote 0

Forum statistics

Threads
1,221,581
Messages
6,160,630
Members
451,661
Latest member
hamdan17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top