Hello Doug
Brilliant, Doug. I actually managed to add this in and get it to work in
just a few minutes. Must be some kind of record for me!
strDBQuery, and all the other strings regarding database names and file
locations are set by user input or by reading Registry entries. On first
use, the user is presented with a dialogue which prompts them to locate the
path and file name of their database, and then prompts them for the table
name they wish to link to. The same is true of query names, so that users
can have any name they wish, but as long as the format is correct (the right
number of columns, with the right names, as specified in my user guide
documents) their database will work with my script.
Now, to make things even more complicated, I have to get the same thing
working for Word 2000. Because the intended users of this script are split
on two different versions of Word I have to write all this so it will work
for users with either/or versions of Word. At present I have the two working
scripts which connect to Access through Word 97 and Word 2000. Now I also
have the query version working in 97, and have managed to get it working in
2000 (I know it sounds long winded, but it works so far. Hopefully you won't
mind me asking for more help with this). But I can't click on a listbox
entry in the 2000 version to get that entry to display in the textboxes.
The Access connection script I have is repeated below, as is the lstCompany
script. I think that for a programmer it will be easy to amend so that it
reads the lastbox data and transferrs the required entry to the textboxes.
Unfortunately I've already spent the best part of another day trying this
with no luck...
Best wishes
Peter L Kessler
[email protected]
(My usual address,
[email protected] is going to be rested temporarily to
help my fight against masses of spam, so please use my home address if you
need to.)
*****************************************
'Word 2000 connection to Access database
Private Sub optDB_Click()
' Clear object to free up memory
Set mrstAddressees = Nothing
Set mcnnCESurvey = Nothing
' ADO database connection using Jet provider for an Access db
mcnnCESurvey.ConnectionString = Replace$(mcDBConnectionString, "%1",
strDBPath)
mcnnCESurvey.Open
If mcnnCESurvey.State <> adStateOpen Then
MsgBox "Failed to establish database connection: " & _
strDBPath, vbInformation
Exit Sub
End If
lstCompany.Clear
With mrstAddressees 'MyRecordSet
' .Open strDBTable, mcnnCESurvey, _
'This is the only change I made to get this connection working for a
query
.Open strDBQuery, mcnnCESurvey, _
adOpenKeyset, adLockOptimistic, adCmdTableDirect
'.Index = "PrimaryKey"
.MoveFirst
Do Until .EOF
' If Company name exists
If ![Company] <> "" Then
' If Name exists, add both together
If ![Company] <> "" Then
lstCompany.AddItem ![Company] & ", " & ![name]
lstCompany.List(lstCompany.ListCount - 1, 1) = ![ID]
Else
lstCompany.AddItem ![Company]
lstCompany.List(lstCompany.ListCount - 1, 1) = ![ID]
End If
End If
.MoveNext
Loop
End With
'And without a Query, this is how an entry is transferred to the textboxes
in Word 2000:
lstCompany_Click()
Dim strKey As String
With lstCompany
If .ListIndex < 0 Then GoTo Ending
strkey = .List(.ListIndex, 1)
End With
With mrstAddressees 'MyRecordSet
.Seek Array(strkey), adSeekFirstEQ
If .EOF Then
MsgBox "Unable to seek to: " & ", Company: " &
strkey, vbOKOnly, Me.Caption
GoTo Ending
End If
' Populate Form controls
txtToAddress2.Text = AssignBlankIfNull(![name])
txtToAddress3.Text = AssignBlankIfNull(![Company])
txtToAddress4.Text = AssignBlankIfNull(![ADDRESS1])
txtToAddress5.Text = AssignBlankIfNull(![ADDRESS2])
txtToAddress6.Text = AssignBlankIfNull(![ADDRESS3])
txtToAddress7.Text = AssignBlankIfNull(![City])
txtToAddress8.Text = AssignBlankIfNull(![PostCode])
txtToAddress10.Text = AssignBlankIfNull(![Fax])
txtToAddress11.Text = AssignBlankIfNull(![TEL])
txtToAddress12.Text = AssignBlankIfNull(![County])
txtToAddress13.Text = AssignBlankIfNull(![Country])
End With
End Sub
Public Function AssignBlankIfNull(ByVal varValue As Variant) As String
AssignBlankIfNull = IIf(IsNull(varValue), vbNullString, varValue)
End Function ' AssignBlankIfNull