ppkgoldengun
MIS
I am trying to complete a Do Loop sequence that includes a recordset findnext command. Unfortunately, the Findnext always stays at the last found record (it successfully finds the previous records) instead of return a EOF is true which would end the loop. Any hints would be greatly appreciated. My code:
Private Sub MergeButton_Click()
Dim Letterloc As String
Dim strInsured As String
strInsured = Me.txtInsuredName
Dim srInsuredName As String
Dim strInsuredAddress As String
Dim strInsuredCity As String
Dim strInsuredCode As String
Dim strInsuredZip As String
Dim strInsuredContact As String
Dim strPurchaseOrder As String
Dim strCertDesc As String
Dim strInsType As String
Dim strExpDate As String
Dim db As DAO.Database
Dim rstInsured As DAO.Recordset
Set db = CurrentDb
Set rstInsured = db.OpenRecordset("qryCertDetailRedAll", dbOpenSnapshot)
rstInsured.FindFirst "[InsuredName]='" & strInsured & "'"
'Set rstInsured = db.OpenRecordset("tblEmployees", dbOpenSnapshot)
'Set rstInsured = db.OpenRecordset("qryContract", dbOpenSnapshot)
Debug.Print strInsured
Debug.Print rstInsured![InsuredName]
Debug.Print rstInsured![ExpDate]
'Debug.Print rstInsured![Certificate Description]
'Debug.Print rstInsured![InsType]
Letterloc = """" & WordLoc & """"
Set objWord = CreateObject("Word.Application")
With objWord
' Make the application visible.
.Visible = True
' Open the document.
'.Documents.Open ("c:\Access\SJSU\CertNear.dot")
.Documents.Open (Letterloc)
' Move to each bookmark and insert text from the form.
'Make sure that list follows bookmark order in Document.
'Set all the values to Zeros or empty strings if a null value is found so Word doesnt choke
strInsuredName = Nz(rstInsured![InsuredName])
strInsuredAddress = Nz(rstInsured![InsuredAddress])
strInsuredCity = Nz(rstInsured![InsuredCity])
strInsuredCode = Nz(rstInsured![InsuredCode])
strInsuredZip = Nz(rstInsured![InsuredZip])
strInsuredContact = Nz(rstInsured![InsuredContact])
.ActiveDocument.Bookmarks("InsuredName").Select
.Selection.Text = (CStr(strInsuredName))
.ActiveDocument.Bookmarks("InsuredAddress").Select
.Selection.Text = (CStr(strInsuredAddress))
.ActiveDocument.Bookmarks("InsuredCity").Select
.Selection.Text = (CStr(strInsuredCity))
.ActiveDocument.Bookmarks("InsuredCode").Select
.Selection.Text = (CStr(strInsuredCode))
.ActiveDocument.Bookmarks("InsuredZip").Select
.Selection.Text = (CStr(strInsuredZip))
.ActiveDocument.Bookmarks("InsuredContact").Select
.Selection.Text = (CStr(strInsuredContact))
' Go To First Table In Document
objWord.Selection.WholeStory
objWord.Selection.HomeKey Unit:=6
With objWord.Selection
.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
.MoveDown Unit:=wdLine, Count:=2
End With
Do While Not rstInsured.EOF
strPurchaseOrder = Nz(rstInsured![PurchaseOrder])
strCertDesc = Nz(rstInsured![Certificate Description])
strInsType = Nz(rstInsured![InsType])
strExpDate = Nz(rstInsured![ExpDate])
With objWord.Selection
.TypeText Text:=(CStr(strPurchaseOrder))
.MoveRight Unit:=wdCell
.TypeText Text:=(CStr(strCertDesc))
.MoveRight Unit:=wdCell
.TypeText Text:=(CStr(strInsType))
.MoveRight Unit:=wdCell
.TypeText Text:=(CStr(strExpDate))
.MoveRight Unit:=wdCell
End With
rstInsured.FindNext "[InsuredName]='" & strInsured & "'"
Loop
rstInsured.Close
Selection.SelectRow
Selection.Rows.Delete
End With
' Print the document in the foreground so Microsoft Word 97
' will not close until the document finishes printing.
objWord.ActiveDocument.PrintOut Background:=False
' Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Quit Microsoft Word 97 and release the object variable.
objWord.Quit
Set objWord = Nothing
Exit Sub
MergeButton_Err:
' If a field on the form is empty
' remove the bookmark text and continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
' If the Photo field is empty.
ElseIf Err.Number = 2046 Then
MsgBox "Please add a photo to this record and try again."
Else
MsgBox Err.Number & vbCr & Err.Description
End If
Exit Sub
' End Sub
End Sub
Private Sub MergeButton_Click()
Dim Letterloc As String
Dim strInsured As String
strInsured = Me.txtInsuredName
Dim srInsuredName As String
Dim strInsuredAddress As String
Dim strInsuredCity As String
Dim strInsuredCode As String
Dim strInsuredZip As String
Dim strInsuredContact As String
Dim strPurchaseOrder As String
Dim strCertDesc As String
Dim strInsType As String
Dim strExpDate As String
Dim db As DAO.Database
Dim rstInsured As DAO.Recordset
Set db = CurrentDb
Set rstInsured = db.OpenRecordset("qryCertDetailRedAll", dbOpenSnapshot)
rstInsured.FindFirst "[InsuredName]='" & strInsured & "'"
'Set rstInsured = db.OpenRecordset("tblEmployees", dbOpenSnapshot)
'Set rstInsured = db.OpenRecordset("qryContract", dbOpenSnapshot)
Debug.Print strInsured
Debug.Print rstInsured![InsuredName]
Debug.Print rstInsured![ExpDate]
'Debug.Print rstInsured![Certificate Description]
'Debug.Print rstInsured![InsType]
Letterloc = """" & WordLoc & """"
Set objWord = CreateObject("Word.Application")
With objWord
' Make the application visible.
.Visible = True
' Open the document.
'.Documents.Open ("c:\Access\SJSU\CertNear.dot")
.Documents.Open (Letterloc)
' Move to each bookmark and insert text from the form.
'Make sure that list follows bookmark order in Document.
'Set all the values to Zeros or empty strings if a null value is found so Word doesnt choke
strInsuredName = Nz(rstInsured![InsuredName])
strInsuredAddress = Nz(rstInsured![InsuredAddress])
strInsuredCity = Nz(rstInsured![InsuredCity])
strInsuredCode = Nz(rstInsured![InsuredCode])
strInsuredZip = Nz(rstInsured![InsuredZip])
strInsuredContact = Nz(rstInsured![InsuredContact])
.ActiveDocument.Bookmarks("InsuredName").Select
.Selection.Text = (CStr(strInsuredName))
.ActiveDocument.Bookmarks("InsuredAddress").Select
.Selection.Text = (CStr(strInsuredAddress))
.ActiveDocument.Bookmarks("InsuredCity").Select
.Selection.Text = (CStr(strInsuredCity))
.ActiveDocument.Bookmarks("InsuredCode").Select
.Selection.Text = (CStr(strInsuredCode))
.ActiveDocument.Bookmarks("InsuredZip").Select
.Selection.Text = (CStr(strInsuredZip))
.ActiveDocument.Bookmarks("InsuredContact").Select
.Selection.Text = (CStr(strInsuredContact))
' Go To First Table In Document
objWord.Selection.WholeStory
objWord.Selection.HomeKey Unit:=6
With objWord.Selection
.GoTo What:=wdGoToTable, Which:=wdGoToFirst, Count:=1, Name:=""
.MoveDown Unit:=wdLine, Count:=2
End With
Do While Not rstInsured.EOF
strPurchaseOrder = Nz(rstInsured![PurchaseOrder])
strCertDesc = Nz(rstInsured![Certificate Description])
strInsType = Nz(rstInsured![InsType])
strExpDate = Nz(rstInsured![ExpDate])
With objWord.Selection
.TypeText Text:=(CStr(strPurchaseOrder))
.MoveRight Unit:=wdCell
.TypeText Text:=(CStr(strCertDesc))
.MoveRight Unit:=wdCell
.TypeText Text:=(CStr(strInsType))
.MoveRight Unit:=wdCell
.TypeText Text:=(CStr(strExpDate))
.MoveRight Unit:=wdCell
End With
rstInsured.FindNext "[InsuredName]='" & strInsured & "'"
Loop
rstInsured.Close
Selection.SelectRow
Selection.Rows.Delete
End With
' Print the document in the foreground so Microsoft Word 97
' will not close until the document finishes printing.
objWord.ActiveDocument.PrintOut Background:=False
' Close the document without saving changes.
objWord.ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
' Quit Microsoft Word 97 and release the object variable.
objWord.Quit
Set objWord = Nothing
Exit Sub
MergeButton_Err:
' If a field on the form is empty
' remove the bookmark text and continue.
If Err.Number = 94 Then
objWord.Selection.Text = ""
Resume Next
' If the Photo field is empty.
ElseIf Err.Number = 2046 Then
MsgBox "Please add a photo to this record and try again."
Else
MsgBox Err.Number & vbCr & Err.Description
End If
Exit Sub
' End Sub
End Sub