Leading zero in filename

V

Vandenee

Hi,

I have following problem: I want to write files (.upd), the name of
the file can have a leading zero (glasnummer).
When I run this code I get the files but the leading zero's deleted.

How can I get a filename with the leading zero (for example 01234.upd)

This is the code I'm using at this moment.

Thanks you for your reply

Sub test()

Dim db As DAO.Database
Dim rst As DAO.Recordset
Set db = CurrentDb
Set rst = db.OpenRecordset("tblGlasgegevens Query")

If rst.RecordCount = 0 Then
MsgBox "Sorry, no records found.", vbExclamation
GoTo end_process
End If

ctr = 1

rst.MoveFirst
Do Until rst.EOF
fn = Format(rst.Fields("glasnummer")) & ".upd"
Open "H:\Gegevens\Michiel\glasgegevens\ACCESS FILES\UPD files
Vimec\" & fn For Output As #1
Print #1, "artikel=" & rst.Fields("glasnummer")
Print #1, "d1=" & rst.Fields("maximum")
Print #1, "d2=" & rst.Fields("mondranddiameter")
Print #1, "d3=" & rst.Fields("triggerdiameter d2")
Print #1, "h1=" & rst.Fields("producthoogte")
Print #1, "h2=" & rst.Fields("bakhoogte")
Print #1, "h3=" & rst.Fields("ondergrens las h3")
Print #1, "h4=" & rst.Fields("bovengrens las h4")
Print #1, "h5=" & rst.Fields("ondergrens been h5")
Print #1, "h6=" & rst.Fields("bovengrens been h6")
Close #1
rst.MoveNext
ctr = ctr + 1
Loop
end_process:
rst.Close
Set rst = Nothing
Set db = Nothing

End Sub
 
D

Douglas J. Steele

Presumably the field glasnummer has been defined as numeric, rather than
text. Numeric values (at least, in Access) don't have leading zeros: 01234
and 1234 are the same numeric value.

Either change the field type to text (so that you can store the leading
zeros), or use an appropriate Format when assigning the file name:

fn = Format(rst.Fields("glasnummer"), "00000") & ".upd"

Incidentally, if that sample code works for you, that implies that you do
not have VBA set to require declaration of all variables (there's no Dim fn
As String). You really should change your settings so that declaration is
required: it can save you literally hours when trying to debug! While in the
VB Editor, go to Tools | Options and look at the Module tab. Ensure that
"Require Variable Declaration" is checked.
 
V

Vandenee

Presumably the field glasnummer has been defined as numeric, rather than
text. Numeric values (at least, in Access) don't have leading zeros: 01234
and 1234 are the same numeric value.

Either change the field type to text (so that you can store the leading
zeros), or use an appropriate Format when assigning the file name:

fn = Format(rst.Fields("glasnummer"), "00000") & ".upd"

Incidentally, if that sample code works for you, that implies that you do
not have VBA set to require declaration of all variables (there's no Dim fn
As String). You really should change your settings so that declaration is
required: it can save you literally hours when trying to debug! While in the
VB Editor, go to Tools | Options and look at the Module tab. Ensure that
"Require Variable Declaration" is checked.

--
Doug Steele, Microsoft Access MVPhttp://I.Am/DougSteele
(no private e-mails, please)
















- Tekst uit oorspronkelijk bericht weergeven -

Doug,

Thanks for your advice. Glasnummer is a text field, but the changing
of the format did the trick.
I'm quit new in VBA so I appriciate your remark on the declaration.

thanks again
 

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

Similar Threads


Top