J
James S. Rice
How do I retrieve the value of a field in a record in an
MS Access 2002 table using Visual Basic?
MS Access 2002 table using Visual Basic?
How do I retrieve the value of a field in a record in an
MS Access 2002 table using Visual Basic?
John,
Thanks for the quick response.
I have a database, xyz.mdb. Within xyz.mdb is a table
called Individuals. Within Individuals is a field called
EmailAddress which is self explanatory. I wish to write
some code in a VBA module that will send an email (I
believe via the SendObject property) at the first of each
month to each individual in the Individuals table. I've
tried the ADO approach, but I lack the experience of
working with recordsets, etc.
Another responder mentioned DLookUp as well.
Which approach do you think would work best for what I am
trying to do?
John,
Thanks again for the support.
I copied and pasted the code you supplied but it didn't
work outright. I'm not sure if it is an engine problem or
what. However, after a bit of trial and error, I was able
to finally get the following to work:
Set db = DBEngine.OpenDatabase("D:\...\xyz", False, True)
Set grp_rs = db.OpenRecordset("Groups")
Set ind_rs = db.OpenRecordset("Individuals")
Do Until ind_rs.EOF
myGroup = ind_rs!GroupName
Do Until grp_rs.EOF
If grp_rs!GroupID = myGroup Then
If grp_rs!Active = True Then
If ind_rs!Active = True Then
argTo = ind_rs!EmailAddress
DoCmd.SendObject ...
grp_rs.MoveFirst
Exit Do
End If
End If
grp_rs.MoveFirst
Exit Do
End If
grp_rs.MoveNext
Loop
ind_rs.MoveNext
Loop
With the recordset problem now in hand, I'm getting an "E-
Mail Error 2046 - The command or action 'SendObject' isn't
available now." message when I try to execute
DoCmd.SendObject. Do you happen to know what could be
causing this error?
I only have one hang up: the GroupName field in the
Individuals table is a look-up to the GroupID field in the
Groups table. If I set fldGroupName = rs!GroupName, I get
Run-time error '3265': Item not found in this collection.
If I set fldGroupName = rs!GroupID, the value returned is
a number (Groups primary key?), not text. How do I get the
text from one of the GroupName fields? There is a
GroupName field in each table.
John,
I'm running a telephony application that pulls records
from the Indivudals table and the app needs to know in
real-time to which Group each Individual belongs, hence,
the Group reference in the Individual's record via LookUp.
I understand what you mean, though, about the data
existing in only one table. I know that's how it is
supposed to be, but I would have a difficult time working
around it at this point.
My current query looks like this:
strSQL = "SELECT * FROM Groups INNER JOIN Individuals" _
& " ON Groups.GroupID = Individuals.GroupName" _
& " WHERE Groups.Active = True" _
& " AND Individuals.Active = True;"
How do I add the Groups table to this query, joining it to
Individuals by GroupID? I'm not familiar enough with this
type of query yet.
Once the query is updated, does the assignment look like
fldGroupName = rs!Groups.GroupName, or would this be legal
syntax?
Thanks again.
Q: Are you storing a (numeric) ID in the GroupName field
in Individuals?
A: YES.
Q: Is this a <yuck, PTOOIE!> Lookup field?
A: YES. I'M STARTING TO GET THE IMPRESSION THAT LOOKUP
FIELDS ARE BAD?
I'm going to look into the relationship/foreign key
approach.
Q: What fields do you actually have in the Individuals
table pertaining to groups?
A: GROUPNAME
Q: Do you have a GroupID field, a GroupName field, or both?
A: GROUPID AND GROUPNAME FIELDS IN GROUPS TABLE AND A
GROUPNAME FIELD IN INDIVIDUALS TABLE.
Q: Is either of these a Lookup field?
A: YES. THE GROUPNAME FIELD IN INDIVIDUALS TABLE IS A
LOOKUP TO GROUPID FIELD IN GROUPS TABLE. SIMILARLY, THERE
IS A TITLE FIELD IN INDIVIDUALS TABLE THAT IS A LOOKUP TO
TITLEID FIELD IN TITLES TABLE.
ok.
I played around with it a bit and finally gained success
with the following SQL statement. Note the abscence of the
* parameter in the SELECT:
strSQL = "SELECT Titles.Title, Individuals.FirstName," _
& " Individuals.MiddleName," _
& " Individuals.LastName," _
& " Individuals.EmailAddress," _
& " Groups.GroupName," _
& " Individuals.PrimaryPhone," _
& " Individuals.SecondaryPhone," _
& " Individuals.TertiaryPhone," _
& " Individuals.VoicePager," _
& " Individuals.DigitalPager," _
& " Individuals.Designee," _
& " Individuals.Contact" _
& " FROM Titles" _
& " INNER JOIN (Groups INNER JOIN Individuals" _
& " ON Groups.GroupID=Individuals.GroupName)" _
& " ON Titles.TitleID=Individuals.Title" _
& " WHERE (((Groups.Active)=True) AND" _
& " ((Individuals.Active)=True));"