New Access user needs help.
I found an old post that pretty muched walked me through writing this code except that I get an error on the SQL update statement in the Where clause. In the update statement I am trying to update the last name and first name fields (which I split into two from the name field) for each current id. Can someone tell me the correct syntax to set the Where [campus ID] = currentidnumber. The code I have is not working.
Error Message-
Run time error '3075':
Syntax error (missing operator) in query expression '[Campus ID] = &rs![Campus ID]'.
Thanks for any help.
Timo
Code:
Option Compare Database
Option Explicit
Public Sub UpdateNameRecord()
Dim db As Database
Dim rs As Recordset
Dim Lastname As String
Dim FirstnamePlusInitial As String
Dim Firstname As String
Set db = CurrentDb
Set rs = db.OpenRecordset("Select * from [CNSM Students]")
'Cycle through each record and extract the information you want to use.
Do While Not rs.EOF
'Find the location of the "," and extract the details to the left of it
Lastname = Left(rs![Name], InStr(rs![Name], ",") - 1)
'Find the location of the "," and extract the details to the right of it
FirstnamePlusInitial = Mid(rs![Name], InStr(rs![Name], ",") + 1)
'Find the location of the " " and extract the details to the right of it
Firstname = Left(FirstnamePlusInitial, InStr(FirstnamePlusInitial, " ") - 1)
'Run an update query
DoCmd.RunSQL "UPDATE [CNSM Students] SET [Last Name] = '" & Lastname & "', [First Name] = '" & Firstname & "' Where [Campus ID] = &rs![Campus ID]"
'Complete the loop and close the recordset
Loop
rs.Close
End Sub