i have the following error when trying to run my code:
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file "c:\export.txt" because it is being used by another process.
The file is created but is empty. I want to export it so that i can use it for a mailmerge template. Here is my code:
Private Sub mnuMMRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMMRun.Click
' setup the text file for export
Dim path As String = "c:\export.txt"
'if it exists, delete it
If File.Exists(path) Then
File.Delete(path)
End If
'create a new text file
Dim fs As FileStream = File.Create(path)
Dim sw As New StreamWriter(path, True, Encoding.ASCII)
Dim insertme As String
'put the headers into the text file
insertme = "ADDRESS_AREA#ADDRESS_LOCALITY#ADDRESS_POSTCODE#"
insertme = +"ADDRESS_PROPERTY#ADDRESS_STREET#ADDRESS_SUB_LOCALITY#"
insertme = +"ADDRESS_SUB_PROPERTY#ADDRESS_SUB_STREET#ADDRESS_TOWN#"
insertme = +"DETERMINATION_DATE#APPLICATION_NUMBER#ADDRESSEE#DATE_RECEIVED#"
insertme = +"MAIN_CONTACT#OFFICER#OFFICER_TELEPHONE#SITE_ADDRESS#PROPOSAL#"
insertme = +"WORK_STARTED_DATE#VARIABLE_DETERMINATION~"
sw.Write(insertme)
'open connection/recordset
Dim clsConnect As New clsMain
clsConnect.OpenConnect("SQL", "Postbook")
'set up the SQL query
Dim SQL As String
SQL = "Select Distinct * from Postbook"
'open the connection
rs.Open(SQL, cnConnection, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
'loop until all records have been read
While rs.EOF = False
' insert data from the DB into the text file
' separate with #, end record with ~
insertme = "" & rs.Fields("Address_4").Value & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("Address_1").Value) & "#"
insertme = +Trim("" & rs.Fields("Address_2").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("Address_3").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("File Ref").Value) & "#"
insertme = +Trim("" & rs.Fields("Name").Value) & "#"
insertme = +Trim("" & rs.Fields("Rec_Date").Value) & "#"
insertme = +Trim("" & rs.Fields("Contact").Value) & "#"
insertme = +Trim("" & rs.Fields("Officer").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
End While
'close the file stream
sw.Close() : fs.Close()
'close the recordset
rs.Close()
Dim w1 As New Word.Application
w1 = New Word.Application
w1.Visible = True
w1.Documents.Open("C:\letter.doc")
End Sub
cd sum1 pls tell me where im going wrong and how to rectify this problem..thank you
An unhandled exception of type 'System.IO.IOException' occurred in mscorlib.dll
Additional information: The process cannot access the file "c:\export.txt" because it is being used by another process.
The file is created but is empty. I want to export it so that i can use it for a mailmerge template. Here is my code:
Private Sub mnuMMRun_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuMMRun.Click
' setup the text file for export
Dim path As String = "c:\export.txt"
'if it exists, delete it
If File.Exists(path) Then
File.Delete(path)
End If
'create a new text file
Dim fs As FileStream = File.Create(path)
Dim sw As New StreamWriter(path, True, Encoding.ASCII)
Dim insertme As String
'put the headers into the text file
insertme = "ADDRESS_AREA#ADDRESS_LOCALITY#ADDRESS_POSTCODE#"
insertme = +"ADDRESS_PROPERTY#ADDRESS_STREET#ADDRESS_SUB_LOCALITY#"
insertme = +"ADDRESS_SUB_PROPERTY#ADDRESS_SUB_STREET#ADDRESS_TOWN#"
insertme = +"DETERMINATION_DATE#APPLICATION_NUMBER#ADDRESSEE#DATE_RECEIVED#"
insertme = +"MAIN_CONTACT#OFFICER#OFFICER_TELEPHONE#SITE_ADDRESS#PROPOSAL#"
insertme = +"WORK_STARTED_DATE#VARIABLE_DETERMINATION~"
sw.Write(insertme)
'open connection/recordset
Dim clsConnect As New clsMain
clsConnect.OpenConnect("SQL", "Postbook")
'set up the SQL query
Dim SQL As String
SQL = "Select Distinct * from Postbook"
'open the connection
rs.Open(SQL, cnConnection, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)
'loop until all records have been read
While rs.EOF = False
' insert data from the DB into the text file
' separate with #, end record with ~
insertme = "" & rs.Fields("Address_4").Value & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("Address_1").Value) & "#"
insertme = +Trim("" & rs.Fields("Address_2").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("Address_3").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("File Ref").Value) & "#"
insertme = +Trim("" & rs.Fields("Name").Value) & "#"
insertme = +Trim("" & rs.Fields("Rec_Date").Value) & "#"
insertme = +Trim("" & rs.Fields("Contact").Value) & "#"
insertme = +Trim("" & rs.Fields("Officer").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
insertme = +Trim("" & rs.Fields("#").Value) & "#"
End While
'close the file stream
sw.Close() : fs.Close()
'close the recordset
rs.Close()
Dim w1 As New Word.Application
w1 = New Word.Application
w1.Visible = True
w1.Documents.Open("C:\letter.doc")
End Sub
cd sum1 pls tell me where im going wrong and how to rectify this problem..thank you