reading from a database

C

Charlie Mac

VBA Gerus,

I have a macro (Word 2003) that presents a commandbar with > 10
buttons.... I need to load variables from a small database when Word
is started. The database was created using Access 2003. I am
guessing that this is easy for those that know.

Thanks.

Charlie from Texas
 
D

Doug Robbins - Word MVP

And for those that understand what you are trying to do. Where do you want
the data from the database to go?

--
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
 
G

Gunnar Nygaard

VBA Gerus,

I have a macro (Word 2003) that presents a commandbar with > 10
buttons.... I need to load variables from a small database when Word
is started. The database was created using Access 2003. I am
guessing that this is easy for those that know.

Thanks.

Charlie from Texas

Hi Charlie from Texas

For a couple of reasons, I still use Word 2000. I made a simple .dot a few
months ago. Behind the input form for addressee, addresser and general
letter info, I've put the code below. PostNr is sort of a zip-code (4
digits), and I retrieve the (city-)name uniquely assosiated to that number.
What you retrieve from your database is all dependent on your SQL.


HTH

Rgds
Gunnar from Norway


'tbPostNr is a textbox control on a Word form
If Len(tbPostNr.Text) = 4 Then
' Open connection to Access to retrive Poststed based on PostNr
' ***********************************************************
'
' IF RUN ON OTHER THAN WORD 2000, 2002 or 2003,
' THE REFERENCE TO DAO 3.6 MAY FAIL
' CHECK TOOLS -> REFERENCES -> "Microsoft DOA 3.6 Object Library"
'
' ***********************************************************
Dim dbs As DAO.Database, rst As DAO.Recordset

'Database file location is stored in a CustomDocumentProperties to let
users
'manipulate the value without having to see my code.
strDatabasefil =
ActiveDocument.CustomDocumentProperties("NCDatabasefil")
Set dbs = OpenDatabase(Name:=strDatabasefil)

'I could of course do it this way:
'Set dbs = OpenDatabase(Name:="\\CompanyServer\OurDBs\OurRegister.mdb")

'tbPostNr is a textbox control on a Word form
strSQL = "SELECT Poststed FROM tblSted WHERE Postnr=" & Chr(34) & _
tbPostNr.Text & Chr(34) & ";"
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
' dbOpenSnapshot is a faster read-only sort of data access. Check Access
refs.

With rst
If Not .BOF And Not .EOF Then
'Do stuff only if recordset returns data
ActiveDocument.Bookmarks("PostSted").Range.Text = !Poststed
End If
End With
rst.Close
Set dbs = Nothing
End If
 
C

Charlie Mac

Doug,

Thank you for your response. I need to load several arrays and a few
variables located in the toolbar's code...to do some text searching.
I created a VB.Net app to manage the database's content which changes
frequently. A button on the toolbar would need to read values from
the database, load arrays, and a few variables as an initialization
routine for the toolbar's keyword searching code. I just don't know
how to open the database and read values from a toolbar's button. The
only means I have found requires a form. This is probably easy, but
when you don't know...its impossible. Thanks.

Charlie From Texas
 
C

Charlie Mac

Gunnar from Norway,

Thank you for your response. I can read a database from a form, but I
am trying to do this from a toolbar's button and it appears that my
code is "not tolerated" unless it is placed on a form, which I am
trying to avoid.... Thanks again.

Take care,

Charlie from Texas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top