G
GHawkins
I'm working with an Access database (Access 2007). TableA is my main table
with data, and TableB contains some items that need to be updated in TableA.
The tables are set up as follows:
TableA:
EmplID (primary key)
QtrDate (primary key)
Code
TableB:
EmplID (primary key)
ChangeDate
Code
Right now, I am taking the first record in TableB, searching TableA for a
match, then comparing the dates. When TableA.ID = TableB.EmplID and
TableA.Date = TableB.ChangeDate, I update TableA.Code with the value in
TableB. This all works fine, but recently we've run into a problem where
there are extra spaces at the end of some data in the TableA.EmplID. Employee
1234 might show up as '1234' in some records, but as '1234 ' in others. When
there are extra spaces in TableA.ID, it is not seen as a match when I compare
it to TableB, so the Code is not updated. Is there a way to Trim the ID in
TableA before comparing it to TableB?
Here's the code - it's not something I wrote, just something I support. I
don’t work with VBA much.
Dim sqlquery As String
Dim Empl As Database, EmplCode as Recordset, Changes as Recordset, EmpQuery
As QueryDef
Set Empl = CurrentDb()
DoEvents
Set EmpQuery = Empl.CreateQueryDef()
EmpQuery.NAME = ""
sqlquery = "SELECT DBO_TableB.EmplID, DBO_TableB.EffectiveDate,
DBO_TableB.Category from DBO_TableB"
EmpQuery.SQL = sqlquery
DoEvents
Set Changes = EmpQuery.OpenRecordset(dbOpenForwardOnly)
Set EmplCode = Empl.OpenRecordset("TableA", DB_OPEN_TABLE)
If Not Changes.EOF Then
Do Until Changes.EOF
EmplCode.Index =â€EmplIDâ€
EmplCode.Seek “=â€, Changes(“EmplIDâ€)
If Not EmplCode.NoMatch Then
If EmplCode(“EmplIDâ€) = Changes(“EmplIDâ€) Then
Do Until EmplCode(“EmplIDâ€) <> Changes(“EmplIDâ€)
If EmplCode(“QtrDateâ€) >= Changes(“ChangeDateâ€) Then
EmplCode.Edit
EmplCode(“Categoryâ€) = Changes(“Categoryâ€)
EmplCode.Update
End If
EmplCode.MoveNext
If EmplCode.EOF = True Then
EmplCode.MoveFirst
End If
Loop
End If
End If
Changes.MoveNext
Loop
End If
Changes.Close
EmplCode.Close
Exit Sub
with data, and TableB contains some items that need to be updated in TableA.
The tables are set up as follows:
TableA:
EmplID (primary key)
QtrDate (primary key)
Code
TableB:
EmplID (primary key)
ChangeDate
Code
Right now, I am taking the first record in TableB, searching TableA for a
match, then comparing the dates. When TableA.ID = TableB.EmplID and
TableA.Date = TableB.ChangeDate, I update TableA.Code with the value in
TableB. This all works fine, but recently we've run into a problem where
there are extra spaces at the end of some data in the TableA.EmplID. Employee
1234 might show up as '1234' in some records, but as '1234 ' in others. When
there are extra spaces in TableA.ID, it is not seen as a match when I compare
it to TableB, so the Code is not updated. Is there a way to Trim the ID in
TableA before comparing it to TableB?
Here's the code - it's not something I wrote, just something I support. I
don’t work with VBA much.
Dim sqlquery As String
Dim Empl As Database, EmplCode as Recordset, Changes as Recordset, EmpQuery
As QueryDef
Set Empl = CurrentDb()
DoEvents
Set EmpQuery = Empl.CreateQueryDef()
EmpQuery.NAME = ""
sqlquery = "SELECT DBO_TableB.EmplID, DBO_TableB.EffectiveDate,
DBO_TableB.Category from DBO_TableB"
EmpQuery.SQL = sqlquery
DoEvents
Set Changes = EmpQuery.OpenRecordset(dbOpenForwardOnly)
Set EmplCode = Empl.OpenRecordset("TableA", DB_OPEN_TABLE)
If Not Changes.EOF Then
Do Until Changes.EOF
EmplCode.Index =â€EmplIDâ€
EmplCode.Seek “=â€, Changes(“EmplIDâ€)
If Not EmplCode.NoMatch Then
If EmplCode(“EmplIDâ€) = Changes(“EmplIDâ€) Then
Do Until EmplCode(“EmplIDâ€) <> Changes(“EmplIDâ€)
If EmplCode(“QtrDateâ€) >= Changes(“ChangeDateâ€) Then
EmplCode.Edit
EmplCode(“Categoryâ€) = Changes(“Categoryâ€)
EmplCode.Update
End If
EmplCode.MoveNext
If EmplCode.EOF = True Then
EmplCode.MoveFirst
End If
Loop
End If
End If
Changes.MoveNext
Loop
End If
Changes.Close
EmplCode.Close
Exit Sub