I modified your code a little - mostly re-arranging lines.
I tried both a table and a query. Both wrote to the text file.... no errors.
Here is the code:
Code:
Function Wrap_Detail(pCallingRoutine As Integer)
Dim rs As Recordset
Dim strSQL As String
Dim WF As Integer
Dim strFileName As String
WF = FreeFile 'get the next free handle
strFileName = "CityDetail.txt"
Close #WF '<<--- don't really need this line
Open "\\Server\shared$\Information Technology\NAS ITDept\" & strFileName For Output As #WF
' Open "f:\forum\" & strFileName For Output As #WF
If pCallingRoutine = 1 Then
strSQL = "Select * From [Community_Detail1]"
' strSQL = "Select * From [group]"
ElseIf pCallingRoutine = 2 Then
strSQL = "Select * From [Community_Detail2]"
' strSQL = "Select * From [qrylocation]"
End If
Set rs = CurrentDb.OpenRecordset(strSQL, dbOpenSnapshot)
With rs
If .RecordCount = 0 Then
Print #WF, "Record Count = " & rs.RecordCount
Else
rs.MoveLast
Print #WF, "Record Count = " & rs.RecordCount
rs.MoveFirst
End If
End With
With rs
Do While Not rs.EOF
Print #WF, rs.Fields(0)
rs.MoveNext '<<<---- necessary in recordset loops
Loop
End With
Close #WF 'Close text file.
rs.Close 'close recordset
Set rs = Nothing
End Function
This is how I call the function from a button click event:
Code:
Private Sub test12_Click()
Dim x
x = Wrap_Detail(1)
' x = Wrap_Detail(2)
End Sub
I left my testing lines in - they can be deleted.
BTW, the following two lines should be at the top of every module:
Code:
Option Compare Database
Option Explicit