Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Printing a Table

Status
Not open for further replies.

orisons

Programmer
Feb 18, 2002
49
CA
I have nearly completed my "FIRST" not out of the book vb project, but I am having a problem I would like data I have had from user to be displayed on a table (this I can achieve on screen no problem, with various labels BLAH BLAH, but I would like to know how to print a table from the printer, every time I try to print I can get text, but have now Idea how to get it in a table form to make the hard copy more presentable and easier to understand, please note in your answer that the word "Newbie" applies to people with more knowledge of VB than me
Thankyou for any help
 

If you are using the printer object you need to build each line before you use the print method. Meaning...

MyStringVariable = DataAccessMethod.Field("FieldName") & MyDelimiterCharacter & DataAccessMethod.Field("FieldName2")

Ok, so we have a start but things don't seem to line up. Well instead of using a delimeter characrer like vbTab we can pad the contents of each value with something like...
[tt]
Option Explicit

Public Enum Side
Left = 0
Right = 1
End Enum


Private Sub Form_Load()

Dim S As String, A As String
S = "Test"

A = Pad(S, 25, Left)

End Sub

Public Function Pad(StringToPad As String, TotalLengthOfReturnString As Integer, LeftOrRight As Side, Optional CharacterToPadWith As String = " ") As String

On Error GoTo PadError

Dim StringLength As Integer, TotalLength As Integer, Difference As Integer

StringLength = Len(StringToPad)

Difference = TotalLengthOfReturnString - StringLength

If Difference > 0 Then

If LeftOrRight = Left Then

Pad = String(Difference, CharacterToPadWith) & StringToPad

Else

Pad = StringToPad & String(Difference, CharacterToPadWith)

End If

Else

Pad = StringToPad

End If

Exit Function
PadError:

MsgBox Err.Description

End Function
[/tt]

Good Luck

 
Thankyou for your promt reply and I will try this, if you dont mind could you explain some of it as I have said I am new and I dont want to end up in the "monkey see monkey do " dilema I would like to understand
Thankyou
 

Start a new project and copy the code into the code window and press F8. This will start to run the application and the line of "Form_Load" should be highlighted yellow.

As you see above this line I have created a UDE (User Defined Enumeration (Either 0 or 1(Left or Right))).

Below this you see I have declare procedural level variables "S and A" both string. I then set S = "Test".

Next I set "A" equal to the result of the "Pad" function.

As you can see the "Pad" function has a couple of arguements, namely StringToPad As String, TotalLengthOfReturnString As Integer, LeftOrRight As Side, Optional CharacterToPadWith As String = " ".

Lets take a quick look at "LeftOrRight As Side". That is the UDE I created above. Lets do a simple test.

In the form load event below where I set A = the result of the Pad function type in your own value for "S". Meaning...
[tt]
S = "YourStringValue"
[/tt]
Then Below that type in "A = Pad(". You will see a tooltip popup describing each arguement the function needs (no quotes). So continue typing "S, yournumbervalue," at this point you will see a little popup with two choices "Left Right". Pick one. You will also see in the tooltip that the last arguement is bracketed "[]". This means that the arguement is optional. Optional arguement to prodedures should and need to be placed last in the arguement list. Since it is optional you do not need to specify the character to pad the string with since its default is a space " ".

Ok, now into the pad function itself. The first line in the function is the beginning of an error handler. It means that if an error happens I want the code to jump to the "PadError" label. If you look at the bottom of the function you will see an "Exit Function" and the label "PadError:". You will also notice that once again I have declared procedural level variables (Just noticed I do not need the "TotalLength" variable).

Below that I use the "Len" function to return the length of the string that you passed in.

The next line I do some simple math to get the difference between the length of the string and how long you want it to be "Difference".

I then check the value of the "Difference" variable to see if it is greater than zero. It could be zero or even a negative number if you passed in a string length of 20 and told me you wanted the string to be 15 "Difference" would equal -5. So as you look down the code you will see and "Else" clause that matches up with the indentation of this if statement, and contained within I make the Pad function equal your origional string because it length is equal or greater than you requested length.

Now if the string that you passed in is less than the desired length I next check to see which side you want the padding on (LeftOrRight).

I then set the pad function equal to your string and the amount of padding needed to make it your length using your padding character. I do this with the "String" Function, which is different from variable declaration.

Other String Functions you can check out besides String and Len are... Left, Right, Mid, Str, Asc, Chr, StrReverse, InStr, InStrRev, Trim, RTrim, LTrim, and Space.

I think that will do for now. If you have any specific questions please feel free to ask this or the appropriate forum.

Good Luck

 

Oh yeah! after seeing another post and looking at mine you may have problems if you try to use the left and right functions with my UDE in there. So Lets change the UDE to...
[tt]
Public Enum Side
PadLeftSide = 0
PadRightSide = 1
End Enum
[/tt]
That should avoid any problems.

Good Luck

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top