Question : Update Temp Recordset using VBA

Hello EE
I need to update a value in a recordset.  My approach is not working...sigh.  Can you correct the code below?

Thanks,

LVBarnes
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
Public Function xxx()
    Dim frst As New ADODB.Recordset
    Dim strSql2 as String

    strSql2 = "SELECT tblAnalystCustXRef.TBPPath, tblAnalystCustXRef.DefaultCustNbr, tblAnalystCustXRef.AnalystName, tblAnalystCustXRef.PayMethod, tblAnalystCustXRef.ClaimTypeDesc, '' as ArchFileName, tblAnalystCustXRef.ArchivePath " & _
              "FROM tblAnalystCustXRef " & _
              "WHERE tblAnalystCustXRef.TBPPath IS NOT NULL " & _
                  "AND tblAnalystCustXRef.Active = 1 " & _
                  "AND tblAnalystCustXRef.AnalystName = '" & _
                  Forms!frmLogon.cboLogon & "';"

    With frst
        'I tried both of these
	'.Open strSql2, CurrentProject.Connection, adOpenStatic, adLockReadOnly 'Original
        '.Open strSql2, CurrentProject.Connection, adOpenDynamic, adLockOptimistic 'Changed to allow updates
   Do While Not .EOF
	' if the folder exists,
        If Not frst(0) Is Nothing Then
            If fso.FolderExists(frst(0)) Then
                ' set the folder
                Set fld = fso.GetFolder(frst(0))
                ' loop through the files in the folder,
                For Each fil In fld.Files
                    ' if the file is one of the target types,
                    If LCase(Right(fil.Name, 3)) = "xls" _
                        Or LCase(Right(fil.Name, 4)) = "xlsx" _
                        Or LCase(Right(fil.Name, 3)) = "txt" _
                        Or LCase(Right(fil.Name, 3)) = "csv" Then

                        'This section is added to copy the file name to ArchFileName field in the recordset
                        If InStr(fil.Name, "'") > 0 Then
                            CurrentDb.Execute "UPDATE frst SET frst(5)='" & fil.Name & _
                                              "' WHERE frst(0)='" & frst(0) & _
                                              "' AND frst(1)=" & frst(1) & _
                                              " AND frst(2)='" & frst(2) & _
                                              "' AND frst(3)='" & frst(3) & _
                                              "' AND frst(4)='" & frst(4) & "';"

Answer : Update Temp Recordset using VBA

The cursor and lock types are fine.  You can't perform an update on an unbound field.  If you take your SQL statement and paste it into an Access query and then change the query to an Update query and try to update ArchFileName to something you'll get a prompt for a parameter value for ''.  If you enter something in the prompt you'll receive a message indicating the field is not updateable.

I am guessing you do not want to update/modify the data in the table tblAnalystCustXRef but I'm not sure I understand what you are trying to accomplish then.  If you simply need to use the value of fil.Name in other parts of the module you can assign the value to a public variable.  You can also add a field to the table expressly for this purpose so you can update it using your current procedure.

OM Gang
Random Solutions  
 
programming4us programming4us