Set TextBox and Label text to Vertically Align Center (1 Viewer)

abzalali

Registered User.
Local time
Today, 22:12
Joined
Dec 12, 2012
Messages
118
Code:
Public Sub VerticalAlignCenter(ByRef ctl As Control)
On Error GoTo ErrorCode
    Dim MinimumMargin As Integer
    Dim BorderWidth As Integer
    Dim TwipsPerPoint
    TwipsPerPoint = 20
    If Not ((TypeOf ctl Is TextBox) Or (TypeOf ctl Is Label)) Then Exit Sub
   'Figure out how many lines it is
   Dim LenOfText, WidOfBox, NumberOfLines, HtOfText
   If TypeOf ctl Is TextBox Then
    LenOfText = ctl.Text
    Else:
    LenOfText = ctl.Caption
    End If
    'how wide is this puppy?
    WidOfBox = ctl.Width
    LenOfText = (Len(LenOfText) * TwipsPerPoint * ctl.FontSize) / 2
    NumberOfLines = Int(LenOfText / WidOfBox) + 1
    HtOfText = NumberOfLines * TwipsPerPoint * ctl.FontSize
    
   
    MinimumMargin = 1 * TwipsPerPoint
    BorderWidth = (ctl.BorderWidth * TwipsPerPoint) / 2
    
    ctl.TopMargin = ((ctl.Height - HtOfText) / 2) - MinimumMargin - BorderWidth
    
ErrorCode:
    Exit Sub
End Sub

I need a little help (I tried for over two hours to make this work, still new to all this). What I don't know is how / were are you calling the VerticalAlignCenter function in report?
 

abzalali

Registered User.
Local time
Today, 22:12
Joined
Dec 12, 2012
Messages
118
It's work only Access 2000! And 32 bit only@Cronk.

Thanks
Mir
 

missinglinq

AWF VIP
Local time
Today, 11:12
Joined
Jun 20, 2003
Messages
6,423
It's work only Access 2000! And 32 bit only@Cronk

Did you try it? I suspect Stephen put that warning in there to make it understood that it wouldn't work in the previous version, i.e. Access 97! Access 2000 was quite a departure from 97.

Linq ;0)>
 

JHB

Have been here a while
Local time
Today, 17:12
Joined
Jun 17, 2012
Messages
7,732
By me it works in MS-Access 2010, accdb format.

 

Attachments

  • Vertical.jpg
    Vertical.jpg
    38 KB · Views: 20,684

abzalali

Registered User.
Local time
Today, 22:12
Joined
Dec 12, 2012
Messages
118
Dear JHB,

Could you tell me how refer code to the text box. "Lebans code for only 32 bit systems"

Thanks
Mir
 

JHB

Have been here a while
Local time
Today, 17:12
Joined
Jun 17, 2012
Messages
7,732
Could you be more specific?
 

abzalali

Registered User.
Local time
Today, 22:12
Joined
Dec 12, 2012
Messages
118
Dear JHB,

It works but do you try it in 64 bit?
http://www.lebans.com/verticaljustification.htm

I just want know how to refer/use the code shown below in report:
Code:
Public Sub VerticalAlignCenter(ByRef ctl As Control)
On Error GoTo ErrorCode
    Dim MinimumMargin As Integer
    Dim BorderWidth As Integer
    Dim TwipsPerPoint
    TwipsPerPoint = 20
    If Not ((TypeOf ctl Is TextBox) Or (TypeOf ctl Is Label)) Then Exit Sub
   'Figure out how many lines it is
   Dim LenOfText, WidOfBox, NumberOfLines, HtOfText
   If TypeOf ctl Is TextBox Then
    LenOfText = ctl.Text
    Else:
    LenOfText = ctl.Caption
    End If
    'how wide is this puppy?
    WidOfBox = ctl.Width
    LenOfText = (Len(LenOfText) * TwipsPerPoint * ctl.FontSize) / 2
    NumberOfLines = Int(LenOfText / WidOfBox) + 1
    HtOfText = NumberOfLines * TwipsPerPoint * ctl.FontSize
    
   
    MinimumMargin = 1 * TwipsPerPoint
    BorderWidth = (ctl.BorderWidth * TwipsPerPoint) / 2
    
    ctl.TopMargin = ((ctl.Height - HtOfText) / 2) - MinimumMargin - BorderWidth
    
ErrorCode:
    Exit Sub
End Sub

Thanks
Mir
 

Cronk

Registered User.
Local time
Tomorrow, 01:12
Joined
Jul 4, 2013
Messages
2,772
I tried it in a 64 bit PC. Did you?

The example of the report in the sample database shows how to vertically centre text.
 

JHB

Have been here a while
Local time
Today, 17:12
Joined
Jun 17, 2012
Messages
7,732
Dear JHB,

It works but do you try it in 64 bit?
http://www.lebans.com/verticaljustification.htm

I just want know how to refer/use the code shown below in report:
My computer is running Windows 8 64 bit-operation system.


As Cronk writes, look at the report in the sample database.
You need to include all the code from the sample database in your database.
If you can't get it then post a snipped version of your database with some sample data in it (zip it) + information in which report you want to use the function.

Take a look at the below code, here is listed which control should use the function, do it in same way in your report.
Code:
Private Sub Detail_Format(Cancel As Integer, FormatCount As Integer)
' You have to call the function every time
' during the Report's Detail Format event.
VerticallyCenter Me.txtCustomerID2
VerticallyCenter Me.txtCustomerName2
VerticallyCenter Me.txtTestMemo2
End Sub
 

Attachments

  • 64bit.jpg
    64bit.jpg
    5 KB · Views: 19,214

hundred4ever

New member
Local time
Today, 08:12
Joined
Jun 7, 2016
Messages
1
Using Access 2013 (64-bit).

To vertically align the text in my labels, I changed the Top Margin value. Right under Gridline Width Right in the Properties section.

Hope this helps. :)

--

Questions:

What are some ways to change a label's caption while in a form?

What are some ways to link a label to a table's field name?

Thanks!
 
Last edited:

idefaz

New member
Local time
Today, 08:12
Joined
Apr 11, 2019
Messages
1
Code:
Private Sub horcenter()
    If ErrCatchOn Then On Error GoTo Grr
    
    Dim ctl As Control
    For Each ctl In Me.Controls
        If ctl.ControlType = acLabel Then
            ctl.TopMargin = ((ctl.Height - (ctl.FontSize * 20)) / 2)
        End If
    Next ctl
    
    horset = True
        
GrrExit:
    Exit Sub
Grr:
    MsgBox Err.Description
    Resume GrrExit
End Sub
 

Users who are viewing this thread

Top Bottom