Hi Doug
I realise I'm sounding "difficult" by not wanting to handle Access Queries,
but the truth is I have no control over how users will set up their
databases. All I can hope to do is manipulate the way the basic data is used
within Word VBA. Hopefully you'll bear with me on this!
Thanks very much for your "fix" I managed to integrate it very easily, and
then spent a little time getting to know just how the data was being
handled.
It works perfectly, and displays the data as I wanted by adding columns 2
and 1 (Company+Name) of the database. The problem is that now when I click
on an entry in the listbox, the visible entry is Company+Name and this is
what is transferred to the "Name" textbox, when all I need there is the Name
on it's own (column 1).
I've tried all morning to work around this without any success, so is this
solvable, or will my head explode before I fix it? My current code is shown
below this message.
Best wishes
Peter
Kessler Associates
E: (e-mail address removed)
W:
http://homepages.tesco.net/~plk33/plk33/index.htm
****************************************
Private Sub optDB_Click() 'View database details in listbox
' Some settings removed here for the sake of brevity
' strDBPath is my database path
' strDBTable is my table name
' strDBQuery is my query name
' db is my database
' rs is my recordset
Dim i As Integer
Dim rs As Recordset
Dim db As Database
Dim NoOfRecords As Long
Dim MyString As String
' Open the database
Set db = OpenDatabase(name:=strDBPath)
Dim j As Integer, m As Integer, n As Integer
' Access the first record from a particular table
Set rs = db.OpenRecordset(strDBQuery, dbOpenForwardOnly)
' Get the number of fields in the table
j = rs.Fields.count + 1 '+1 added for 'Company, Name' listing
' Get the number of Records in the table
' Loop through all the records in the table until the end-of-file
marker is reached
i = 0
Do While Not rs.EOF
i = i + 1
' Access the next record
rs.MoveNext
Loop
rs.Close
' Set the number of columns in the listbox
'lstCompany is my listbox
lstCompany.ColumnCount = j
lstCompany.Clear
' Get the number of records
' Define an array to be loaded with the data
Dim MyArray() As Variant
' Load data into MyArray
ReDim MyArray(i, j)
Set rs = db.OpenRecordset(strDBQuery, dbOpenForwardOnly)
m = 0
Do While Not rs.EOF
MyString = rs.Fields(2)
For n = 2 To j
If rs.Fields(1) <> "" Then
MyString = rs.Fields(2) & ", " & rs.Fields(1) 'Company
name, Name
Else
MyString = rs.Fields(2) 'Company name only
End If
Next n
MyArray(m, 0) = MyString
m = m + 1
rs.MoveNext
Loop
rs.Close
For n = 1 To j - 3
Set rs = db.OpenRecordset(strDBQuery, dbOpenForwardOnly)
m = 0
Do While Not rs.EOF
MyArray(m, n) = rs.Fields(n + 1)
m = m + 1
rs.MoveNext
Loop
Next n
' Load data into ListBox1
lstCompany.List() = MyArray
' Set widths of individual columns in the listbox
lstCompany.ColumnWidths = ";" & 0 & ";" & 0 & ";" & 0 & ";" _
& 0 & ";" & 0 & ";" & 0 & ";" & 0 & ";" & 0 & ";" & 0 & ";" & 0 &
";" & 0
lstCompany.ColumnCount = 1
lstCompany.ColumnWidths = -1 'Resets default
rs.Close
db.Close
End Sub
Private Sub lstCompany_Click()
If Me.optDB Then 'database is being viewed in lstCompany
With rs '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(lstCompany.Column(0)) 'Name
txtToAddress3.Text =
AssignBlankIfNull(lstCompany.Column(1)) 'Company
txtToAddress4.Text =
AssignBlankIfNull(lstCompany.Column(2)) 'Address1
txtToAddress5.Text =
AssignBlankIfNull(lstCompany.Column(3)) 'Address2
txtToAddress6.Text =
AssignBlankIfNull(lstCompany.Column(4)) 'Address3
txtToAddress7.Text =
AssignBlankIfNull(lstCompany.Column(5)) 'City
txtToAddress8.Text =
AssignBlankIfNull(lstCompany.Column(7)) 'Post Code
' txtToAddress9.Text =
AssignBlankIfNull(lstCompany.Column(9))
txtToAddress10.Text =
AssignBlankIfNull(lstCompany.Column(11)) 'Fax
txtToAddress11.Text =
AssignBlankIfNull(lstCompany.Column(10)) 'Tel
txtToAddress12.Text =
AssignBlankIfNull(lstCompany.Column(6)) 'County
txtToAddress13.Text =
AssignBlankIfNull(lstCompany.Column(8)) 'Country
End With
End If
End Sub