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