Hi guys, time for another newbie question.
I'm "improving" an existing Table
It's a table of books where the author is unfortunately specified in one of two ways - "Joe Soap", or "Soap, Joe" (it's a historical thing!).
I decided to tidy up the Book Table by running down the table extracting the author's forename(s) and surname, using this data to create a brand new Author Table with an unique primary key , then adding the Author's primary key to the existing Book Table as a foreign key.
All reasonable so far.
I used the "if I can't find an Author entry using SQL, then add a new Author" technique.
Then I did something silly - I started thinking!
Why don't I record the number of Books by each author?
The amended technique was as before, but with additional processing so that if I DID match an Author, I could update a "Hit Count".
Hmm!
I'm using DAO to open the Book and Author tables as Recordsets.
Note - in the code snippets below, anything not "Dim"med in the code has already been declared "universally".
I'm reading the Book table sequentially, and getting the data into AuthorFname and AuthorSname (both strings). Then I get a bit giddy (and I suspect that this is where the problems start), 'cos I use a Function to get hold of the (possibly existing) Author record.
(I have to use double quotes, otherwise Edna O'Brien screws the system!)
If the Author DOESN'T already exist, I add an entry as below.
Now comes the somewhat arthritic bit.
What I want to do (assuming I've found a matching record) is to simply update it's Hit Count.
Unfortunately, the only way I seem to be able to get addressability to the record to update it is to pick out the foreign key from the SQL based returned record (kVal in the code below), do a MoveFirst, then a Move!
There's GOT to be a better way to get addressability to the record I wish to update. Unfortunately, in my researches all I've found is
MoveFirst,
MoveLast,
MoveNext, and
Move n records.
Can anyone point me in the right direction?
I'm "improving" an existing Table
It's a table of books where the author is unfortunately specified in one of two ways - "Joe Soap", or "Soap, Joe" (it's a historical thing!).
I decided to tidy up the Book Table by running down the table extracting the author's forename(s) and surname, using this data to create a brand new Author Table with an unique primary key , then adding the Author's primary key to the existing Book Table as a foreign key.
All reasonable so far.
I used the "if I can't find an Author entry using SQL, then add a new Author" technique.
Then I did something silly - I started thinking!
Why don't I record the number of Books by each author?
The amended technique was as before, but with additional processing so that if I DID match an Author, I could update a "Hit Count".
Hmm!
I'm using DAO to open the Book and Author tables as Recordsets.
Code:
Set dbs = CurrentDb 'Use the current database.
Set rstBook = dbs.OpenRecordset("Book", dbOpenTable)
Set rstAuthor = dbs.OpenRecordset("Author", dbOpenTable)
I'm reading the Book table sequentially, and getting the data into AuthorFname and AuthorSname (both strings). Then I get a bit giddy (and I suspect that this is where the problems start), 'cos I use a Function to get hold of the (possibly existing) Author record.
Code:
'*
'** See if the Author already exists.
'*
critStr = "Surname = """ & AuthorSname & _
""" and " & _
"Forenames = """ & AuthorFname & """"
LocFlag = LocateAuthor("[pkAuthor]", "Author", critStr)
Code:
Public Function LocateAuthor(strField As String, _
strTable As String, _
strWhere As String)
Dim rs As DAO.Recordset 'Related records
Dim strSql As String
'*Initialise result
LocateAuthor = 0
'*build SQL string
strSql = "SELECT " & strField & " FROM " & strTable
strSql = strSql & " WHERE " & strWhere
Set rs = DBEngine(0)(0).OpenRecordset(strSql, dbOpenDynaset)
If rs.RecordCount > 0 Then
kVal = rs.Fields("pkAuthor").Value
LocateAuthor = 1
End If
Set rs = Nothing
End Function
If the Author DOESN'T already exist, I add an entry as below.
Code:
Sub NoMatchX()
AuthorPtr = AuthorPtr + 1
With rstAuthor
.AddNew
!pkAuthor = AuthorPtr
!Forenames = AuthorFname
!Surname = AuthorSname
![Hit Count] = 1
.Update
.Bookmark = .LastModified
End With
End Sub 'NoMatchX
What I want to do (assuming I've found a matching record) is to simply update it's Hit Count.
Unfortunately, the only way I seem to be able to get addressability to the record to update it is to pick out the foreign key from the SQL based returned record (kVal in the code below), do a MoveFirst, then a Move!
Code:
Sub MatchX()
Dim tmp As Long
'*
'* Update Author Record.
'*
With rstAuthor
.MoveFirst
.Move kVal - 1
End With
tmp = rstAuthor.Fields("Hit Count").Value + 1
With rstAuthor
.Edit
![Hit Count] = tmp
.Update
.Bookmark = .LastModified
End With
End Sub 'MatchX
There's GOT to be a better way to get addressability to the record I wish to update. Unfortunately, in my researches all I've found is
MoveFirst,
MoveLast,
MoveNext, and
Move n records.
Can anyone point me in the right direction?