How to Calculate Character Width

Posted on 2005-04-26
Last Modified: 2012-06-27
In Access XP I have a continuous bound form with 2 controls:

- txtRecordID bound to RecordID
- txtRecord bound to Record

Each of these controls has FontName=CourierNew and FontSize=10 so that each character has the same width

The record is actually made up of 10 columns. So I have 10 labels in the form header. I also have a settings table that contains the names of each of these labels as well as its width in characters. In the Form_Load event I dynamically assign the the Left and Width attributes of each label so that it properly defines the start and end postion of each column. The detail section height is twice the height to txtRecord. Just above the text box I dynamically position some vertical lines to further show the field separation.

My problem is that the positioning of the form header labels and vertical lines in the detail section is never quite right across the entire width of txtRecord. I've tried the following base assumptions without total success:

dim lngUnitWidth as long

lngUnitWidth = 1440 / 12 ' the form grid is 24 x 24 and each character seems be be about 2 grid lines wide

lngUnitWidth = me.txtRecord.Width / 90 ' the text box is full at 90 characters

Any suggestions to help me with this dynamic alignement of labels and vertical bars?
Question by:rmk
    LVL 39

    Expert Comment

    Are you saying the vertical lines and controls in the detail section does not line up with the labels in the header?

    If so try:

    [vertical line].left = [header label].left

    [text box in detail section].left = [header label].left
    [text box in detail section].width = [header label].width

    Author Comment

    The vertical lines in the detail section line up correctly with the labels in the header section. However neither of these always line up nicely between characters in the txtRecord text box.

    Here is an example of the txtRecord contents for 1 record:


    This is my settings table that defines how wide each of the labels needs to be:

    BankFieldID      Name      Length
    1                      Code      1
    2                      BatchDate      8
    3                      BatchNbr      5
    4                      CheckNbr      15
    5                      TaxMap      15
    6                      AssocNbr      2
    7                      BillNbr      6
    8                      TaxYear      4
    9                      InvoiceAmt      9
    10                      CheckAmt      9
    11                      CheckChar      1
    LVL 48

    Expert Comment

    by:Gustav Brock
    You can pick some ideas how to solve this here:

    LVL 39

    Expert Comment

    What I would do is use Mid statements to parse out each piece of the record into separate text fields.  It sure would be a lot easier and a lot easier to read.  You could even format each text field  appropriately.  Each Mid statement can go in the text field control source property.  For example:
    in the txtBatchDate control source put:    =Mid([txtRecord], 2, 8)

    I'm assuming in the above example "E200312020011900000000000221406000240001005302010797200400013156000001315609" is a txtRecord

    Author Comment

    I don't want to parse out the fields because I have to allow the users to add or delete characters in the txtRecord text box and have everything shift accordingly. This is a lock box application and unfortunately the bank does not always send properly formatted data. I also have some buttons that let the user do bulk character inserts or deletes across a range of records. It all works quite nicely. By fiddling around, I've now got the labels lining up well enough that it's not worth any more effort.
    LVL 39

    Expert Comment

    I'm glad you got it worked out.

    Accepted Solution

    The question has been PAQ'd and the 250 points have been refunded.
    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    This isn't a frequent question on EE. I must have seen it three or four times (among several thousand questions). However, I use this trick quite often, most frequently as a delayed Current event. A form does not expose it's calculation dependenc…
    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

    779 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

    17 Experts available now in Live!

    Get 1:1 Help Now