You're very close. You only want o open and close the output file when you have found the next PatNo.
I also updated the code to use FreeFile to get the next available file handler number. It is better than assuming #1 is available.
Code:
Public Function TextOutput()
Dim rs As DAO.Recordset
Dim sPath As String
Dim FH as Integer
Dim sLastPatNo As String
sPath = "C:\temp\" ' just have the directory here
Set rs = CurrentDb.OpenRecordset("HL7 Outbound Final")
sLastPatNo = ""
Do Until rs.EOF
' test to see if a new patient or the first one
If sLastPatNo = "" then
' this is the first time so open the file
FH = Freefile ' get available file handle
Open sPath & rs("Patno") & ".hp" For Output As #FH
Else
If sLastPatNo <> rs("Patno") then
' next PatNo found
' close file currently opened and open a New file
Close #FH
' open output file for next PatNo
FH = Freefile ' get available file handle
Open sPath & rs("Patno") & ".hp" For Output As #FH
End IF
sLastPatNo = rs("Patno") ' Save the PatNo
PatNo = rs("Patno")
hl7out = rs("HL7")
Print #FH, PatNo, hl7out
' store the current PatNo
sLastPatNo = rs("Patno")
rs.MoveNext
Loop
Close #FH
rs.Close
Set rs = Nothing
End Function