Quote:
>However, Stephen, I am interested in learning to use the DAO/SQL query
>techniques. From what you're saying, I could save a comma delimited
>file as a DBF file or I guess use an Excel SS for my data source. And
>of course I could create the file in Access and query the MDB file.
WARNING: You're best off copying all the below into a word processor and setting
your font to a small-point fixed font like Courier so it's easier to read.
Okay, here's a typical example ... I want to pop up a form that shows a list of
employees. The user clicks on a name to select it and perform some function.
(Although they could double-click it which would be a typical Windows event, I find a
lot of users aren't that handy with a mouse, so I have them click once on the name
then click a button below like "OK" or "Edit" or "Delete" or whatever.) The list of
names in the list box comes from a table in an Access database.
One scenario where we use this is in a clerical pool. They generate a
pre-programmed letter then select from the list box the name of the employee whose
signature will appear at the bottom of the letter. The list box displays only last
name and first name in the format "Doe, John" but when the user clicks "OK" it
retrieves from the database other fields, such as job title, phone number, fax
number, signature initials, etc.
First, let's create our database. We'll call it Employees and store it on a
server in the folder P:\DBS. (P: being a mapped drive on the file server.) In the
Employees database, we'll create a table called tblEmployees. The tblEmployees table
has these fields:
AutoID 'In this example, we'll use an AutoNumber assigned by Access
LastName
FirstName
Signature '(for example, I sign "Stephen C. Smith")
Title
Initials
Phone
Fax
Now, for the Word template ... I create a module called "Macros" but there are
many different ways to do this. I've seen one example where they used the Word
object "ThisDocument" to store the code.
In the Macros module, I start by creating some global variables. Mine are:
Public db As Database
Public rs As Recordset
Public i As Integer
Public iAutoID As Integer
Public iChoice as Integer
Public strName As String
Public strSQL As String
I also create a global constant for the database itself:
Public Const cDb As String = "P:\DBS\Employees.mdb"
This makes it easier to port the code to other templates or other applications;
I just change the value of cDb to whatever database has the data being used to
populate the list box.
Then we create the UserForm, which I'll call frmEmployees. Place on
frmEmployees a list box we'll call lstEmployees. The lstEmployees object has these
properties:
BoundColumn = 1 'The first column will be the unique AutoID used in the
table to ID the record
ColumnCount = 2 'Column 1 will be the AutoID; Column 2 will be a
concatenation
ColumnHeads = False 'True would display the field names and we don't want that!
ColumnWidths = 0 pt; 150 pt. 'Column 1 is 0 pt. because we don't want to
display
'the Auto ID, just the name. You'll probably have to play
'with the Column 2 width to see what works for you.
Here's the code that I place in the UserForm_Initialize event:
'--------------------------------------------------------------------
Private Sub UserForm_Initialize()
'This will hold the array of values to be displayed.
Dim RepArray()
Set db = OpenDatabase(cDb, False, True)
'Define the SQL string to be used in retrieving the recordset.
strSQL = "SELECT AutoID, LastName, FirstName FROM tblEmployees ORDER BY
LastName, FirstName"
'Open the recordset.
Set rs = db.OpenRecordset(strSQL)
'Redim the array with the values retrieved by the recordset.
ReDim RepArray((rs.RecordCount - 1), 2)
'Populate the array with records for the lstEmployees list box.
'iAutoID holds the AutoNumber (unique ID).
'strName is a concatenation of the Last Name and First Name fields.
For i = 0 To (rs.RecordCount - 1)
iAutoID = rs.Fields("AutoID")
strName = rs.Fields("LastName") & ", " & rs.Fields("FirstName")
RepArray(i, 0) = iAutoID
RepArray(i, 1) = Trim(strName)
rs.MoveNext
Next i
'Populate the lstEmployees list box with the RepArray values.
frmEmployees.lstEmployees.List() = RepArray
'Set the ListIndex to 0 so no one name is a default.
frmEmployees.lstEmployees.ListIndex = 0
'Clear the DAO variables.
Set db = Nothing
Set rs = Nothing
End Sub
'--------------------------------------------------------------------
*** ONE BIG TRAP -- Make sure that in Tools References you set a reference to
your Microsoft DAO Object Library. It should be listed on the Tools References list
of available references. If not, search your computer for a file named DAO350.DLL or
similar. (Search for DAO*.DLL ...) Our inept I.S. department sometimes thinks it's
being clever by not doing a complete Office install and they leave off DLL's.
To determine which record the user selected, I place this code in the Click
event of the button they select (e.g. "OK"):
'--------------------------------------------------------------------
Private Sub cmdOK_Click()
'Column 1 has the AutoID number, so that's the value we use to retrieve
'a record from the database.
frmEmployees.lstEmployees.TextColumn = 1
'Assign the .Text property to iChoice, a global variable.
iChoice = frmEmployees.lstEmployees.Text
End Sub
'--------------------------------------------------------------------
From here, you can do pretty much anything you want because the global variable
iChoice contains the AutoID for the selected record. For example, you could retrieve
the record from the database with code similar to this:
'--------------------------------------------------------------------
Set db = OpenDatabase(cDb, False, True)
'Define the SQL string to be used in retrieving the recordset.
strSQL = "SELECT LastName, FirstName, Signature, Title, Initials, Phone, Fax " &
_
"FROM tblEmployees WHERE AutoID = " & iChoice
'Open the recordset.
Set rs = db.OpenRecordset(strSQL)
'--------------------------------------------------------------------
You could even delete the selected record:
'--------------------------------------------------------------------
Set db = OpenDatabase(cDb, False, True)
'Define the SQL string to delete the record
strSQL = "DELETE * FROM tblEmployees WHERE AutoID = " & iChoice
'Execute the SQL string
db.Execute strSQL
'--------------------------------------------------------------------
There are a lot of different ways to accomplish all the above, but this works
for me. I'm sure more experienced members of this newsgroup can suggest other ways
to do it. We used to use ODBC but this is faster and much easier to configure.
Stephen