Reputation: 93
I have data in my listview1 like this:
And i want to save Quantity until Total cost Coloum from that listview to .txt but i dont know how to save it to .txt
i found the referencee drom another web, they used this code:
Private Sub testfile as String = application.startupPath & "\testfile.txt"
Dim mywriter as new.IO.streamwriter(testfile)
for eacy myItem as ListviewItem in Listview1.items
mywriter.writeLine(myitem.text & "#" & myItem.SubItem(1).text & "#" & myitem.subitem(2).text)
next
mywriter.close()
but VB6 can't compile this code. please help me
Upvotes: 0
Views: 1826
Reputation: 7697
ListItems
and ListSubItems
are Base 1, this means they are starting from index 1 and you will find it in the same order as you have added it to the ListView
.
For example: the Price for product "a" is in ListView1.ListItems(2).SubItems(4)
because the first column is the ListItem
Itself, which you can get as follows : ListView1.ListItems(2).Text
.
So, if your question is about printing to file, you may need some small, optional, helper functions, which you can always keep in your snippet library, for example in a module
, and (maybe) reuse.
Please, keep in mind there are really a lot of ways to get your task done, this is just an example.
Public Function NormalizePath(path As String) As String
' Normalize a Windows path with Backslash
Const DirSeparator = "\"
If Right(path, 1) = DirSeparator Then
NormalizePath = path
Else
NormalizePath = path & DirSeparator
End If
End Function
Public Function Formatted(TextValue, FormatType, ColumnWidth) As String
' Make fixed length fields
Dim Result As String, PlaceHolder As String
Dim CurrencyValue As Currency
PlaceHolder = Space(ColumnWidth)
Select Case FormatType
Case "Text"
Result = Left(TextValue & PlaceHolder, ColumnWidth)
Case "Integer"
Result = Right(PlaceHolder & TextValue, ColumnWidth)
Case "Currency"
CurrencyValue = CCur(TextValue)
Result = Right(PlaceHolder & Format(CurrencyValue, "0.00"), ColumnWidth)
End Select
Formatted = Result
End Function
Now to the point: double-click your CommandButton
labeled Print Receipt and write this code:
PrintReceipt
Copy and paste following function to your form:
Private Sub PrintReceipt()
On Error GoTo PrintReceipt_Error
Dim LineText As String, CellText As String
Dim TotalAmt As Currency, FormattedTotalAmt As String
Dim FileNum As Integer, FullFileName As String, i As Long, l As Long
FullFileName = NormalizePath(App.path) & "Receipt.txt"
FileNum = FreeFile
Open FullFileName For Output As #FileNum
Print #FileNum, "| " & Formatted("Qty.", "Text", 5) & " | " & Formatted("Product Name", "Text", 12) & " | " & Formatted("Unit Amt.", "Text", 9) & " | " & Formatted("Amount", "Text", 12) & " | "
Print #FileNum, String(2 * 2 + 3 * 3 + 5 + 12 + 9 + 12, "-")
l = ListView1.ListItems.Count
For i = 1 To l
LineText = "| "
CellText = ListView1.ListItems(i).SubItems(2) 'Quantity
LineText = LineText & Formatted(CellText, "Integer", 5) & " | "
CellText = ListView1.ListItems(i).SubItems(3) 'Product_Name
LineText = LineText & Formatted(CellText, "Text", 12) & " | "
CellText = ListView1.ListItems(i).SubItems(4) 'Price
LineText = LineText & Formatted(CellText, "Currency", 9) & " | "
CellText = ListView1.ListItems(i).SubItems(5) 'Total_Cost
LineText = LineText & Formatted(CellText, "Currency", 12) & " | "
TotalAmt = TotalAmt + CCur(CellText)
Print #FileNum, LineText
Next
FormattedTotalAmt = Format(TotalAmt, "0.00")
Print #FileNum, String(2 * 2 + 3 * 3 + 5 + 12 + 9 + 12, "-")
Print #FileNum, Formatted("", "Text", 2 + 3 + 5) & Formatted("Total Amount", "Text", 12) & String(27 - Len(FormattedTotalAmt), ".") & FormattedTotalAmt
Close #FileNum 'Close the file
Exit Sub
PrintReceipt_Error:
Close #FileNum 'Always close the file
End Sub
If you did it all right, you will find inside the path of your project a nicely formatted copy of your received data. Open the text file with Notepad and from menu Format->Font choose a fixed-width font, for example Courier New. You should be able to see something like this:
The coolest thing you can do now with this file, is to send it back to your device as Order Confirmation.
Final notes:
Happy learning!
Upvotes: 1