Query called from VBA doesn't have field formats

A

akinter

I have DESPERATELY tried to figure this out. I have created a very big
query in Design view, the looked at the data in datasheet view, then
called it from vba to send it to a pdf. Unfortunately, the data in the
datasheet view and from within vba does not look the same. For some
reason, I get exponentials (these are calculated fields within the
query) which the pdf doesn't like. I have MANY fields, so I really
don't want to format each and every one within the code - besides - if
I was going to do that - I'd just have the query within the code.
Please help! The code is below.

Function MergeAndPrint(tableName As String, pdfPath As String,
prntrName As String)

Dim ofArray As Variant
Dim base As String
Dim xfdfFileName As String
Dim Rec As Integer
Dim hFile As Long

Dim rst As New ADODB.Recordset
Set objShell = CreateObject("WScript.Shell")

If Dir(pdfPath) = "" Then
pdfPath = CurrentProject.Path & "\" & pdfPath
If Dir(pdfPath) = "" Then
MsgBox "ERROR - File Not Found: '" & pdfPath & "'"
Exit Function
End If
End If

ofArray = Split(pdfPath, ".", 2)
base = ofArray(0)
Debug.Print base

rst.Open tableName, CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
rst.MoveFirst
firstJob = True

Rec = 1

Do
If (rst!JMF_ID = Forms![Choose_JMF_Test_Contractor].cmbJMF_ID)
And _
(rst!Ignition_Marshall_ID =
Forms![Choose_JMF_Test_Contractor].CmbIgnitionMarshallID) Then

' this is where the field (some of them) end up with exponential
format!
' MsgBox "dif8 is " & rst![dif8]


'-- Create filename (one for each input record)
xfdfFileName = base & "_" & Pad(Rec, 4) & ".xfdf"

Debug.Print "xfdfFileName=" & xfdfFileName

hFile = FreeFile
'-- Open output xfdf file
Open xfdfFileName For Output As hFile

'-- Write xfdf header
Print #hFile, "<?xml version='1.0' encoding='UTF-8'?>"
Print #hFile, "<xfdf xmlns='http://ns.adobe.com/xfdf/'
xml:space='preserve'>"
Print #hFile, "<fields>"

'-- Dump each field of the record to the output file in
xfdf format
For Each fld In rst.Fields
Print #hFile, "<field name='" & fld.Name & "'>"
Print #hFile, "<value>" & fld.Value & "</value>"
Print #hFile, "</field>"
Next

'-- Write xfdf footer
Print #hFile, "</fields>"
Print #hFile, "<f href='" & pdfPath & "' />"
Print #hFile, "</xfdf>"

Close #hFile

'-- Merge and print the record
PrintPDF xfdfFileName, pdfPath, prntrName
Debug.Print ""
End If

rst.MoveNext
Rec = Rec + 1

Loop Until rst.EOF

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top