zraj07
Board Regular
- Joined
- Jun 15, 2006
- Messages
- 80
This works: While I am in this Module, the code will update the two recordsets "Location Info" and "Other Info". I am using Access 2003-2007 and Word 97-2003.
My form fields import successfully to my database.
Option Compare Database
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:\Test\" & _
InputBox("Type in and enter the name of your document " & _
"you want to import:", "Import test document")
Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Test\" & "Test.mdb;"
rst.Open "Location_Info", cnn, adOpenKeyset, adLockOptimistic
With rst
.AddNew
!fldCompany = doc.FormFields("fldCompany").Result
!fldName = doc.FormFields("fldName").Result
!fldNumber = doc.FormFields("fldNumber").Result
.Update
.Close
rst.Open "Other_Info", cnn, adOpenKeyset, adLockOptimistic
With rst
.AddNew
!chkvalidationY = doc.FormFields("chkvalidationY").Result
!chkvalidationN = doc.FormFields("chkvalidationN").Result
!fldValDesc = doc.FormFields("fldValDesc").Result
End With
End With
doc.Close
If blnQuitWord Then appWord.Quit
cnn.Close
MsgBox "Test DocImported!"
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
****************************************************************************************************
However, if I try to do this as you see below, and call "Other_Info" from a separate module, I get the 3709 error or an Object required error.
I really want to call "Other_Info" and continue to update the database that was presumably still open, but it seems
Access forgets about that open connection once I step out and enter "Module1.Other_info
Option Compare Database
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:\Test\" & _
InputBox("Type in and enter the name of your document " & _
"you want to import:", "Import test document")
Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Test\" & "Test.mdb;"
rst.Open "Location_Info", cnn, adOpenKeyset, adLockOptimistic
With rst
.AddNew
!fldCompany = doc.FormFields("fldCompany").Result
!fldName = doc.FormFields("fldName").Result
!fldNumber = doc.FormFields("fldNumber").Result
.Update
.Close
Module1.Other_Info
End With
doc.Close
If blnQuitWord Then appWord.Quit
cnn.Close
MsgBox "Test Info 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
************************************************************************************************************************
Here is what "Module1.Other_Info looks like.
I have tried commenting out things that i don't think should be required given the module this was called from already has a database connection established.
"other_info"
Sub OtherInfo()
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:\Test\" & _
' InputBox("Type in and enter the name of your Test document " & _
' "you want to import:", "Import Test Doc")
'Set appWord = GetObject(, "Word.Application")
'Set doc = appWord.Documents.Open(strDocName)
'cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
' "Data Source=C:\Test\" & "Test.mdb;"
rst.Open "Other_Info", cnn, adOpenKeyset, adLockOptimistic
With rst
.AddNew
!chkvalidationY = doc.FormFields("chkvalidationY").Result
!chkvalidationN = doc.FormFields("chkvalidationN").Result
!fldValDesc = doc.FormFields("fldValDesc").Result
.Update
.Close
End With
'doc.Close
'If blnQuitWord Then appWord.Quit
'cnn.Close
'MsgBox "IMF0405 Location_Pipe_Info 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
My form fields import successfully to my database.
Option Compare Database
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:\Test\" & _
InputBox("Type in and enter the name of your document " & _
"you want to import:", "Import test document")
Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Test\" & "Test.mdb;"
rst.Open "Location_Info", cnn, adOpenKeyset, adLockOptimistic
With rst
.AddNew
!fldCompany = doc.FormFields("fldCompany").Result
!fldName = doc.FormFields("fldName").Result
!fldNumber = doc.FormFields("fldNumber").Result
.Update
.Close
rst.Open "Other_Info", cnn, adOpenKeyset, adLockOptimistic
With rst
.AddNew
!chkvalidationY = doc.FormFields("chkvalidationY").Result
!chkvalidationN = doc.FormFields("chkvalidationN").Result
!fldValDesc = doc.FormFields("fldValDesc").Result
End With
End With
doc.Close
If blnQuitWord Then appWord.Quit
cnn.Close
MsgBox "Test DocImported!"
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
****************************************************************************************************
However, if I try to do this as you see below, and call "Other_Info" from a separate module, I get the 3709 error or an Object required error.
I really want to call "Other_Info" and continue to update the database that was presumably still open, but it seems
Access forgets about that open connection once I step out and enter "Module1.Other_info
Option Compare Database
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:\Test\" & _
InputBox("Type in and enter the name of your document " & _
"you want to import:", "Import test document")
Set appWord = GetObject(, "Word.Application")
Set doc = appWord.Documents.Open(strDocName)
cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=C:\Test\" & "Test.mdb;"
rst.Open "Location_Info", cnn, adOpenKeyset, adLockOptimistic
With rst
.AddNew
!fldCompany = doc.FormFields("fldCompany").Result
!fldName = doc.FormFields("fldName").Result
!fldNumber = doc.FormFields("fldNumber").Result
.Update
.Close
Module1.Other_Info
End With
doc.Close
If blnQuitWord Then appWord.Quit
cnn.Close
MsgBox "Test Info 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
************************************************************************************************************************
Here is what "Module1.Other_Info looks like.
I have tried commenting out things that i don't think should be required given the module this was called from already has a database connection established.
"other_info"
Sub OtherInfo()
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:\Test\" & _
' InputBox("Type in and enter the name of your Test document " & _
' "you want to import:", "Import Test Doc")
'Set appWord = GetObject(, "Word.Application")
'Set doc = appWord.Documents.Open(strDocName)
'cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;" & _
' "Data Source=C:\Test\" & "Test.mdb;"
rst.Open "Other_Info", cnn, adOpenKeyset, adLockOptimistic
With rst
.AddNew
!chkvalidationY = doc.FormFields("chkvalidationY").Result
!chkvalidationN = doc.FormFields("chkvalidationN").Result
!fldValDesc = doc.FormFields("fldValDesc").Result
.Update
.Close
End With
'doc.Close
'If blnQuitWord Then appWord.Quit
'cnn.Close
'MsgBox "IMF0405 Location_Pipe_Info 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