Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / Forms Programming / April 2005

Tip: Looking for answers? Try searching our database.

Import data from Word using a form

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
DB Queen (Not) - 11 Apr 2005 10:05 GMT
I have an existing database and have recently created a form for staff to add
data to it easier than before.  I'm having trouble finding code to import
this data as most are for new forms going to new databases. I've found the
code below and don't know if it's a case of just amending it or whether I
need a totally different set of rules? Please help!

Sub GetWordData()
Dim appWord As Word.Application
Dim doc As Word.Document
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim strDocName As String
Dim blnQuitWord As Boolean

On Error GoTo ErrorHandling

strDocName = "C:\Contracts\" & _
    InputBox("Enter the name of the Word contract " & _
    "you want to import:", "Import Contract")

Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)

cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    "Data Source=C:\My Documents\" & _
    "Healthcare Contracts.mdb;"
rst.Open "tblContracts", cnn, _
    adOpenKeyset, adLockOptimistic

With rst
    .AddNew
    !FirstName = doc.FormFields("fldFirstName").Result
    !LastName = doc.FormFields("fldLastName").Result
    !Company = doc.FormFields("fldCompany").Result
    !Address = doc.FormFields("fldAddress").Result
    !City = doc.FormFields("fldCity").Result
    !State = doc.FormFields("fldState").Result
    !ZIP = doc.FormFields("fldZIP1").Result & _
        "-" & doc.FormFields("fldZIP2").Result
    !Phone = doc.FormFields("fldPhone").Result
    !SocialSecurity = doc.FormFields("fldSocialSecurity").Result
    !Gender = doc.FormFields("fldGender").Result
    !BirthDate = doc.FormFields("fldBirthDate").Result
    !AdditionalCoverage = _
        doc.FormFields("fldAdditional").Result
    .Update
    .Close
End With
doc.Close
If blnQuitWord Then appWord.Quit
cnn.Close
MsgBox "Contract Imported!"

Cleanup:
Set rst = Nothing
Set cnn = Nothing
Set doc = Nothing
Set appWord = Nothing
Exit Sub
ErrorHandling:
Select Case Err
Case -2147022986, 429
    Set appWord = CreateObject("Word.Application")
    blnQuitWord = True
    Resume Next
Case 5121, 5174
    MsgBox "You must select a valid Word document. " _
        & "No data imported.", vbOKOnly, _
        "Document Not Found"
Case 5941
    MsgBox "The document you selected does not " _
        & "contain the required form fields. " _
        & "No data imported.", vbOKOnly, _
        "Fields Not Found"
Case Else
    MsgBox Err & ": " & Err.Description
End Select
GoTo Cleanup
End Sub
John Webb - 11 Apr 2005 11:15 GMT
Hi,

Yes, I think that should work fine, as long as you have set your word form
up to use form fields properly - which I assume you have.

One other thing thou, I would recommend late binding the word object,
rather than early binding it; simply because early binding it can possibly
cause reference errors.

Cheers

John Webb
 
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.