Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / September 2006

Tip: Looking for answers? Try searching our database.

Help with DAO and word

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jim - 24 Sep 2006 03:19 GMT
Hello,
I have a userform which fills list/combo boxes from access databases. I
have gotten this far with questions to this group and looking at the links
like mvps.org.  I have a few questions / more things that I would like to
do but can't find the answers.  If you could help ....  I use Word 2003,
Access 2003, Win XP pro sp2

Here is a sample of the line that calls the sub to fill a box -

FillList Me.txt_AddressFile.Value, "Select * FROM doctoraddresses WHERE
Frequent = true and Inactive = False", Me.cb_ReferringAttending 'uses the
address database to fill the doctors addresses   (it is on one line in my
form)

here is the code that I call to fill each box, I tried with just a
targetcontrol.list = rstdata but it didn't work right, this work for me.

Private Sub FillList(aDataBaseFile As String, SelectString As String,
TargetControl As Control)
  Dim wrkJet As Workspace
  Dim dbsDataBase As DAO.Database
  Dim rstData As DAO.Recordset
  Dim numfields, numrecords As Long
  Dim aDataBasePath As String
  Dim DataArray() As Variant
  Dim TargetText As String
  On Error GoTo errmsg
  TargetText = TargetControl.Text
  Set wrkJet = CreateWorkspace("", "admin", "", dbUseJet)
  Set dbsDataBase = wrkJet.OpenDatabase(aDataBaseFile, True)
   Set rstData = dbsDataBase.OpenRecordset(SelectString, dbOpenDynaset)
   numfields = rstData.Fields.Count
   If TargetControl.ColumnCount < numfields Then  'make sure the box has
enough columns
       TargetControl.ColumnCount = numfields
   End If
  With rstData                   'fill rstdata with the database values
   Do While Not .EOF             'count the number of records
       numrecords = .RecordCount
       .MoveNext
   Loop
   rstData.MoveFirst
  ReDim DataArray(1 To numrecords, 0 To numfields - 1)
  For i = 1 To numrecords
   For j = 0 To numfields - 1
   DataArray(i, j) = rstData.Fields(j)
   Next j
   rstData.MoveNext
   Next i
  End With
  TargetControl.List() = DataArray
  'TargetControl.ListIndex = 1
  'TargetControl.Text = TargetText
  rstData.Close
  dbsDataBase.Close
Exit Sub
errmsg:
   MsgBox " the " & TargetControl.Name & " listbox could not be filled
with " & aDataBaseFile
End Sub

here are my questions:

1. Any way to get just a few of the data fields instead of the whole row? I
could not get it right

2. Any way to "ORDER BY" to sort the data like you can in access/SQL

3. I can get info to a table with a new row, but can I change data in an
existing row ?  Each row has a unique ID field so I can identify it if
needed.

Thanks, Jim
Doug Robbins - Word MVP - 24 Sep 2006 07:37 GMT
Create a select query in the database to contain just the data that you want
in the order that you want.  Here is the code that I would use to populate a
listbox from a table/query in Access:

Private Sub UserForm_Initialize()
   Dim db As DAO.Database
   Dim rs As DAO.Recordset
   Dim NoOfRecords As Long
   ' Open the database
   Set db = OpenDatabase("D:\Access\ResidencesXP.mdb")
   ' Retrieve the recordset
   Set rs = db.OpenRecordset("SELECT * FROM Owners")
   ' Determine the number of retrieved records
   With rs
        .MoveLast
        NoOfRecords = .RecordCount
        .MoveFirst
   End With
   ' Set the number of Columns = number of Fields in recordset
   ListBox1.ColumnCount = rs.Fields.Count
   ' Load the ListBox with the retrieved records
   ListBox1.Column = rs.GetRows(NoOfRecords)
   ' Cleanup
   rs.Close
   db.Close
   Set rs = Nothing
   Set db = Nothing
End Sub

Signature

Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

> Hello,
> I have a userform which fills list/combo boxes from access databases. I
[quoted text clipped - 70 lines]
>
> Thanks, Jim
Jim - 25 Sep 2006 17:28 GMT
Doug,

Thank you very much, I'll try that when I get back to my other computer.  
One last thing, any way to put data back into an existing row in the
table when something changes. ? Do I have to copy the whole list box back
into the table especially if I got the data from just a query instead of
the whole row or can I just change the selected fields which match the
unique ID.  I guess I could use a query that just gives that 1 row with
the ID and change that, but any way to do just 1 field alone??

Again, my thanks.

Jim

> Create a select query in the database to contain just the data that
> you want in the order that you want.  Here is the code that I would
[quoted text clipped - 24 lines]
>     Set db = Nothing
> End Sub
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.