Results 1 to 6 of 6

Thread: Word, VBA and a Database

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    27

    Word, VBA and a Database

    Hi Guys,

    I need to be able to use VBA to connect to a Microsoft Access Database, use SQL to query my database and insert values at bookmarks all within a Microsoft Word Document.

    I am sure it can be done but need some information on the references and code etc... I know how to do it in ASP but can't figure this VBA out. I know how to place values into the word document etc using VBA, just not the databse connection etc...

    Thanks
    Shane

  2. #2
    Hyperactive Member nagasrikanth's Avatar
    Join Date
    Nov 2004
    Location
    India,Hyderabad.
    Posts
    420

    Lightbulb Re: Word, VBA and a Database

    Hi,

    This is the way normally i use to connect to my database.
    Hope it helps u..

    VB Code:
    1. Dim w As Workspace
    2. Dim w As Workspace
    3. Dim db As Database
    4. Dim rst As Recordset
    5.  
    6. Set w = DBEngine.Workspaces(0)
    7. Set db = w.OpenDatabase("c:\mis.mde")
    8. Set rst = db.OpenRecordset("tbl_months")
    9.     If Not rst.RecordCount = 0 Then
    10.         MsgBox rst.RecordCount
    11.     End If

    *****You need to refer Microsoft DAO for this in u r VBA..
    All the best

    Thanx & Regards
    Anu.
    The Difference between a Successful person and others is not a Lack of Knowledge,
    But rather a Lack of WILL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    27

    Re: Word, VBA and a Database

    Ok... once I have that... how would I use SQL to query the table, and return a particular record set etc so I can then put particular fields of my record into particular places on my document.

  4. #4
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Word, VBA and a Database

    The bookmarks in your document I don't know, but instead of opening a table you could open your query in the recordset:

    Set rst = db.OpenRecordset("SELECT fldFoo FROM tblBar WHERE fldFoo = 'Holy Cow'")
    Tengo mas preguntas que contestas

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Oct 2005
    Posts
    27

    Re: Word, VBA and a Database

    How can you specify the databse password when you connect to your database? IE: my access database has a password set on it.

  6. #6
    Hyperactive Member nagasrikanth's Avatar
    Join Date
    Nov 2004
    Location
    India,Hyderabad.
    Posts
    420

    Lightbulb Re: Word, VBA and a Database

    Chk it out ....this will helps u...

    VB Code:
    1. Set db = w.OpenDatabase("c:\db11.mdb", False, False, "MS Access;PWD=123")

    change u r password over here and place the same in ur code..
    Im sure ...It will works...
    All the best..


    Thanx & Regards
    Anu..
    (Please rate the post if it works 4 u...)
    The Difference between a Successful person and others is not a Lack of Knowledge,
    But rather a Lack of WILL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width