Solved

How do I send MSFlexGrid data to a tab-delimited text file in VB6 so that all the columns remain aligned?

Posted on 2010-09-03
10
638 Views
Last Modified: 2012-06-27
I want to send the data from VB6 MSFlexGrid1 to a tab-delimited text file and preserve the column alignments. Anyone know how to do this? Thanks for any help...
0
Comment
Question by:jazjef
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 11

Expert Comment

by:kbirecki
ID: 33602416
Ok, first, tab delimited text files won't look like the columns are aligned unless you open in an app like Excel that can show columns properly.  Is that what you expect?

Regarding the flexgrid, is your data based on a record set or data you manually pushed into the control?
0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 33603052
Kbirecki is correct.. Tabs in a plain text file don't automatically present the text in columns. Only the application used to view the text file can use the tabs to attempt to display it in columns.
Excel won't do it directly, you have to import the file into excel and confirm that it is a TAB file.
Other Apps like Wordpad still require you to set TAB widths manually..
What do you plan to use this file for? A Printed Report? or to email this file as an Invoice or Report?
This might help the ExEx folk give you some specific advice..
0
 
LVL 4

Author Comment

by:jazjef
ID: 33603129
I would like it to be printable---but viewable as a text file type report so you don't need Excel in order to view it. The data would be manually pushed/entered into the control----there would be no recordset or datasource feeding it. What about the DataReports feature? Could you feed it from an MSFlexGrid? Are there any add-ons that will take the data of a grid and generate a simple report that you know of. Thanks for the responses.....
0
 
LVL 11

Expert Comment

by:kbirecki
ID: 33603302
The MSFlexGrid is a display element.  I think whatever your source is should be the source for any other output.  Otherwise you;d have to write something that parses the contents of the flexgrid to spit them back out somewhere.  If you have the data to put into the flexgrid, just use that to push to your output format.

As for a text file output in columns, you also have to think about whether the viewer is using a monospace font or not.  Monospace fonts allow you to display text in columns, but only if you can be sure the recipient is using a viewer that is showing in monospace.
0
 
LVL 5

Expert Comment

by:BrianVSoft
ID: 33603306
By using Printer.CurrentX and Printer.CurrentY you can print each cell text to exact column widths. You can even print vertical lines between the columns and a box around the whole format..
Then, by using one of the free PDF printer apps, you can direct that same printout to a PDF file which can be viewed by most PC users. The viewer is freely available.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Accepted Solution

by:
VBClassicGuy earned 500 total points
ID: 33605691
Here is a routine I use to print out a FlexGrid in "report" format. You can specify a PDF printer if you so desire. The routine takes two parameters, "fg", the name of the FlexGrid, and "sel%", which if non-zero means print only the selected rows. The routine calls three printing routines, Print LJ, Print CJ, and Print RJ. They are included after the main routine's code. The main routine also calls a routine called "PrintHdr", which merely prints a header on the page.You can comment it out, or make your own. Lastly, my FlexGrid's differ from most in that when I initialize them, I add a beginning row that is only 15 twips wide. It makes a good-looking dividing line, and also allows me to call a routine that clears the contents of a FlexGrid while leaving the header intact. So, my first row (row 0) is the header, second row (row 1) is almost invisible, and my first row of data starts at the third row (row 2). You can not use this "invisible" row and change the code accordingly if you like. Or yeah, if the FlexGrid's Tag property is "NoEmpty", any blank rows will not be printed.
Anyway, using variable-spaced fonts (no mono-spaced fonts needed), the rows line up and the alignment is preserved. Here is the code:

Sub FlexPrint(fg As MSFlexGrid, sel%)
   Dim tabs%(2, 48)
   
   page% = 1
   
   txt$ = fg.Tag
   If Left$(txt$, 7) = "NoEmpty" Then
      NoEmpty% = True
      chkCol% = Val(Right$(txt$, 2))
   End If
   
   Rem *** Calc column width factor ***
   adj! = Round(Printer.fontSize) / 10
   
   Rem *** Calc total column gaps - add space if an LJ is preceded by an RJ ***
   For c% = 1 To fg.Cols - 1
      Select Case fg.ColAlignment(c%)
         Case 0 To 2 'LJ
            Select Case fg.ColAlignment(c% - 1)
               Case 6 To 8 'RJ
                  gap% = gap% + 50
               Case 9      'generic
                  txt$ = fg.TextMatrix(2, c% - 1)
                  If IsNumeric(txt$) Then 'RJ
                     gap% = gap% + 50
                  End If
            End Select
         Case 9
            txt$ = fg.TextMatrix(2, c%)
            If IsNumeric(txt$) Then
               Rem *** NOP ***
            Else
               Select Case fg.ColAlignment(c% - 1)
                  Case 6 To 8 'RJ
                     gap% = gap% + 50
                  Case 9      'generic
                     txt$ = fg.TextMatrix(2, c% - 1)
                     If IsNumeric(txt$) Then 'RJ
                        gap% = gap% + 50
                     End If
               End Select
            End If
      End Select
   Next c%
   
   Rem *** Calc line length ***
   For c% = 0 To fg.Cols - 1
      w% = w% + fg.ColWidth(c%) * adj!
   Next c%
   w% = w% + gap%
   
   Rem *** Set margin ***
   margin% = (Printer.ScaleWidth - w%) / 2
   
   Rem *** Calc column LJ/RJ positions ***
   tabs%(0, 0) = margin%   'LJ
   tabs%(1, 0) = margin% + fg.ColWidth(0) * adj!   'RJ
   tabs%(2, 0) = tabs%(0, 0) + ((tabs%(1, 0) - tabs%(0, 0)) \ 2)  'CJ
   sofar% = tabs%(1, 0)
   Rem *** Only add space between columns if an LJ is preceded by an RJ ***
   For c% = 1 To fg.Cols - 1
      Select Case fg.ColAlignment(c%)
         Case 0 To 2 'LJ
            Select Case fg.ColAlignment(c% - 1)
               Case 6 To 8 'RJ
                  gap% = 50
               Case 9      'generic
                  txt$ = fg.TextMatrix(2, c% - 1)
                  If IsNumeric(txt$) Then 'RJ
                     gap% = 50
                  End If
               Case Else
                  gap% = 0
            End Select
         Case 9
            txt$ = fg.TextMatrix(2, c%)
            If IsNumeric(txt$) Then 'RJ
               gap% = 0
            Else
               Select Case fg.ColAlignment(c% - 1)
                  Case 6 To 8 'RJ
                     gap% = 50
                  Case 9      'generic
                     txt$ = fg.TextMatrix(2, c% - 1)
                     If IsNumeric(txt$) Then 'RJ
                        gap% = 50
                     End If
                  Case Else
                     gap% = 0
               End Select
            End If
         Case Else
            gap% = 0
      End Select
      tabs%(0, c%) = sofar% + gap%  'LJ
      tabs%(1, c%) = tabs%(0, c%) + fg.ColWidth(c%) * adj!  'RJ
      tabs%(2, c%) = tabs%(0, c%) + ((tabs%(1, c%) - tabs%(0, c%)) \ 2) 'CJ
      sofar% = tabs%(1, c%)
   Next c%
   
   Rem *** Print header ***
   If Len(fg.TextMatrix(0, 0)) Then
      Rem *** Only prints header if Row 0 has text ***
      Printer.FontBold = True
      For c% = 0 To fg.Cols - 1
         If fg.ColWidth(c%) Then
            Select Case fg.ColAlignment(c%)
               Case 0 To 2   'LJ
                  Call PrintLJ(tabs%(0, c%), fg.TextMatrix(0, c%), False)
               Case 3 To 5   'CJ
                  Call PrintCJ(tabs%(2, c%), fg.TextMatrix(0, c%), False)
               Case 6 To 8   'RJ
                  Call PrintRJ(tabs%(1, c%), fg.TextMatrix(0, c%), False)
               Case 9
                  txt$ = fg.TextMatrix(0, c%)
                  If IsNumeric(txt$) Then
                     Call PrintRJ(tabs%(1, c%), fg.TextMatrix(0, c%), False)
                  Else
                     Call PrintLJ(tabs%(0, c%), fg.TextMatrix(0, c%), False)
                  End If
            End Select
         End If
      Next c%
      Printer.Print
      Printer.CurrentY = Printer.CurrentY + 50
      Printer.Line (margin%, Printer.CurrentY)-(margin% + w%, Printer.CurrentY)
      Printer.CurrentY = Printer.CurrentY + 50
   End If
   Rem *** Set lines to print ***
   If sel% Then
      st& = fg.Row
      nd& = fg.RowSel
   Else
      st& = 2
      nd& = fg.Rows - 1
   End If
   
   Rem *** Print it! ***
   Printer.FontBold = False
   For r& = st& To nd&
      DontPrint% = False
      If NoEmpty% Then
         txt$ = fg.TextMatrix(r&, chkCol%)
         If (Len(txt$) = 0) Or (Val(txt$) = 0) Then
            DontPrint% = True
         End If
      End If
      If Not DontPrint% Then
         If Printer.CurrentY + Printer.TextHeight("Sample") > Printer.ScaleHeight Then
            Printer.NewPage
            GoSub StartNewPage
         End If
         For c% = 0 To fg.Cols - 1
            DontPrint% = False
            If NoEmpty% And (c% = chkCol%) Then
               txt$ = fg.TextMatrix(r&, c%)
               If (Len(txt$) = 0) Or (Val(txt$) = 0) Then
                  DontPrint% = True
               End If
            End If
            If fg.ColWidth(c%) Then
               Select Case fg.ColAlignment(c%)
                  Case 0 To 2   'LJ
                     Call PrintLJ(tabs%(0, c%), fg.TextMatrix(r&, c%), False)
                  Case 3 To 5   'CJ
                     Call PrintCJ(tabs%(2, c%), fg.TextMatrix(r&, c%), False)
                  Case 6 To 8   'RJ
                     Call PrintRJ(tabs%(1, c%), fg.TextMatrix(r&, c%), False)
                  Case 9
                     txt$ = fg.TextMatrix(r&, c%)
                     If IsNumeric(txt$) Then
                        Call PrintRJ(tabs%(1, c%), txt$, False)
                     Else
                        Call PrintLJ(tabs%(0, c%), txt$, False)
                     End If
               End Select
            End If
         Next c%
         Printer.Print
      End If
   Next r&
   
   Exit Sub
   
StartNewPage:
   page% = page% + 1
   Call PrintHdr(page%)
   Printer.FontBold = True
   Printer.Print
   Rem *** Print legend ***
   For c% = 0 To fg.Cols - 1
      Select Case fg.ColAlignment(c%)
         Case 1   'LJ
            Call PrintLJ(tabs%(0, c%), fg.TextMatrix(0, c%), False)
         Case 4   'CJ
            Call PrintCJ(tabs%(2, c%), fg.TextMatrix(0, c%), False)
         Case 7   'RJ
            Call PrintRJ(tabs%(1, c%), fg.TextMatrix(0, c%), False)
      End Select
   Next c%
   Printer.Print
   Printer.CurrentY = Printer.CurrentY + 50
   Printer.Line (margin%, Printer.CurrentY)-(margin% + w%, Printer.CurrentY)
   Printer.CurrentY = Printer.CurrentY + 50
   Printer.FontBold = False
   Return
End Sub
 
Sub PrintLJ(pos%, txt$, crlf%)
   If pos% = 0 Then
      Printer.CurrentX = Printer.ScaleWidth / 2
   Else
      Printer.CurrentX = pos%
   End If
   Printer.Print txt$;
   If crlf% Then Printer.Print
End Sub
Sub PrintCJ(pos%, txt$, crlf%)
   If pos% = 0 Then
      Printer.CurrentX = Printer.ScaleWidth / 2 - Printer.TextWidth(txt$) / 2
   Else
      Printer.CurrentX = pos% - Printer.TextWidth(txt$) / 2
   End If
   Printer.Print txt$;
   If crlf% Then Printer.Print
End Sub
Sub PrintRJ(pos%, txt$, crlf%)
   If pos% = 0 Then
      Printer.CurrentX = Printer.ScaleWidth / 2 - Printer.TextWidth(txt$)
   Else
      Printer.CurrentX = pos% - Printer.TextWidth(txt$)
   End If
   Printer.Print txt$;
   If crlf% Then Printer.Print
End Sub
0
 
LVL 11

Expert Comment

by:kbirecki
ID: 33606345
VBClassicGuy, now that's a silver platter!  That looks like a lot of work.  Good job!
0
 
LVL 4

Author Comment

by:jazjef
ID: 33623809
That's pretty elaborate vbclassicguy.... over my head; I don't know if I can implement that. I was thinking about something like this.... it will print an msflexgrid but without a vbcrlf somewhere if you print more than 1 then they print on top of each other. This will send an msflexgrid to Primo PDF or my HP Laserjet printer....

'0 = Primo PDF printer
'1 = print-to-file dialog
'2 = *.xps
'3 = ERROR
'4 = Laserjet 110
'5 = sendfax

Set Printer = Printers(0)
Printer.PaintPicture grd_VisStats.Picture, 0, 0
Printer.EndDoc

 
0
 
LVL 4

Author Closing Comment

by:jazjef
ID: 33686227
Nice solution VBClassicGuy... major-league coding. I have included another method I got to work in case anyone following this post is interested. Thanks to all....

'_____PRINT the gridview controls

CommonDialog3.PrinterDefault = True
CommonDialog3.CancelError = True

'_____Set flags - no page numbers, return the selected printer
CommonDialog3.flags = cdlPDReturnDC + cdlPDNoPageNums

'_____Enables error handling to catch cancel error
On Error Resume Next
'_____display the print dialog box
CommonDialog3.ShowPrinter

If Err Then
    '_____This code runs if the dialog was cancelled
    MsgBox "Printing Cancelled"
    Exit Sub
End If

'____Prints the msflexgrids
Printer.PaintPicture MSFlexGrid1.Picture, 500, 500
Printer.PaintPicture MSFlexGrid2.Picture, 500, 5000
Printer.PaintPicture MSFlexGrid3.Picture, 500, 9500

Printer.EndDoc
0
 
LVL 14

Expert Comment

by:VBClassicGuy
ID: 33686737
Well, thank you , Sir. Glad to help out. Happy coding...
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now