-
Oct 12th, 2017, 12:14 PM
#1
Thread Starter
Member
How to introduce line break each 12 words when writing a file'?
Hi! I have the following code to read a FlexGrid and save the data in a txtFile. I want to introduce a break line each 12 parameters written that are separated by " ; ".
My code:
Code:
Private Sub Save_Data_Click()
Dim ret As Boolean
' Le envia el control MsFlexgrid, el path del archivo txt y el delimitador
ret = Export_Data(Grid1, "C:\Documents and Settings\Administrador\Escritorio\TFG\Registros\Register_Data.txt", Chr(59))
If ret Then
MsgBox "Table exported! ", vbInformation
End If
End Sub
Public Function Export_Data(MSFlexGrid As Object, Path_Txt As String, Delimitador As Variant) As Boolean
On Error GoTo Funcion_Error
Dim i, ja As Integer
Dim Free_File As Integer
' NĂºmero de archivo libre para crear el archivo de texto
Free_File = FreeFile
' Abre y crea el archivo
Open Path_Txt For Output As #Free_File
Print #Free_File, "#MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod#"
' Recorre las filas del Flexgrid
For i = 1 To Grid1.Rows - 1
Grid1.Row = Fi 'Empiezo en la fila 1 no en la cero
For j = 1 To Grid1.Cols - 1
Grid1.Col = j
' Write the data
'Print #Free_File, vbNullString & Grid1.text & vbNullString;
If j >= 1 Then
Print #Free_File, Grid1.text & Delimitador;
End If
Next
'Print #Free_File, ""
Next
Close
Exportar_Datos = True
' Fin
Exit Function
' error
Funcion_Error:
Close #Free_File
MsgBox Err.Description, vbCritical
End Function
What it shows in the file:
Code:
#MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod# (1sr line)
;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps-20MHz/1S;1131,1109;26m4s;210ms;-62@1Mbps;25 (2nd line) dB;99%;60958;192.168.100.253;CCK:1-11;;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps- (2nd line)
20MHz/1S;1207,1185;27m10s;440ms;-32@6Mbps;55 dB;96%;60488;192.168.100.253;CCK:1- (2nd line) 11;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (2nd line)
And I want:
Code:
#MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod# (1sr line)
;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps-20MHz/1S;1131,1109;26m4s;210ms;-62@1Mbps;25 (2nd line) dB;99%;60958;192.168.100.253;CCK:1-11; (2nd line)
;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps- (3rd line)
20MHz/1S;1207,1185;27m10s;440ms;-32@6Mbps;55 dB;96%;60488;192.168.100.253;CCK:1- (3rd line) 11;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (3rd line)
The ;;;;;;;;;;;;;;;; are blank cells. It would be a great idea delate it too....
Thank you!
-
Oct 12th, 2017, 12:24 PM
#2
Re: How to introduce line break each 12 words when writing a file'?
Hi ams16,
I didn't study your code, but I did study the two text files you posted. Also, you didn't post a screen-shot of your FlexGrid, so I'm not totally clear as to what we're looking at.
However, on the FlexGrid, it appears that you have a single row that you wish to break up into multiple lines in your text file. That seems fairly straightforward to me. Just casually looking at it, maybe you can check for "B8" in the start of a cell's text. If found, send a vbCrLf to your text file so that a new line is started.
Alternatively, maybe you could search for the entire "B8:64:91:68:1C:50". That seems to be what you're using to define a new line. However, I've got no idea what this data relates to.
Personally, it seems like a fairly straightforward parsing issue. You just need to thoroughly understand the structure of your data and how to appropriately parse it.
Good Luck,
Elroy
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Oct 12th, 2017, 03:33 PM
#3
Re: How to introduce line break each 12 words when writing a file'?
ams
I think I get your drift .. here is my initial stab at it.
Code:
With FG1
.Top = 500
.Left = 500
.Width = 9000 - 50
.Height = 1400
.Visible = True
.Cols = 12
.Rows = 5
.FixedCols = 0
For ii = 0 To 11
.ColWidth(ii) = 750
Next ii
' 1a. read 1st line .. has CR .. carriage Return
fpath = "D:\VBForums\amsf1.txt"
Open fpath For Input As #1
Line Input #1, xtr
' 1b. populate FG "headers"
For ii = 1 To 12
nn = InStr(xtr, ";")
If nn > 0 Then
txt = Left(xtr, nn - 1)
Else
txt = xtr
End If
.TextMatrix(0, ii - 1) = txt
' prep for next
xtr = Mid(xtr, nn + 1)
Next ii
' 2a. read 2nd line .. 1 very long line of text, no CR's
Line Input #1, xtr
Close #1
' 2b. split at each ";"
rr = 1
For rr = 1 To 3
For cc = 1 To 12
nn = InStr(xtr, ";")
If nn > 0 Then
txt = Left(xtr, nn - 1)
Else
txt = xtr
End If
.TextMatrix(rr, cc - 1) = txt
.Col = cc - 1
.CellAlignment = 0
' prep for next
xtr = Mid(xtr, nn + 1)
If cc = 12 Then
Exit For
End If
Next cc
Next rr
End With
.. here is the textfile as I interpreted it
Code:
#MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod#
;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps-20MHz/1S;1131,1109;26m4s;210ms;-62@1Mbps;25 dB;99%;60958;192.168.100.253;CCK:1-11;;B8:64:91:68:1C:50;65Mbps-20MHz/1S;65Mbps- 20MHz/1S;1207,1185;27m10s;440ms;-32@6Mbps;55 dB;96%;60488;192.168.100.253;CCK:1- 11;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Assumptions
- line 1 has a Carriage Return at the end if it
- line 2 does not .. it is one very long line of text
.. and here is a screenshot
Issues
- line 1, used for col headers, seems ok
- the other lines do not
- I removed the (1st line), etc, text as I assumed it was for discussion purposes
- Each new row seems to be offset by 1 col.
There seem to be some issues regarding location of your ";" separators
Perhaps you could post the actual text file
HTH
Spoo
-
Oct 12th, 2017, 06:21 PM
#4
Re: How to introduce line break each 12 words when writing a file'?
Gotta ask a simple question. Why not just output the text file in a format easier for you to parse?
Write to file, the record using whatever delimiter you want. And at the end of the record, write a carriage return. Now you can use Line Input to retrieve an entire record vs trying to parse out run-on record data. See my comments in the code, prefixed with <<<
Code:
For i = 1 To Grid1.Rows - 1
Grid1.Row = i 'Empiezo en la fila 1 no en la cero
For j = 1 To Grid1.Cols - 2 ' <<< note we changed this to 2
Grid1.Col = j
' Write the data
Print #Free_File, Grid1.Text & Delimitador;
Next
Grid1.Col = j ' <<< write last field and a carriage return
Print #Free_File, Grid1.Text & Delimitador ' <<< note no semicolon
Next
Edited: That final "Print" statement probably shouldn't include the Delimitador value.
If using code like the above, you can use Line Input statements to re-populate your grid
Code:
Dim sFields() As String, sRecord As String, sHeader As String
... open the file
Line Input #Free_File, sHeader
Do Until EOF(Free_File)
Line Input #Free_File, sRecord
If sRecord = String(Len(sRecord), Delimitador) Then
' empty record
ElseIf sRecord <> vbNullString Then ' ignore blank lines in text file
' populate your Grid
sFields() = Split(sRecord, Delimitador)
For j = 0 To UBound(sFields)
.... add field data to grid
Next
End If
Loop
Close #Free_File
Last edited by LaVolpe; Oct 13th, 2017 at 08:26 AM.
-
Oct 13th, 2017, 07:22 AM
#5
Re: How to introduce line break each 12 words when writing a file'?
Originally Posted by LaVolpe
Gotta ask a simple question. Why not just output the text file in a format easier for you to parse?
Indeed.
(Somewhat OT .. your boys did us in .. here's to hoping you go all the way)
-
Oct 13th, 2017, 07:46 AM
#6
Re: How to introduce line break each 12 words when writing a file'?
Originally Posted by Spooman
your boys did us in .. here's to hoping you go all the way)
That was a very fan-friendly game -- lots of plays to cheer & jeer for both teams' fans.
P.S. Dusty seems to be a playoff jinx. We dealt with it when he was with the Cubs
-
Oct 13th, 2017, 12:54 PM
#7
Thread Starter
Member
Re: How to introduce line break each 12 words when writing a file'?
Thank you for your help.
I explain you what I want with the next picture. I want to read my FlexGrid of two columns (I read only the second colum) and a variable number of rows because I am monitoring data and each time I click a button, a new group of data is added to the table.
In the next picture I show you what I have:
And in this picture I show you what I want:
The second MAC direction could be different from the first one.
-
Oct 14th, 2017, 04:56 PM
#8
Thread Starter
Member
Re: How to introduce line break each 12 words when writing a file'?
anybody knows?
-
Oct 14th, 2017, 05:16 PM
#9
Re: How to introduce line break each 12 words when writing a file'?
ams
Sorry to say, but I don't understand what you mean
Plus, your images are too small to read
Did you try the code in my post #3?
Spoo
-
Oct 14th, 2017, 07:50 PM
#10
Re: How to introduce line break each 12 words when writing a file'?
this line commented out in your code should put a new line for each row, but no need to print anything
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Oct 15th, 2017, 06:52 AM
#11
Re: How to introduce line break each 12 words when writing a file'?
-
Oct 15th, 2017, 10:14 AM
#12
Thread Starter
Member
Re: How to introduce line break each 12 words when writing a file'?
Thank you for your help Spoo but the solution of #3 is not what I want.
I have this grid:
As you can see, my application cathes the same data of various devices. In the next rows from "Dispositivo 2 (Device 2 in english)" there are the same data as you can se of "Dispositivo 1" but ovbiously with different values.
I want to read all the grid ( but only the second column) and save the data in rows in a txt file (1 row for each device). Example: If the app detects two devices I want this in my txt file:
#MAC; RbRx; RbTx; PKTs; Tact; LastAct; RSSI; SNR; CCQtx; Throughput; IP; Mod#
B8:64:91:68:1C:50;65Mbps-20MHz/1S;5.5Mbps;36,44;22s;150ms;-35@HT20-7;51 dB;52%;3971;192.168.100.253;CCK:1-11;
B8:64:91:68:1C:51;60Mbps-20MHz/1S;5.5Mbps;37,45;23s;460ms;-35@HT20-7;51 dB;54%;3971;192.168.100.253;CCK:1-11;
-
Oct 15th, 2017, 03:12 PM
#13
Re: How to introduce line break each 12 words when writing a file'?
try like
Code:
Grid1.column = 2
For i = 1 To Grid1.Rows - 1 step 13
j = 1 to 12
grid1.row = i + j
Print #Free_File, Grid1.text & Delimitador;
next
print #free_file
next
assumes that Dispositivo 1 is the text in row 1, this is untested
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|