How to read and write to the same csv file using lotusscript? - csv

I have a CSV file. I need to read each line from this file, perform certain function and once done, I need to append a value something like "updated" at the end of each line. Can someone help me to achieve this using LotusScript ? I am using "open file for input" statement to open and read the file. I want to append the value after the function is performed at each line.
Open xlFilename$ For Input As Filenum%
Do Until Eof(Filenum%)
Line Input #Filenum%, txt$
'perform certain function
**'Would like to append csv here.**
loop
close Filenum

If you were appending new rows at the end, you would need to close the file, and then Open it again to append. I.e., after your loop...
Close Filenum
Open xlFilename$ For Append As Filenum2
Print #Filenum2, stuffYouWantToAppend
But I see you are trying to append new columns at the end of each row. It's going to be much easier if you simply treat your existing file as input and create a new one that you will Print to for output.
Open xlFilename$ For Input As Filenum1%
Open xlFilename$ + ".output.csv" For Output As Filenum2%
Do Until Eof(Filenum%)
Line Input #Filenum1%, txt$
'perform certain function
Print #Filenum2%, txt + "," + additionalColumnsGoHere
loop
close Filenum
And if you want, you could then Kill your input file and then rename your output file back to the original file's name using the Name statement.
Doing it in place is probably possible by opening the file in RANDOM READ WRITE mode, but it just isn't worth the trouble.

Related

Access: display .vsd from attachments

I have an Access table where each item has attached a Visio file (.vsd).
In my Access form, I would like to see the file. I don't care if it is an editable Visio file, a preview or just an image.
I have built a VBA code that let me load the Visio file from a Directory. But I need to load the file from a table.
Here my VBA code.
Private Sub Carica_Dati()
Dim path As String
path = "C:\Users\VisioFlow_001.vsd"
With Me.VisioObject ' name of the OLE Object where I want to put the Visio file
.Class = "Visio.Drawing.11"
.OLETypeAllowed = acOLELinked
.SourceDoc = path ' HERE I WANT TO LOAD THE FILE FROM A TABLE OF THE DB
.Enabled = True
.Locked = False
.Action = acOLECreateLink
.SizeMode = acOLESizeZoom
End With
End Sub
Here a preview of the form.
UPDATE
Here a picture to show how the file is attached to the table.
Since attachment fields in Access aren't very consistent, directly loading them into an OLE object is not an option, unless you're willing to do sophisticated things
Microsofts documentation on attachments can be found here
My observations on attachments: the binary data field contains one of the following:
Some characters I can't identify + the file type + the file data appended to it
Some characters I can't identify + the file type + a compressed version of the file data appended to it
Microsoft, in all it's wisdom, has supplied us with a way to save the original file to the disk, but hasn't supplied us with a way to remove those initial characters and the file type from the actual file data, or an easy way to identify if the file is compressed or not (you can check the file type with the table supplied in the link to check if it should be).
In conclusion, you're probably off best either replacing your attachment field with an OLE object in the database, or writing the attachment files to disk before displaying them.
If you use an OLE object field, and load them in as long binary data (not through the GUI), you can easily achieve the behaviour you seek without writing the file to disk, since the binary data is available without any extra characters.
To write an attachment file to disk:
Dim rsForm As DAO.Recordset2
Dim rsFiles As DAO.Recordset2
Set rsForm = Me.Recordset
Set rsFiles = rsForm.Fields("attachment_column").Value
If Not rsFiles.EOF Then
Dim fileLocation As String
fileLocation = Environ("TEMP") & rsFiles.Fields("FileName").Value
rsFiles.Fields("FileData").SaveToFile fileLocation
'Your existing code to display the OLE object here
End If
You do not want to use the Attachment feature. Its purpose is different than what you are attempting.
Put the images into their own stand alone folder outside of the database.
In the table that holds the records for your main form - you need a new field which holds the path & image file name. This is a text field. (If the path segment is uniform for all one can insert that elsewhere via code rather than store it in this field.)
Then in form design - use the image control. This control (all controls) have a source property - that will change with each record using that field that holds the path & file name.
Do a bing/google on the topic of changing an image with every record - the set up isn't intuitive necessarily. Note that older editions did things differently so be sure you get relatively recent advice.
Then when you are using the form and change records - the image will change.
Note after having typed all this.... I have no idea if the visio file type works - I know that jpg and bmp do... so first sanity check a simple fixed image with that file type to see if it works ...

what control should I use to print a long message to a windows in access VBA 2007

I am writting a test code in access 2007 (VBA) that would test the data and report if any record has problem.
It would do it by checking data with external data, so I am not using any of the access reporting tools.
This is an internal tools and will be used only by me, so the UI is not that important.
What I am doing is to write a VBA code that read recordset and external data and check the recordset.
I can do this, but I need a way to print this on UI. Something such as this:
Record no 10 is not valid : reason XXXX
Record 15 is not valid: reason YYYY
I was thinking of using a multi line text box to output this on screen, but I have several problem:
I can not find any way to make a text box as a multi line text box. There is no multi line propertyso I can output a line break.
If the size of recordset is big and there are a lot of errors, I am getting error that the size pro text proprty is too big.
What control should I use? I was thinking about using a label control, but I'm not sure if it's possible.
The easiest way is to use Debug.Print (output goes to the Immediate window -> Ctrl+g).
An Access text box that is large enough is always multi-line capable, there is no special property for that. Just append vbCrLf to each line to create a line break.
Me.txtLog = Me.txtLog & strError & vbCrLf
But for really long text output, a log file is the best option.
To write text files: either use FileSystemObject or the old fashioned Open statement.
The UserForm control in VBA has a MultiLine property, which you can set to True.
The other property is the EnterKeyBehavior that needs to be set to True to allow line breaks.
Alternately yes, you could use a Label control to achieve the same and even format it to look like a TextBox if you care.
But if long string of data, I would consider outputing it straight to a file with Open and For Output or For Append.

How can I write to a new column in a CSV file using VB Script?

how can I make the 'Hi' write to a new column (but same row) in CSV? I thought the comma should do the trick but it doesn't.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile("test.csv", _
ForWriting, True)
objLogFile.Write "Hello,"
objLogFile.Write "Hi"
objLogFile.Writeline
The comma has nothing to do with it. The Write leaves the current position on the same line, and the WriteLine moves it to the next line. So your script produces this:
Hello,Hi
which, I presume, is what you want.
Just in case your real problem is that the application you use to view test.csv shows Hello,Hi in one cell: You need to tell that application to use comma as field separator.

Import CSV data from web service into Excel

I have written a simple web service that returns large volumes of csv data. I will to import this into Excel in a tabular format using Excel's "Data From Web" function.
Is there a way to get Excel to automatically parse the csv fields returned into individual columns as part of the import operation?
At present the only means I have for doing this is to first import the data into a single column and then write VBA code to select the data and split it using TextToColumns. This feels messy / error-prone.
The other alternative I have is to modify the web server to serve back the data as HTML. However, I'm reluctant to do this as adding tags around each csv field will greatly impact the volume of data returned.
Adamski,
Here is something that I use. I found the core somewhere on the internet, but don't know where.
What it does is it opens a tab separated file and reads the data in an excel sheet
If Answer1 = vbYes Then 'I asked prior if to import a tab separated file
Sheets("ZHRNL111").Select 'Select the sheet to dump the data
On Error Resume Next
With ActiveSheet
If .AutoFilterMode Then .ShowAllData 'undo any autofilters
End With
Sheets("ZHRNL111").Cells.Clear 'remove any previous data
On Error GoTo 0
Range("A1").CurrentRegion.Delete
Fname = MyPath & "\LatestReports\Report-111.tsv"
Open Fname For Input As #1
iRow = 1
Line Input #1, Record
On Error Resume Next
Do Until EOF(1)
P = Split(Record, vbTab)
For iCol = 1 To 14
Cells(iRow, iCol) = P(iCol - 1)
Next iCol
iRow = iRow + 1
Line Input #1, Record
Loop
On Error GoTo 0
Close 1
End If
Regards,
Robert Ilbrink
Depending on the version of excel you are running you should be able to open the .csv in excel and use the text to columns feature built into excel.
Also, if you could modify your csv to split columns based on commas "," instead of tabs excel would open it directly without the need to format it.
I know however this can sometimes be a problem depending on the data you are importing because if the data contains a comma it must be inside quotations.
In my experience the best way is to use quotations on every field if possible.
Hope this helps.
I am actually creating a product right now to do this in both XML and JSON for Excel. I know comma delimited does work in Excel, with some caveats. One way around it is to put some "" around the text in between the delimiters for the "Data From Web" feature. There are still issues with that however. I did find that despite it's increased size, XML was the best option for quick turn around. I was able to create the service and hand my project manager the Excel document which he could update at anytime.

Writing to an external file using ACCESS 97

I'm using classic VBA and I'm having problems creating an external file.
Bassically using a table I want to create a standard txt file.
I already found a way to check if the file exists and kill it.
If Dir("%USERPROFILE%\Documents\iMacros\Macros\CriarGDC.iim") <> "" Then
Kill "%USERPROFILE%\Documents\iMacros\Macros\CriarGDC.iim"
Else
End If
and also a way to write all the lines I need
Open "%USERPROFILE%\Documents\iMacros\Macros\CriarGDC.iim" For Append As #1
Print #1, "1100258698,4"
Close #1
and also found a way to launch firefox with the imacros file
Problem IS:
I CANNOT CREATE the file.
I tried using
Shell ("cmd.exe")
SendKeys ("dir /s/b %USERPROFILE%\Documents\iMacros\Macros\*.itwontfindanything > %USERPROFILE%\Documents\iMacros\Macros\CriarGDC.iim")
But I'm having some permissions problems - windows 7 won't allow me to do that.
A lot of people told me that the command
Sub WriteFile(ByVal FileName As String, ByVal Contexto As String)
Dim fnum%
fnum = FreeFile()
Open FileName For Output As fnum
Print #fnum, Contexto
Close #fnum
End Sub
Should work, but it just doesn't!
Erm...
Help.
When giving your WriteFile procedure a FileName which includes an environment variable (such as "%USERPROFILE%\Documents\foo.txt"), VBA gives me error #76, 'Path not found".
Use the Environ() function to resolve your environment variable before you give it to WriteFile.
WriteFile Environ("USERPROFILE") & "\Documents\iMacros\Macros\CriarGDC.iim", "some text"