embedding a font in access

BubBob

Registered User.
Local time
Today, 10:13
Joined
May 20, 2003
Messages
10
Is it possible? I need to embed a barcode font. It is possible to use the font through macros, but it would be much easier to use embbed font in a report.
 
I may be missing something here since I have never had occasion to use a barcode font but wouldn't you just change the font of a textbox when you have the report open in design view. Then when you save the report, the font reference is also saved.
 
But the thing is you have to install the font manually to every computer where the program is used.
 
The Font could be stored as an OLE Object in a Table but to prevent bloat, to a large degree, should be stored as a “raw” Long Binary Object.

In this example I have used the registration code by Fabrizio Ranieri referenced in the link supplied by Norman.

Code:
Option Explicit
Option Compare Text

Private Declare Function AddFontResource& Lib "gdi32" Alias "AddFontResourceA" (ByVal lpFileName As String)
Private Declare Function RemoveFontResource& Lib "gdi32" Alias "RemoveFontResourceA" (ByVal lpFileName As String)


[color=green]'  Called from the Macro AutoExec at startup.[/color]
Public Function AutoExec()

    InstallUserFiles
    
End Function


Public Function GetCurrentDB(ByVal strArgument As String) As String

    Select Case strArgument
           
        Case "User"
            GetCurrentDB = CurrentUser()
        
        Case "PathAndName"
            GetCurrentDB = CurrentDb.Name
        
        Case "FullName"
            GetCurrentDB = Dir(CurrentDb.Name)
        
        Case "Extension"
            GetCurrentDB = Right$(Dir(CurrentDb.Name), 4)
        
        Case "Name"
            GetCurrentDB = Left$(Dir(CurrentDb.Name), Len(Dir(CurrentDb.Name)) - 4)
        
        Case "Path"
            GetCurrentDB = Left$(CurrentDb.Name, Len(CurrentDb.Name) - Len(Dir(CurrentDb.Name)))
        
        Case Else
            GetCurrentDB = "GetCurrentDB(" & Chr$(34) & strArgument & Chr$(34) & ") :- Invalid argument."

    End Select
    
End Function


Private Sub DownloadTableToFile(ByRef fldFieldName As [b]DAO.[/b]Field, _
                                ByVal strFileName As String)
    Dim bytBuffer() As Byte
    Dim intFileNum  As Integer
    
    On Error GoTo ErrorHandler

    intFileNum = FreeFile
    
    Open strFileName For Binary As intFileNum
      
    ReDim bytBuffer(fldFieldName.FieldSize)
    bytBuffer = fldFieldName.GetChunk(0, fldFieldName.FieldSize)
    Put intFileNum, , bytBuffer()
       
ExitProcedure:
    On Error Resume Next
    Close intFileNum
    ReDim bytBuffer(0)
    Exit Sub
    
ErrorHandler:
    Select Case Err.Number
    
        Case 0
            [color=green]' Take action on known errors, or better yet fix them.[/color]
            Resume ExitProcedure
        
        Case Else
            MsgBox "Error in Sub DownloadTableToFile: " & CStr(Err.Number) & vbNewLine & _
                   Err.Description
            Resume ExitProcedure
        
    End Select
    
End Sub


Private Sub InstallUserFiles()
    Dim strFileName  As String
    Dim rstUserFiles As DAO.Recordset
    
    Set rstUserFiles = CurrentDb.OpenRecordset("tblUserFiles", dbopendynaset)
        
    Do Until rstUserFiles.EOF
       
       strFileName = rstUserFiles!FileName
    
       If Dir(GetCurrentDB("Path") & strFileName) = "" Then
           DownloadTableToFile rstUserFiles!File, GetCurrentDB("Path") & strFileName
       Else
           UploadFileToTable GetCurrentDB("Path") & strFileName, rstUserFiles!File, rstUserFiles
       End If
       
       If (rstUserFiles!RegisterFont) Then
           If AddFontResource(GetCurrentDB("Path") & strFileName) <> 1 Then
               MsgBox "Font " & strFileName & " failed to register."
           End If
       End If
       
       rstUserFiles.MoveNext
       
    Loop
    
    Set rstUserFiles = Nothing

End Sub


Private Sub UploadFileToTable(ByVal strFileName As String, _
                              ByRef fldFieldName As [b]DAO.[/b]Field, _
                              ByRef rstTableName As DAO.Recordset)
    Dim bytBuffer() As Byte
    Dim intFileNum  As Integer
    
    On Error GoTo ErrorHandler
    
    intFileNum = FreeFile
    
    Open strFileName For Binary As intFileNum
    
    ReDim bytBuffer(LOF(intFileNum))
    
    Get intFileNum, , bytBuffer
    
    rstTableName.Edit
    fldFieldName.AppendChunk bytBuffer
    rstTableName.Update
    
ExitProcedure:
    On Error Resume Next
    Close intFileNum
    ReDim bytBuffer(0)
    Exit Sub
    
ErrorHandler:
    Select Case Err.Number
    
        Case 0
            [color=green]' Take action on known errors, or better yet fix them.[/color]
            Resume ExitProcedure
        
        Case Else
            MsgBox "Error in Sub UploadFileToTable: " & CStr(Err.Number) & vbNewLine & _
                   Err.Description
            Resume ExitProcedure
        
    End Select

End Sub
The attached demo in A2K will not work first up because there is no Font embedded in Table tblUserFiles and the name of the file must be saved in that Table.

Procedure to get it working: -

Copy the demo to a new directory.
Copy your Font to the same directory.
Shift Down start the demo.
Go to Table tblUserFiles and in the FileName Field insert the name of the Font.
Close the demo and re-open it normally.
Close the demo and rename the Font, simply put an “x” in front of its name.
Restart the demo.
Close it and there should be another Font, by the correct name, in the same directory.
Make a new directory and copy the demo only to that directory.
Open the demo and close it.
You should have a fresh copy of the Font in the new directory.

You may also want to delete the Font on database shutdown but I have not done that here.

Lastly, and most important of all, play with it till you are absolutely sure you know how it works. Please do not use it if you are unsure.

PS.
It is difficult to test on a single machine.
I used the MTCORSVA.TTF font for testing and after taking Pat’s suggestion configured a ComboBox to that Font in a new database.
Close and save that database.
Make a copy and delete the MTCORSVA.TTF Font from the C:\WINNT\Fonts directory.
Reboot the machine.
Go to the database that uses that Font and it will not be displayed.
Close that database and open the test demo.
Close the demo and re-open the database that uses that Font and it will be displayed.

Best test may be, if you can, to configure a database for the Barcode Font and include the above code and without the Barcode Font.
Take that database to a machine that does not support that Font.
Test, test and test again.


Regards,
Chris.
 

Attachments

Last edited:
hi,

it got ur demo version of the font install to work. I copied the tables, module and macro to my db.

opening my db with the needed fonts (either installed or deleted), i get the an error when starting the db at the following line of the module (error code is 13)

Code:
UploadFileToTable GetCurrentDB("Path") & strFileName, rstUserFiles!File, rstUserFiles

i am using win xp, with access 2000

the barcode fonts I am using are placed in teh table as Long Binary Data.

Any idea as to why it is now not working?

Thanks

Ian
 
Last edited:
G’day Ian.

The main reason for the failure was bad programming on my part. :o
I did not fully qualify the Field declarations.

There are two and you will need to change both.

Change…

As Field

To…

As DAO.Field

Looks like you already have a DAO reference but check that as well.
Also looks like you have an ADO reference but it is a higher priority than DAO.

If you make the changes suggested you will not need to change the priorities.

I have bolded the two additions in the original code.

Hope that fixes the problem and thanks for bring that to my attention. :)

Regards,
Chris.
 
Hi All,

I am wondering how BubBob got on with the above code and if anyone else has this working successfully.

I have basically got the code to work but must be doing something wrong. I suspect its what I've put in the table that stores the file name. I have entered my font name GILC____.TTF just like that in the field FileName. I didn't put anything in the File field but noticed Long Binary Data in there after running the code.

Anyway, it all appears to work but now my database is displaying nothing where the fields are that use the above font. If I manually copy the font to the directory and overwrite the file made by the database then open the database, the font works.

Its like the database has made an empty font file.

Also, Chris, you say in your post that storing the font file will cause bloat, what do you mean by that.

Thanks in advance.

Rachael
 
G’day Rachael.

Second question first…

I’m using the word ‘bloat’ in the sense that anything that is added to a database will make it bigger.
But storing the font as a Long Binary Object will cause the least bloat, a 50k font will increase the database size by 50k.
(It would be possible to zip the file before uploading it to the table and unzipping it after downloading it to the running directory, but I have not done that. One reason is the extra code required and another reason is that most of the time the file I save in the table is an image. Zipping images usually does not produce any reasonable compression.)

Your first question would be better handled with a demo of what you have, and a copy of the font, else we could be here a week.

I would need an A97 version of the bare minimum.

If it is still too big for posting then I could PM you my email address.

BTW, the font name looks suspicious.

Regards,
Chris.
 
Hi Chris,

Just noticed, G'day mate!, hows the weather in Brisbane, very cold and wet here.

anyway, thanks for your reply, I have sinced discovered that I have to have a real copy of the font in the same directory ie copy it from the Windows\Fonts directory. The font must be a new one GILC____.ttf, its Gill Sans MT Condensed, great for reports cos its narrow.

Anyway I was wondering if you would know whether when distributing a database via the package deploy wizard, does the auto exec need to run eachtime the database is used or once the file names are in the table and a copy of the font file is in the same directory as the database then job will be right.

In other words, I can distribute the font file to be in the same directory as the database, and if the table with the font file names is ok, do I need to have the mdlGlobal and the auto exec. The reason being that I already have lots of things happening on startup and want this minimised to reduce start up time on older computers.

Hope this makes sense and thanks heaps for your help

Rachael
 
Sorry, forgot to add that I have tested this theory and it appears to work but am nervous about distributing as I don't fully understand the nuts and bolts of what is going on.

Rach
 
G’day Rach

I would be inclined to have it download and register the font each time the database opens. The time it takes is negligible and the end user might want to move the database to another location even to another machine(s).

As for not fully understanding the nuts and bolts that’s fair enough. I would suggest having a play with it, move it from directory to directory, remove the font from your machine, test again and again and get to know it.

You may find it useful for images as well, company logo’s for instance. With this method you can drop a single copy of a logo and then link all references to that copy.

But it is your system and you must decide which way to go.

Hope it all works out well for you.

Regards,
Chris.
 
Hi Chris,

Thanks so much for your replies, I think I will leave the on startup thing happening. This method works really well.

In the past I was distributing an .exe with the font files in it so if the client didn't have the font, they would click on a button "install fonts" and the .exe would put them in the right place, BUT, then it came to my attention that different OS have different folders for fonts C:\Windows\Fonts and C:\WINNT\Fonts so that buggered that idea up. Also, the font wouldn't actually kick in until you went to the font folder and checked to see if it was there. So I had a message telling the client they had to verify its installation by checking the font folder. Very messy!!!!!

So, thanks, cured alot of my headaches

Rach
 
You’re very welcome Rachael and glad it works.

Next thing you might like to consider is where the Table goes, in the front end or back end?

Speed across the network, for these small files, is not all that important but of course it is faster from a front end table to the front-end directory.

The question really centers on whether you will need to make changes to the font or anything else you may wish to store in the table.

You can view these fonts, images, whatever as front-end configuration files and they have nothing whatsoever to do with the end users data or users preferences. Also, the end user may not have the computer skills to, say, make a watermark image for their reports. In that case you can do it for them, at some cost of course, and send them a new front-end all in one package. The same sort of thing might go for an embedded Word help file that would need to be updated from time to time.

There is more to be considered but overall I go for the table in the front end.

Anyway, I’m glad it is working and hope the weather fines up down there.

Have fun and regards,
Chris.
 

Users who are viewing this thread

Back
Top Bottom