Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help Using VBA Query 1

Status
Not open for further replies.

egstatus

Programmer
Apr 14, 2005
143
US
Hi,
I have this query in access:
Code:
UPDATE PAYROLL SET PayRollToDate = CDate( Format$([PayRollToDate], "MM/DD/") & "2012" )
WHERE Year([PayRollToDate]) = 2010;
It runs fime from the query object. Now I want to use it in VBA, I have about 75 tables And I want to use code to do the update. All it does is, it changes the year part of the PayRollToDate field from 2010 to 2012.

I have tryed the following.

Code:
Function UpdateMpayTables()
    Dim db As Database
    Dim tdf As TableDef
    Dim currentdb As Database
    Dim strSQl As String
        
    Set currentdb = Application.currentdb
     
    'For Each tdf In db.TableDefs
    For Each tdf In currentdb.TableDefs
        If (Left(tdf.Name, 4)) = "MPAY" Then
            If Len(Trim(tdf.Name)) = 8 Then
                lngRecordsInTable = lngRecordsInTable + tdf.RecordCount
                DoCmd.SetWarnings False
                 
                 [highlight #EF2929] This is where by problem is[/highlight]                 
                 'Year Field to Update
                 strSQl = "UPDATE " & tdb.name & _
                 " SET PayRollToDate = # " & CDate(Format$(tdf.Name &".PayRollToDate" &, "MM/DD/") & "2012") & "#" & _
                 " WHERE Year([PayRollToDate]) = 2010;"
                             
                 DoCmd.RunSQL strSQl
                 intTableUpdated = intTableUpdated + 1
             End If
        End If
    Next tdf
End Function

Can someone please help me?

Thanks

Ed
 
I think this might work:

Code:
strSQl = "UPDATE " & tdb.name & _
" SET PayRollToDate = DateAdd('yyyy',2, PayRollToDate)" & _
" WHERE Year([PayRollToDate]) = 2010"

This assumes you don't have any spaces in your table names.

Duane
Hook'D on Access
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top